跟踪第二代Java App引擎

时间:2019-05-23 15:18:35

标签: java google-cloud-firestore stackdriver opencensus

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

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

但是添加新痕迹无法正常工作,而且我也看不到任何有关Firestore的痕迹。

我尝试根据googleopencencus文档添加新的跟踪记录

SELECT COUNT(id) FROM log WHERE logentry->'modality' @> '"CT"'::jsonb

当我注册try (Scope scope = tracer.spanBuilder("myTrace").startScopedSpan()) { // do some http requests } 时,我收到一条错误消息,告诉我它已经配置好了。因为我已经看到码头HttpServlet跟踪,这才有意义。但是我找不到自己的踪迹。

此外,当我检查firestore client library source时,它还会按预期添加跟踪,但它们不会出现在云控制台中。

有人知道我想念什么或在哪里获得帮助吗?

1 个答案:

答案 0 :(得分:1)

请注意,documented是不支持App Engine上的OpenCensus。但是,在我尝试过的情况下它确实起作用。

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

/*
 * 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云跟踪集成不同。enter image description here