跟踪第二代Java App引擎 [英] Tracing second generation java app engine

查看:46
本文介绍了跟踪第二代Java App引擎的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

我尝试根据

i'm trying to get some custom tracing and Firestore tracing added to a second generation app engine java app.

Out of the box i can already see my WebServlets being called and calls to the tasks client library in my cloud console.

But adding new traces is not working and also i see no traces about firestore.

I tried adding new traces as according to the google and opencencus documentation

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

When i register StackdriverTraceExporter.createAndRegister() i get a error message which tells me that it is already configured. Which makes sense as i already see the jetty HttpServlet traces. But i cannot find my own traces.

Also when i check firestore client library source It is also adding traces as expected but they do not appear in the cloud console.

Anyone a idea what i am missing or where to get help?

解决方案

Please note that OpenCensus on App Engine is documented as not supported. However, it does work in the cases that I have tried.

I tried doing tracing from App Engine Standard Java 8 with OpenCensus and was able to get it to work without error. The test app that I tried as based on the Quickstart for Java 8 for App Engine Standard Environment with tracing based on the OpenCensus Tracing Quickstart and Stackdriver Export library. The problem that you hit may be related to use of the Firestore and Spanner libraries, which I did not try. Spanner and many other GCP API libraries have built-in instrumentation with OpenCensus but that should not stop you from adding your own tracing as well. I also tried with Datastore using the App Engine Datastore client library. It ran without error but I found that there were no spans generated for the Datastore calls.

/*
 * 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());
  }

}

The test app generates traces like this. Note that the trace agent is opencensus-java, which is different to the built-in App Engine Cloud Trace integration.

这篇关于跟踪第二代Java App引擎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆