Tracing second generation java app engine

穿精又带淫゛_ 提交于 2019-12-13 03:29:04

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/56278381/tracing-second-generation-java-app-engine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!