如何在Go中向第二代App Engine应用程序添加自定义跟踪?

Google App Engine now supports Go 1.11 via the new second-generation standard environment. While converting an older standard environment application to the second generation, it wasn't obvious how to combine tracing information from the app engine infrastructure with custom tracing I added to the application using OpenCensus.

Even though I had created a stackdriver exporter and registered traces, I wasn't seeing custom trace information in the stackdriver console attached to inbound requests.

The key is I was missing is understanding how span context is communicated to the serving app. Google leverages the X-Cloud-Trace-Context header to propagate span context within requests sent to your serving instances, and the go.opencensus.io/exporter/stackdriver/propagation library provides an implementation to extract and persist this information within http requests.

Don't forget to create a stackdriver exporter, and register traces to it. The docs for the exporter library show an example of this.

// CreateSpanFromRequest returns a context and span based on the http.Request.
// If no existing spancontext is found, this will start a new span.
// Modifies existing request to contain the generated span's context.
func CreateSpanFromRequest(name string, r *http.Request) (context.Context, *trace.Span) {
    var span *trace.Span
    ctx := r.Context()
    httpFormat := &propagation.HTTPFormat{}
    sc, ok := httpFormat.SpanContextFromRequest(r)
    if ok {
        ctx, span = trace.StartSpanWithRemoteParent(ctx, name, sc)
    } else {
        ctx, span = trace.StartSpan(ctx, name)
    }
    // Write the span context into the http.Request.  We do this to
    // to enable chaining handlers together more easily.
    httpFormat.SpanContextToRequest(span.SpanContext(), r)
    return ctx, span
}

Using this, I was able to add custom spans to my handlers that would be properly associated with the incoming request information in stackdriver:

func indexHandler(w http.ResponseWriter, r *http.Request) {
    _, span := CreateSpanFromRequest("indexHandler", r)
    defer span.End()
    if r.URL.Path != "/" {
        http.NotFound(w, r)
        return
    }
    fmt.Fprint(w, "Hello, World!")
}