opencensus-specs icon indicating copy to clipboard operation
opencensus-specs copied to clipboard

Change Span parent after starting span

Open walles opened this issue 5 years ago • 0 comments

Hi!

I would like to do this. Code modded from here:

  public static void doWork(Request request) {
    try (Scope ss =
        tracer
            .spanBuilder("MyChildWorkSpan")
            .setRecordEvents(true)
            .setSampler(Samplers.alwaysSample())
            .startScopedSpan()) {
      
      // This I can write myself
      var remoteParent = parseRemoteParentFromIncomingHeaders(request.headers);

      // vvvvvvv Setting the parent like this is not possible today vvvvvvv
      tracer.getCurrentSpan().setParent(remoteParent);
      // ^^^^^^^ Setting the parent like this is not possible today ^^^^^^^

      doImportantThings();
    }
  }

The reason is that I'm working on an annotations based tracing library.

Annotations work fine for internal spans...

@Span(title = "Doing stuff")
public void doStuff() {
  // ...
}

Using an annotation processor and some code generation, that will create a span and execute doStuff() inside of it. Tested it, works fine.

However, for incoming requests I would like to do this:

@Span(title = "Handling request")
public void handleRequest(Request request) {
  MyTracing.consumePropagationHeaders(request.headers);

  // ...
}

... but the span is already active by the time I start parsing the incoming propagation headers, so even if I manage to figure out a remote parent I have no way of telling OpenCensus about that.

The implementation of consumePropagationHeaders() would look something like this (but with error handling...):

public static void consumePropagationHeaders(Map<String, String> headers) {
  Span parentFromHeaders = spanFromHeaders(headers);
  tracer.getCurrentSpan().setParent(parentFromHeaders);  // <-- This does not exist today
}

walles avatar Jun 29 '20 12:06 walles