请注意,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 集成不同。