Skip to content

fix(node-experimental): Ensure http breadcrumbs are correctly added #8937

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 56 additions & 48 deletions packages/node-experimental/src/integrations/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { EventProcessor, Hub, Integration } from '@sentry/types';
import type { ClientRequest, IncomingMessage, ServerResponse } from 'http';

import type { NodeExperimentalClient } from '../sdk/client';
import { getParentHub } from '../utils/getParentHub';
import { getRequestSpanData } from '../utils/getRequestSpanData';

interface TracingOptions {
Expand Down Expand Up @@ -95,8 +96,11 @@ export class Http implements Integration {
new HttpInstrumentation({
requireParentforOutgoingSpans: true,
requireParentforIncomingSpans: false,
applyCustomAttributesOnSpan: (span, req, res) => {
this._onSpan(span as unknown as OtelSpan, req, res);
requestHook: (span, req) => {
this._updateSentrySpan(span as unknown as OtelSpan, req);
},
responseHook: (span, res) => {
this._addRequestBreadcrumb(span as unknown as OtelSpan, res);
},
}),
],
Expand Down Expand Up @@ -136,64 +140,68 @@ export class Http implements Integration {
return;
};

/** Handle an emitted span from the HTTP instrumentation. */
private _onSpan(
span: OtelSpan,
request: ClientRequest | IncomingMessage,
response: IncomingMessage | ServerResponse,
): void {
const data = getRequestSpanData(span, request, response);
/** Update the Sentry span data based on the OTEL span. */
private _updateSentrySpan(span: OtelSpan, request: ClientRequest | IncomingMessage): void {
const data = getRequestSpanData(span);
const { attributes } = span;

const sentrySpan = _INTERNAL_getSentrySpan(span.spanContext().spanId);
if (sentrySpan) {
sentrySpan.origin = 'auto.http.otel.http';
if (!sentrySpan) {
return;
}

const additionalData: Record<string, unknown> = {
url: data.url,
};
sentrySpan.origin = 'auto.http.otel.http';

if (sentrySpan instanceof Transaction && span.kind === SpanKind.SERVER) {
sentrySpan.setMetadata({ request });
}
const additionalData: Record<string, unknown> = {
url: data.url,
};

if (attributes[SemanticAttributes.HTTP_STATUS_CODE]) {
const statusCode = attributes[SemanticAttributes.HTTP_STATUS_CODE] as string;
additionalData['http.response.status_code'] = statusCode;
if (sentrySpan instanceof Transaction && span.kind === SpanKind.SERVER) {
sentrySpan.setMetadata({ request });
}

sentrySpan.setTag('http.status_code', statusCode);
}
if (attributes[SemanticAttributes.HTTP_STATUS_CODE]) {
const statusCode = attributes[SemanticAttributes.HTTP_STATUS_CODE] as string;
additionalData['http.response.status_code'] = statusCode;

if (data['http.query']) {
additionalData['http.query'] = data['http.query'].slice(1);
}
if (data['http.fragment']) {
additionalData['http.fragment'] = data['http.fragment'].slice(1);
}
sentrySpan.setTag('http.status_code', statusCode);
}

Object.keys(additionalData).forEach(prop => {
const value = additionalData[prop];
sentrySpan.setData(prop, value);
});
if (data['http.query']) {
additionalData['http.query'] = data['http.query'].slice(1);
}
if (data['http.fragment']) {
additionalData['http.fragment'] = data['http.fragment'].slice(1);
}

if (this._breadcrumbs) {
getCurrentHub().addBreadcrumb(
{
category: 'http',
data: {
status_code: response.statusCode,
...data,
},
type: 'http',
},
{
event: 'response',
request,
response,
},
);
Object.keys(additionalData).forEach(prop => {
const value = additionalData[prop];
sentrySpan.setData(prop, value);
});
}

/** Add a breadcrumb for outgoing requests. */
private _addRequestBreadcrumb(span: OtelSpan, response: IncomingMessage | ServerResponse): void {
if (!this._breadcrumbs || span.kind !== SpanKind.CLIENT) {
return;
}

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.

{
category: 'http',
data: {
status_code: response.statusCode,
...data,
},
type: 'http',
},
{
event: 'response',
// request, ???
response,
},
);
}
}

Expand Down
8 changes: 7 additions & 1 deletion packages/node-experimental/src/sdk/otelContextManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { Carrier, Hub } from '@sentry/core';
import { ensureHubOnCarrier, getCurrentHub, getHubFromCarrier } from '@sentry/core';

export const OTEL_CONTEXT_HUB_KEY = api.createContextKey('sentry_hub');
export const OTEL_CONTEXT_PARENT_KEY = api.createContextKey('sentry_parent_context');

function createNewHub(parent: Hub | undefined): Hub {
const carrier: Carrier = {};
Expand Down Expand Up @@ -33,6 +34,11 @@ export class SentryContextManager extends AsyncLocalStorageContextManager {
const existingHub = getCurrentHub();
const newHub = createNewHub(existingHub);

return super.with(context.setValue(OTEL_CONTEXT_HUB_KEY, newHub), fn, thisArg, ...args);
return super.with(
context.setValue(OTEL_CONTEXT_HUB_KEY, newHub).setValue(OTEL_CONTEXT_PARENT_KEY, context),
fn,
thisArg,
...args,
);
}
}
21 changes: 21 additions & 0 deletions packages/node-experimental/src/utils/getParentHub.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as api from '@opentelemetry/api';
import { getCurrentHub } from '@sentry/core';
import type { Hub } from '@sentry/types';

import { OTEL_CONTEXT_HUB_KEY, OTEL_CONTEXT_PARENT_KEY } from '../sdk/otelContextManager';

/**
* Get the hub of the parent context.
* Can be useful if you e.g. need to add a breadcrumb from inside of an ongoing span.
*
* Without this, if you e.g. try to add a breadcrumb from an instrumentation's `onSpan` callback or similar,
* you'll always attach it to the current hub, which in that case will be hyper specific to the instrumentation.
*/
export function getParentHub(): Hub {
const ctx = api.context.active();
const parentContext = ctx?.getValue(OTEL_CONTEXT_PARENT_KEY) as api.Context | undefined;

const parentHub = parentContext && (parentContext.getValue(OTEL_CONTEXT_HUB_KEY) as Hub | undefined);

return parentHub || getCurrentHub();
}
7 changes: 1 addition & 6 deletions packages/node-experimental/src/utils/getRequestSpanData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@ import type { Span as OtelSpan } from '@opentelemetry/sdk-trace-base';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import type { SanitizedRequestData } from '@sentry/types';
import { getSanitizedUrlString, parseUrl } from '@sentry/utils';
import type { ClientRequest, IncomingMessage, ServerResponse } from 'http';

/**
* Get sanitizied request data from an OTEL span.
*/
export function getRequestSpanData(
span: OtelSpan,
_request: ClientRequest | IncomingMessage,
_response: IncomingMessage | ServerResponse,
): SanitizedRequestData {
export function getRequestSpanData(span: OtelSpan): SanitizedRequestData {
const data: SanitizedRequestData = {
url: span.attributes[SemanticAttributes.HTTP_URL] as string,
'http.method': (span.attributes[SemanticAttributes.HTTP_METHOD] as string) || 'GET',
Expand Down