lightstep-tracer-javascript icon indicating copy to clipboard operation
lightstep-tracer-javascript copied to clipboard

Hook to add extra tags to auto instrumented XHR and fetch

Open javoire opened this issue 5 years ago • 6 comments

E.g. right before around here (instrument_fetch.js#L190) we could run a hook/callback like:

tags = extraTagsHook(tags);

and the extraTagsHook would be passed in where the tracer is instantiated, e.g:

opentracing.initGlobalTracer(new lightstep.Tracer({
    access_token   : '{your_access_token}',
    component_name : '{your_service_or_app_name}',
    extraTagsHook: tags => {
      tags['web.origin.pathname'] = window.location.pathname;
      return tags;
    }
}));

WDYT?

Cheers!

related https://github.com/lightstep/lightstep-tracer-javascript/issues/90

javoire avatar May 09 '19 15:05 javoire

I like the idea, although I wonder about registering the callback in the tracer options. That said, we already do kinda non-standard things there so maybe it wouldn't be a huge deal...

austinlparker avatar May 10 '19 04:05 austinlparker

@austinlparker I'm happy to add it anywhere else :) do you have any suggestions?

Cheers

javoire avatar May 11 '19 00:05 javoire

Just speculating, but would it be possible to add these tags, without modifying lightstep-tracer-javascript itself, by setting up a Tracer that extends from the Lightstep Tracer and overrides the inject method? Similar to how we can inject our own headers.

In that method we have access to spanContext, format and carrier. Perhaps spanContext gives us the option to add tags.

charud avatar May 14 '19 20:05 charud

Looks like the spanContext is fairly limited: https://github.com/opentracing/opentracing-javascript/blob/master/src/span_context.ts What we need to access is the actual span (https://github.com/opentracing/opentracing-javascript/blob/master/src/span.ts).

Perhaps it would be useful to have this feature baked into lightstep-tracer. But it could also be accomplished by the consumer, by wrapping globalTracer().startSpan() in a function that adds the custom tags. Unless you need context from the actual XHR/Fetch call, then the implementation needs to look different either way.

charud avatar May 14 '19 20:05 charud

It seems like the latter suggestion (a wrapper around startSpan) could be a nice little package to put in opentracing-contrib for the entire community.

austinlparker avatar May 16 '19 15:05 austinlparker

Yes SpanContext is a "child" of Span

I like the span wrapper idea.

javoire avatar May 22 '19 14:05 javoire