javagoogle-app-enginegoogle-cloud-platformgoogle-cloud-stackdrivergoogle-cloud-trace

How to use Stackdriver Trace to create custom spans in App Engine Standard?


I try to add custom spans and annotations to an AppEngine Standard application. In the Traces page of the GCP console, I see the default spans but not the custom spans that I have added.

I follow the documentation: https://cloud.google.com/trace/docs/setup/java I use the latest version of opencensus-api 0.23.0

The Stackdrive Trace API is enabled and I see in the metrics page of the API that the application does successful google.devtools.cloudtrace.v2.TraceService.BatchWriteSpans calls.

I created a sample application based on the official helloworld application that reproduces this problem.

The interesting parts:

private static final Tracer tracer = Tracing.getTracer();

static {
    try {
        System.out.println("Init StackdriverTraceExporter");
        StackdriverTraceExporter.createAndRegister(
                StackdriverTraceConfiguration.builder()
                        .setProjectId("project-id")
                        .build());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

public static String getInfo() {

    try (Scope ignored = tracer.spanBuilder("MyChildWorkSpan").setSampler(Samplers.alwaysSample()).startScopedSpan()) {
        tracer.getCurrentSpan().addAnnotation("annotation example");
        try {
            Thread.sleep(100);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        System.out.println("annotation created");
        return "Version: " + System.getProperty("java.version")
                + " OS: " + System.getProperty("os.name")
                + " User: " + System.getProperty("user.name")
                + " Span: " + tracer.getCurrentSpan();
    }
}

You can see the full code here


I figured out my mistake. This is what I saw when I opened the question: trace_list The problem is that the service and version are selected.

These custom spans appear like a separate request and I need to select all services otherwise they are hidden. child span

So at least I can see that this works in a way but this is not what I intended. I would like to achieve something like this: combined_trace

How can I attach my custom spans to the root span that is displayed for the request?


Solution

  • I checked your issue and I made some reproductions on my side and I was able to add the custom span "MyChildWorkSpan" and the annotation "annotation example" making same changes as your did from your code to the Helloworld application in AppEngine Standard with Java 8.

    I've used both versions of the io.opencensus (0.12.2 as documented here (with Maven) and 0.23.0 which is the version you're trying to use). In both cases, I was succeeded to see the custom span in the Stackdriver Trace list.

    Could you attach a screenshot of what you're seeing in your Google Cloud console > Stackdriver Trace list when browsing to the ? Can you see the custom span "MyChildWorkSpan" and the annotation "annotation example" there? Or is there any other custom span/annotation you're not seeing?