Skip to content
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

fix(node-experimental): Ensure we only create HTTP spans when outgoing #8966

Merged
merged 1 commit into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
105 changes: 57 additions & 48 deletions packages/node-experimental/src/integrations/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,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 +139,70 @@ 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);
getCurrentHub().addBreadcrumb(
{
category: 'http',
data: {
status_code: response.statusCode,
...data,
},
type: 'http',
},
{
event: 'response',
// TODO FN: Do we need access to `request` here?
// If we do, we'll have to use the `applyCustomAttributesOnSpan` hook instead,
// but this has worse context semantics than request/responseHook.
response,
},
);
}
}

Expand Down
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