【问题标题】:Tracing second generation java app engine追踪二代java应用引擎
【发布时间】:2023-03-07 14:09:02
【问题描述】:

我正在尝试将一些自定义跟踪和 Firestore 跟踪添加到第二代应用引擎 java 应用中。

开箱即用,我已经可以看到我的 WebServlet 被调用并调用了我的云控制台中的任务客户端库。

但是添加新的痕迹不起作用,而且我也没有看到关于 firestore 的痕迹。

我尝试根据googleopencencus 文档添加新的痕迹

try (Scope scope = tracer.spanBuilder("myTrace").startScopedSpan()) {
  // do some http requests
}

当我注册StackdriverTraceExporter.createAndRegister() 时,我收到一条错误消息,告诉我它已经配置好了。这是有道理的,因为我已经看到了码头 HttpServlet 的踪迹。但我找不到自己的踪迹。

当我检查firestore client library source 时,它也按预期添加了跟踪,但它们没有出现在云控制台中。

任何人知道我缺少什么或在哪里获得帮助?

【问题讨论】:

    标签: java google-cloud-firestore stackdriver opencensus


    【解决方案1】:

    请注意,App Engine 上的 OpenCensus 是 documented,因为不受支持。但是,它在我尝试过的情况下确实有效。

    我尝试使用 OpenCensus 从 App Engine 标准 Java 8 进行跟踪,并且能够使其正常工作而不会出错。我尝试的测试应用程序基于Quickstart for Java 8 for App Engine Standard Environment,跟踪基于OpenCensus Tracing QuickstartStackdriver Export 库。您遇到的问题可能与使用 Firestore 和 Spanner 库有关,我没有尝试过。 Spanner 和许多其他 GCP API 库都内置了 OpenCensus 检测,但这不应阻止您添加自己的跟踪。我还尝试使用 App Engine Datastore 客户端库使用 Datastore。它运行没有错误,但我发现没有为 Datastore 调用生成跨度。

    /*
     * Copyright 2019 Google Inc.
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     * http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    
    package com.example.appengine.java8;
    
    import com.google.appengine.api.utils.SystemProperty;
    
    import io.opencensus.common.Scope;
    import io.opencensus.exporter.trace.stackdriver.StackdriverTraceConfiguration;
    import io.opencensus.exporter.trace.stackdriver.StackdriverTraceExporter;
    import io.opencensus.trace.AttributeValue;
    import io.opencensus.trace.Span;
    import io.opencensus.trace.Status;
    import io.opencensus.trace.Tracing;
    import io.opencensus.trace.Tracer;
    import io.opencensus.trace.config.TraceConfig;
    import io.opencensus.trace.config.TraceParams;
    import io.opencensus.trace.samplers.Samplers;
    
    import java.io.IOException;
    import java.util.Properties;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    @WebServlet(name = "OCAppEngineTest", value = "/main")
    public class OCAppEngineTest extends HttpServlet {
    
      @Override
      public void destroy() {
        Tracing.getExportComponent().shutdown();
      }
    
      @Override
      public void doGet(HttpServletRequest request, HttpServletResponse response)
          throws IOException {
        Tracer tracer = Tracing.getTracer();
        try (Scope scope = tracer.spanBuilder("main").startScopedSpan()) {
          Properties properties = System.getProperties();
          response.setContentType("text/plain");
          response.getWriter().println("Hello App Engine - Standard using "
                + SystemProperty.version.get() + " Java "
                + properties.get("java.specification.version"));
        }
      }
    
      public static String getInfo() {
        return "Version: " + System.getProperty("java.version")
              + " OS: " + System.getProperty("os.name")
              + " User: " + System.getProperty("user.name");
      }
    
      @Override
      public void init() throws ServletException {
        try {
          setupOpenCensusAndStackdriverExporter();
        } catch (IOException e) {
          // log message
        }
      }
    
      private void setupOpenCensusAndStackdriverExporter()
          throws IOException {
        String appId = SystemProperty.applicationId.get();
    
        StackdriverTraceExporter.createAndRegister(
          StackdriverTraceConfiguration.builder()
            .setProjectId(appId)
            .build());
        TraceConfig traceConfig = Tracing.getTraceConfig();
        TraceParams activeTraceParams = traceConfig.getActiveTraceParams();
        traceConfig.updateActiveTraceParams(
          activeTraceParams.toBuilder().setSampler(
            Samplers.alwaysSample()).build());
      }
    
    }
    

    测试应用会生成这样的跟踪。请注意,trace 代理是 opencensus-java,与内置的 App Engine Cloud Trace 集成不同。

    【讨论】:

      猜你喜欢
      • 2011-05-25
      • 1970-01-01
      • 1970-01-01
      • 2010-12-11
      • 2011-11-18
      • 1970-01-01
      • 2014-08-30
      • 2012-04-11
      • 1970-01-01
      相关资源
      最近更新 更多