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

ref(serverless): Extract propagation context #8429

Merged
merged 2 commits into from
Jul 4, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions packages/serverless/src/awslambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Scope } from '@sentry/node';
import * as Sentry from '@sentry/node';
import { captureException, captureMessage, flush, getCurrentHub, withScope } from '@sentry/node';
import type { Integration } from '@sentry/types';
import { baggageHeaderToDynamicSamplingContext, extractTraceparentData, isString, logger } from '@sentry/utils';
import { isString, logger, tracingContextFromHeaders } from '@sentry/utils';
// NOTE: I have no idea how to fix this right now, and don't want to waste more time, as it builds just fine — Kamil
// eslint-disable-next-line import/no-unresolved
import type { Context, Handler } from 'aws-lambda';
Expand Down Expand Up @@ -274,17 +274,20 @@ export function wrapHandler<TEvent, TResult>(
}, timeoutWarningDelay) as unknown as NodeJS.Timeout;
}

// Applying `sentry-trace` to context
let traceparentData;
const eventWithHeaders = event as { headers?: { [key: string]: string } };
if (eventWithHeaders.headers && isString(eventWithHeaders.headers['sentry-trace'])) {
traceparentData = extractTraceparentData(eventWithHeaders.headers['sentry-trace']);
}
const hub = getCurrentHub();

const baggageHeader = eventWithHeaders.headers && eventWithHeaders.headers.baggage;
const dynamicSamplingContext = baggageHeaderToDynamicSamplingContext(baggageHeader);
const eventWithHeaders = event as { headers?: { [key: string]: string } };

const hub = getCurrentHub();
const sentryTrace =
eventWithHeaders.headers && isString(eventWithHeaders.headers['sentry-trace'])
? eventWithHeaders.headers['sentry-trace']
: undefined;
Comment on lines +281 to +284
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const sentryTrace =
eventWithHeaders.headers && isString(eventWithHeaders.headers['sentry-trace'])
? eventWithHeaders.headers['sentry-trace']
: undefined;
const sentryTrace = eventWithHeaders.headers?.['sentry-trace'] || undefined;

as we are already using optional chaining, might as well use it here as well ;)

Copy link
Member Author

Choose a reason for hiding this comment

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

We need the isString check - see #8425 (comment) for more details.

const baggage = eventWithHeaders.headers?.baggage;
const { traceparentData, dynamicSamplingContext, propagationContext } = tracingContextFromHeaders(
sentryTrace,
baggage,
);
hub.getScope().setPropagationContext(propagationContext);

const transaction = hub.startTransaction({
name: context.functionName,
Expand Down
30 changes: 10 additions & 20 deletions packages/serverless/src/gcpfunction/http.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import type { AddRequestDataToEventOptions } from '@sentry/node';
import { captureException, flush, getCurrentHub } from '@sentry/node';
import {
baggageHeaderToDynamicSamplingContext,
extractTraceparentData,
isString,
isThenable,
logger,
stripUrlQueryAndFragment,
} from '@sentry/utils';
import { isString, isThenable, logger, stripUrlQueryAndFragment, tracingContextFromHeaders } from '@sentry/utils';

import { domainify, proxyFunction } from './../utils';
import type { HttpFunction, WrapperOptions } from './general';
Expand Down Expand Up @@ -68,21 +61,18 @@ function _wrapHttpFunction(fn: HttpFunction, wrapOptions: Partial<HttpFunctionWr
...wrapOptions,
};
return (req, res) => {
const hub = getCurrentHub();

const reqMethod = (req.method || '').toUpperCase();
const reqUrl = stripUrlQueryAndFragment(req.originalUrl || req.url || '');

// Applying `sentry-trace` to context
let traceparentData;
const reqWithHeaders = req as { headers?: { [key: string]: string } };
if (reqWithHeaders.headers && isString(reqWithHeaders.headers['sentry-trace'])) {
traceparentData = extractTraceparentData(reqWithHeaders.headers['sentry-trace']);
}

const baggageHeader = reqWithHeaders.headers?.baggage;

const dynamicSamplingContext = baggageHeaderToDynamicSamplingContext(baggageHeader);

const hub = getCurrentHub();
const sentryTrace = req.headers && isString(req.headers['sentry-trace']) ? req.headers['sentry-trace'] : undefined;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const sentryTrace = req.headers && isString(req.headers['sentry-trace']) ? req.headers['sentry-trace'] : undefined;
const sentryTrace = req.headers?.['sentry-trace'] || undefined;

const baggage = req.headers?.baggage;
const { traceparentData, dynamicSamplingContext, propagationContext } = tracingContextFromHeaders(
sentryTrace,
baggage,
);
hub.getScope().setPropagationContext(propagationContext);

const transaction = hub.startTransaction({
name: `${reqMethod} ${reqUrl}`,
Expand Down
1 change: 1 addition & 0 deletions packages/serverless/test/__mocks__/@sentry/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const fakeScope = {
setSpan: jest.fn(),
getTransaction: jest.fn(() => fakeTransaction),
setSDKProcessingMetadata: jest.fn(),
setPropagationContext: jest.fn(),
};
export const fakeSpan = {
finish: jest.fn(),
Expand Down