Skip to content

Conversation

@mydea
Copy link
Member

@mydea mydea commented Sep 4, 2023

Due to the way OTEL handles context (=scope/hub), I noticed that breadcrumbs added in the http integration are not correctly captured. The reason is that OTEL forks the hub for the http request context, and runs the hooks inside of this forked context. In order to deal with this, we have to actually attach the breadcrumb to the parent Hub in this specific case.

So this code:

app.get("/error", async (request, reply) => {
    console.log('before request');

    await makeRequest(`http://example.com/`);

    console.log('after request');

    Sentry.captureException(new Error(`test error`));

    reply.send({status: "OK"})
  });

Results in a correct transaction:

image

and a correct error

image

This is a bit hacky, but works as far as I can tell. Here is the code that "triggers" this: https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-http/src/http.ts#L532. note that the context.with() above results in a hub fork for us.

Not sure if there is a more elegant solution for this...? But I guess this is a fundamental challenge when we try to align our hub/scope usage with OTEL context.

One note on requestHook & responseHook vs. applyCustomAttributesOnSpan: These differ slightly in when/where they are executed. I got less consistent/logical results of hub propagation on that hook, where the xxxHook callbacks worked more reliably for me. I think the reason is that applyCustomAttributesOnSpan is called a bit later in the request lifecycle... However, the downside is that we do not easily have the request and response available in the callback, so we can't easily add it as a breadcrumb hint (without caching it somewhere, ...). Not sure how important that is to us...

Due to the way OTEL handles context (=scope/hub), I noticed that breadcrumbs added in the http integration are not correctly captured.
The reason is that OTEL forks the hub for the http request context, and runs the hooks inside of this forked context.
In order to deal with this, we have to actually attach the breadcrumb to the _parent_ Hub in this specific case.
@mydea mydea requested review from AbhiPrasad, Lms24 and lforst September 4, 2023 11:22
@mydea mydea self-assigned this Sep 4, 2023
}

const data = getRequestSpanData(span);
getParentHub().addBreadcrumb(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, the most correct (?) would be to add the breadcrumb both to the current and the parent hub 🤔 does that make sense? We could also make it e.g.:

const hub = getCurrentHub();
const parentHub = getParentHub();
hub.addBreadcrumb(breadcrumb);
if (hub !== parentHub) {
  parentHub.addBreadcrumb(breadcrumb);
}

To be on the very safe side? The breadcrumb to the "actual current" hub would only really be relevant/visible if something happened inside of the OTEL internals, I believe (but it's a bit hard to say with all the callbacks/nested function scopes/etc.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without being too deep into this, it sounds reasonable to me to add it to both.

@github-actions
Copy link
Contributor

github-actions bot commented Sep 4, 2023

size-limit report 📦

Path Size
@sentry/browser (incl. Tracing, Replay) - Webpack (gzipped) 75.32 KB (-0.01% 🔽)
@sentry/browser (incl. Tracing) - Webpack (gzipped) 31.21 KB (-0.01% 🔽)
@sentry/browser - Webpack (gzipped) 21.83 KB (-0.01% 🔽)
@sentry/browser (incl. Tracing, Replay) - ES6 CDN Bundle (gzipped) 69.84 KB (-0.01% 🔽)
@sentry/browser (incl. Tracing) - ES6 CDN Bundle (gzipped) 28.16 KB (-0.02% 🔽)
@sentry/browser - ES6 CDN Bundle (gzipped) 20.15 KB (-0.02% 🔽)
@sentry/browser (incl. Tracing, Replay) - ES6 CDN Bundle (minified & uncompressed) 220.72 KB (0%)
@sentry/browser (incl. Tracing) - ES6 CDN Bundle (minified & uncompressed) 85 KB (0%)
@sentry/browser - ES6 CDN Bundle (minified & uncompressed) 59.71 KB (0%)
@sentry/browser (incl. Tracing) - ES5 CDN Bundle (gzipped) 31.06 KB (-0.01% 🔽)
@sentry/react (incl. Tracing, Replay) - Webpack (gzipped) 75.34 KB (-0.01% 🔽)
@sentry/react - Webpack (gzipped) 21.86 KB (-0.01% 🔽)
@sentry/nextjs Client (incl. Tracing, Replay) - Webpack (gzipped) 93.2 KB (-0.01% 🔽)
@sentry/nextjs Client - Webpack (gzipped) 50.78 KB (-0.01% 🔽)

@mydea
Copy link
Member Author

mydea commented Sep 4, 2023

Note that one thing that does not solve is that if you have this function:

function makeRequest(url) {
    return new Promise((resolve) => {
      http.request(url, (httpRes) => {
        console.log('log inside of http callback');
        httpRes.on('data', () => {});
        httpRes.on('end', () => {
        resolve();
        });
      }).end();
    });
  }

The log inside of http callback will not be visible as a breadcrumb... Not sure if/what the way would be to make that show up, as that is auto-instrument-captured somewhere inside of the OTEL context stack that has been forked off, some levels deep 🤔

Copy link
Member

@Lms24 Lms24 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still feel like I have limited context around OTEL context<->sentry hubs but the workaround looks reasonable to me.

}

const data = getRequestSpanData(span);
getParentHub().addBreadcrumb(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without being too deep into this, it sounds reasonable to me to add it to both.

@mydea
Copy link
Member Author

mydea commented Sep 7, 2023

Closing this, as actually this needs more in-depth fixes to properly work 😬

@mydea mydea closed this Sep 7, 2023
@mydea mydea deleted the fn/otel-http-breadcrumb branch September 7, 2023 08:04
mydea added a commit that referenced this pull request Sep 11, 2023
#8966)

This is a fork of
#8937, with only the
"uncontroversial" stuff, mainly fixing that we only create HTTP
breadcrumbs for _outgoing_ requests.

In addition, this also migrates to using `requestHook` and
`responseHook` instead of `applyCustomAttributesOnSpan`. We may have to
revisit this later, but these hooks seem to have a better context
awareness (=they are called in a more reasonable OTEL context, which
gives the callbacks there better access to scope data etc). However that
means we cannot (easily) pass both request and response as breadcrumb
hints - not sure how important that is to us... For now I'd say that's
OK.

Note that also `requestHook` is only called when the request finishes,
so we already have all the response OTEL span attributes correctly set
there.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants