appengine icon indicating copy to clipboard operation
appengine copied to clipboard

Interaction with OpenCensus tracing

Open carlpett opened this issue 6 years ago • 1 comments

Hi, I'm incorporating OpenCensus/Stackdriver tracing in an app on AppEngine Standard (Go 1.11). I'm seeing some unexpected interaction between my spans and the "automatic" spans from Datastore, Urlfetch and friends. These don't seem to get linked to my spans as expected, but always have the automatic / root span as a parent. Is this expected? From looking quickly at the code in this repo, it looks like there is no hooks here, so I guess the tracing is performed in the remote service endpoint?

My PoC code right now is basically

func myHttpHandler(w http.ResponseWriter, r *http.Request) {
	httpFormat := propagation.HTTPFormat{}
	ctx := r.Context()
	var span *trace.Span
	if root, ok := httpFormat.SpanContextFromRequest(r); ok {
		ctx, span = trace.StartSpanWithRemoteParent(ctx, "my-root", root)
		defer span.End()
	}
	// ... do stuff with more spans, and calls to GCP services
}

Should this work?

carlpett avatar Aug 13 '19 20:08 carlpett

I dug into this a bit more recently and found the tracing being hooked up here: https://github.com/golang/appengine/blob/b6ce0843b556e44e6416df5c2fd05db0869cc8d4/internal/api.go#L404-L406

It seems that the reason for the spans getting connected to the root span rather than as children of my relevant spans is that the context object has the headers from the original request, and does not check the context. So it only gets the incoming trace id, not any child spans started. Is this considered a bug or a feature? From my perspective it makes Cloud Trace really hard to actually use well with AppEngine, since the traces get jumbled.

ping @broady @juliehockett

carlpett avatar Feb 21 '20 11:02 carlpett