Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

Change Span parent after starting span #271

Open
walles opened this issue Jun 29, 2020 · 0 comments
Open

Change Span parent after starting span #271

walles opened this issue Jun 29, 2020 · 0 comments

Comments

@walles
Copy link

walles commented Jun 29, 2020

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
}
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant