0

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

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

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

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

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

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

此外,当我检查firestore 客户端库源时,它也按预期添加了跟踪,但它们没有出现在云控制台中。

任何人都知道我错过了什么或在哪里获得帮助?

4

1 回答 1

1

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

我尝试使用 OpenCensus 从 App Engine Standard Java 8 进行跟踪,并且能够使其正常工作而不会出错。我尝试的测试应用程序基于适用于 App Engine 标准环境的 Java 8 快速入门,具有基于OpenCensus 跟踪快速入门和Stackdriver导出库的跟踪。您遇到的问题可能与使用 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());
  }

}

测试应用程序会生成这样的跟踪。请注意,跟踪代理是 opencensus-java,这与内置的 App Engine Cloud Trace 集成不同。在此处输入图像描述

于 2019-06-28T00:21:37.283 回答