sentry-docs icon indicating copy to clipboard operation
sentry-docs copied to clipboard

Document how to associate `sentry-trace` with manual instrumentation

Open marandaneto opened this issue 3 years ago • 19 comments

We need to document how to associate the sentry-trace if using manual instrumentation for Java, .NET, React Native and other SDKs if possible.

just a Java example:

// assume there's a span/transaction or just create a new one
final ISpan activeSpan = hub.getSpan();
final SentryTraceHeader sentryTraceHeader = activeSpan.toSentryTrace();

// assume you can intercept the request call and add its spans header
request.getHeaders().add(sentryTraceHeader.getName(), sentryTraceHeader.getValue());

activeSpan.finish();

cc @bruno-garcia @jennmueng @maciejwalkowiak @Tyrrrz @brustolin

marandaneto avatar Feb 01 '21 10:02 marandaneto

@marandaneto Let's sync on this before you do it. My work on the dynamic sampling/tracestate headers will likely come into play.

lobsterkatie avatar Feb 01 '21 14:02 lobsterkatie

.NET example:

using var httpClient = new HttpClient(new SentryHttpMessageHandler());

I think this is pretty unique to .NET though.

One can also do this fully manually:

var span = hub.GetSpan();
var traceHeader = span.GetTraceHeader();

using var request = new HttpRequestMessage(HttpMethod.Get, "...");
request.Headers.Add("sentry-trace", traceHeader.ToString());

span.Finish();

Tyrrrz avatar Feb 01 '21 15:02 Tyrrrz

@maciejwalkowiak how would we do it on Java right now? is my code snippet accurate?

marandaneto avatar Feb 19 '21 12:02 marandaneto

If the snippet is about creating a span around HTTP call, it think we should also show how to start a child span, set the status and finish. Like its done here: https://github.com/getsentry/sentry-java/blob/main/sentry-spring/src/main/java/io/sentry/spring/tracing/SentrySpanClientHttpRequestInterceptor.java#L38

maciejwalkowiak avatar Feb 19 '21 12:02 maciejwalkowiak

that makes total sense, I just wanted to be sure that the important bits are correct which is:

final SentryTraceHeader sentryTraceHeader = activeSpan.toSentryTrace();
request.getHeaders().add(sentryTraceHeader.getName(), sentryTraceHeader.getValue());

marandaneto avatar Feb 19 '21 12:02 marandaneto

FYI - IDK how soon it will be released, but once my dynamic sampling work on the tracestate header goes out, toSentryTrace will be deprecated in favor of getTraceHeaders, which will return both the sentry-trace and tracestate headers.

lobsterkatie avatar Feb 26 '21 16:02 lobsterkatie

This issue has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you label it Status: Backlog or Status: In Progress, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

github-actions[bot] avatar Mar 19 '21 18:03 github-actions[bot]

It's not clear to me how to instantiate a trace from a request with the SentryTraceHeader - is there any documentation on this?

crummy avatar Mar 22 '21 09:03 crummy

@crummy indeed, thanks for reporting this, subscribe to this issue please https://github.com/getsentry/sentry-java/issues/1346

marandaneto avatar Mar 22 '21 13:03 marandaneto

@lobsterkatie mind reviewing https://github.com/getsentry/sentry-docs/pull/3324 ? we've got questions from multiple people already so we need to tackle this at least for the Java SDK

marandaneto avatar Mar 29 '21 16:03 marandaneto

Wanna implement manual passing trace_id for Python Sanic requests (sentry's integration do nothing here), but can't find any info about header name to pass trace_id. What's the name of default sentry trace header?

Olegt0rr avatar Apr 04 '21 11:04 Olegt0rr

@Olegt0rr it's the PR's title, sentry-trace

marandaneto avatar Apr 06 '21 07:04 marandaneto

Keeping it open as a placeholder for other platforms, it's still not documented for the other SDKs other than Java/Android afaik.

marandaneto avatar Apr 06 '21 07:04 marandaneto

This should be common content and just the code snippet would change per platform, only relevant for server-side SDKs. Java is already done but should adapt to the common content.

marandaneto avatar Jan 21 '22 13:01 marandaneto

Would be great to see some info about this in the node and serverless (node) docs. I've been digging into the code for days trying to figure out how to get traces to be passed from one lambda through into another when calling them via the AWS SDK instead of HTTP.

ex-nerd avatar Jul 07 '22 02:07 ex-nerd

:+1: me too. I've got the sentry trace-id header going from my SPA to graphql backend but unable to make use of it in the backend. I'm seeing it in my console.logs() but doing stuff like the below is doing nothing (yes, the value is being retrieved correctly). Tried a bunch of different ways of using it besides that. There are no docs on how to do this properly.

context.transaction = Sentry.startTransaction({
        op: 'gql',
        name: request.operationName,
        traceId: request.http.headers.get('sentry-trace')
      })

Edit: actually finally got this working after taking a break and coming back and reading what the Express integration does.

import { extractTraceparentData } from '@sentry/tracing'
...
const traceparentData = extractTraceparentData(request.http.headers.get('sentry-trace'))

      context.transaction = Sentry.startTransaction({
        op: 'gql',
        name: 'GraphQLTransaction',
        ...traceparentData
      })

Then it worked. Very easy, but some docs would have prevented hours of digging.

callum-p avatar Jul 14 '22 04:07 callum-p

knowing how to get the current sentry-trace id would also be helpful.

Once use case i have is for not rest calls

mgarf avatar Aug 15 '22 20:08 mgarf

It seems like the docs at https://docs.sentry.io/platforms/node/guides/aws-lambda/performance/connect-services/ have been updated with instructions for generating a header via span.toTraceparent … I assume this includes both sentry-trace and the poorly-documented baggage header but I haven't had a chance to test this or figure out how to it will affect things if I start a transaction inside of another (as there is no way to pass this info at the request level for things like AWS lambda invocations, e.g. https://github.com/getsentry/sentry-javascript/issues/7701).

ex-nerd avatar Apr 10 '23 21:04 ex-nerd

👍 me too. I've got the sentry trace-id header going from my SPA to graphql backend but unable to make use of it in the backend. I'm seeing it in my console.logs() but doing stuff like the below is doing nothing (yes, the value is being retrieved correctly). Tried a bunch of different ways of using it besides that. There are no docs on how to do this properly.

context.transaction = Sentry.startTransaction({
        op: 'gql',
        name: request.operationName,
        traceId: request.http.headers.get('sentry-trace')
      })

Edit: actually finally got this working after taking a break and coming back and reading what the Express integration does.

import { extractTraceparentData } from '@sentry/tracing'
...
const traceparentData = extractTraceparentData(request.http.headers.get('sentry-trace'))

      context.transaction = Sentry.startTransaction({
        op: 'gql',
        name: 'GraphQLTransaction',
        ...traceparentData
      })

Then it worked. Very easy, but some docs would have prevented hours of digging.

👍 me too. I've got the sentry trace-id header going from my SPA to graphql backend but unable to make use of it in the backend. I'm seeing it in my console.logs() but doing stuff like the below is doing nothing (yes, the value is being retrieved correctly). Tried a bunch of different ways of using it besides that. There are no docs on how to do this properly.

context.transaction = Sentry.startTransaction({
        op: 'gql',
        name: request.operationName,
        traceId: request.http.headers.get('sentry-trace')
      })

Edit: actually finally got this working after taking a break and coming back and reading what the Express integration does.

import { extractTraceparentData } from '@sentry/tracing'
...
const traceparentData = extractTraceparentData(request.http.headers.get('sentry-trace'))

      context.transaction = Sentry.startTransaction({
        op: 'gql',
        name: 'GraphQLTransaction',
        ...traceparentData
      })

Then it worked. Very easy, but some docs would have prevented hours of digging.

Thanks bro! You saved my time! Would be good to have some examples of using custom instrumentation in the sentry docs

JR-RomanZaiats avatar Nov 10 '23 12:11 JR-RomanZaiats