Skip to content

Commit 7f30526

Browse files
committed
chore(aws): Remove manual span creation
1 parent 815fc27 commit 7f30526

File tree

3 files changed

+18
-215
lines changed

3 files changed

+18
-215
lines changed

dev-packages/e2e-tests/test-applications/aws-serverless-esm/tests/basic.test.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ test('AWS Serverless SDK sends events in ESM mode', async ({ request }) => {
4343
trace_id: expect.stringMatching(/[a-f0-9]{32}/),
4444
});
4545

46-
expect(transactionEvent.spans).toHaveLength(3);
46+
expect(transactionEvent.spans).toHaveLength(2);
4747

4848
// shows that the Otel Http instrumentation is working
4949
expect(transactionEvent.spans).toContainEqual(
@@ -58,19 +58,6 @@ test('AWS Serverless SDK sends events in ESM mode', async ({ request }) => {
5858
}),
5959
);
6060

61-
expect(transactionEvent.spans).toContainEqual(
62-
expect.objectContaining({
63-
data: {
64-
'sentry.op': 'function.aws.lambda',
65-
'sentry.origin': 'auto.function.serverless',
66-
'sentry.source': 'component',
67-
},
68-
description: 'my-lambda',
69-
op: 'function.aws.lambda',
70-
origin: 'auto.function.serverless',
71-
}),
72-
);
73-
7461
// shows that the manual span creation is working
7562
expect(transactionEvent.spans).toContainEqual(
7663
expect.objectContaining({

packages/aws-serverless/src/sdk.ts

Lines changed: 5 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,24 @@
1-
import type { Integration, Options, Scope, Span } from '@sentry/core';
2-
import {
3-
applySdkMetadata,
4-
debug,
5-
getSDKSource,
6-
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
7-
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
8-
} from '@sentry/core';
1+
import type { Integration, Options, Scope } from '@sentry/core';
2+
import { applySdkMetadata, debug, getSDKSource } from '@sentry/core';
93
import type { NodeClient, NodeOptions } from '@sentry/node';
104
import {
115
captureException,
126
captureMessage,
13-
continueTrace,
147
flush,
158
getCurrentScope,
169
getDefaultIntegrationsWithoutPerformance,
1710
initWithoutDefaultIntegrations,
18-
startSpanManual,
1911
withScope,
2012
} from '@sentry/node';
2113
import type { Context, Handler } from 'aws-lambda';
2214
import { existsSync } from 'fs';
23-
import { hostname } from 'os';
2415
import { basename, resolve } from 'path';
2516
import { performance } from 'perf_hooks';
2617
import { types } from 'util';
2718
import { DEBUG_BUILD } from './debug-build';
2819
import { awsIntegration } from './integration/aws';
2920
import { awsLambdaIntegration } from './integration/awslambda';
30-
import { getAwsTraceData, markEventUnhandled } from './utils';
21+
import { markEventUnhandled } from './utils';
3122

3223
const { isPromise } = types;
3324

@@ -54,12 +45,6 @@ export interface WrapperOptions {
5445
* @default false
5546
*/
5647
captureAllSettledReasons: boolean;
57-
/**
58-
* Automatically trace all handler invocations.
59-
* You may want to disable this if you use express within Lambda.
60-
* @default true
61-
*/
62-
startTrace: boolean;
6348
}
6449

6550
/**
@@ -207,18 +192,6 @@ function enhanceScopeWithEnvironmentData(scope: Scope, context: Context, startTi
207192
});
208193
}
209194

210-
/**
211-
* Adds additional transaction-related information from the environment and AWS Context to the Sentry Scope.
212-
*
213-
* @param scope Scope that should be enhanced
214-
* @param context AWS Lambda context that will be used to extract some part of the data
215-
*/
216-
function enhanceScopeWithTransactionData(scope: Scope, context: Context): void {
217-
scope.setTransactionName(context.functionName);
218-
scope.setTag('server_name', process.env._AWS_XRAY_DAEMON_ADDRESS || process.env.SENTRY_NAME || hostname());
219-
scope.setTag('url', `awslambda:///${context.functionName}`);
220-
}
221-
222195
/**
223196
* Wraps a lambda handler adding it error capture and tracing capabilities.
224197
*
@@ -237,7 +210,6 @@ export function wrapHandler<TEvent, TResult>(
237210
captureTimeoutWarning: true,
238211
timeoutWarningLimit: 500,
239212
captureAllSettledReasons: false,
240-
startTrace: true,
241213
...wrapOptions,
242214
};
243215
let timeoutWarningTimer: NodeJS.Timeout;
@@ -293,7 +265,7 @@ export function wrapHandler<TEvent, TResult>(
293265
}, timeoutWarningDelay) as unknown as NodeJS.Timeout;
294266
}
295267

296-
async function processResult(span?: Span): Promise<TResult> {
268+
async function processResult(): Promise<TResult> {
297269
const scope = getCurrentScope();
298270

299271
let rv: TResult;
@@ -314,60 +286,15 @@ export function wrapHandler<TEvent, TResult>(
314286
throw e;
315287
} finally {
316288
clearTimeout(timeoutWarningTimer);
317-
if (span?.isRecording()) {
318-
span.end();
319-
}
320289
await flush(options.flushTimeout).catch(e => {
321290
DEBUG_BUILD && debug.error(e);
322291
});
323292
}
324293
return rv;
325294
}
326295

327-
// Only start a trace and root span if the handler is not already wrapped by Otel instrumentation
328-
// Otherwise, we create two root spans (one from otel, one from our wrapper).
329-
// If Otel instrumentation didn't work or was filtered by users, we still want to trace the handler.
330-
// TODO: Since bumping the OTEL Instrumentation, this is likely not needed anymore, we can possibly remove this (can be done whenever since it would be non-breaking)
331-
if (options.startTrace && !isWrappedByOtel(handler)) {
332-
const traceData = getAwsTraceData(event as { headers?: Record<string, string> }, context);
333-
334-
return continueTrace({ sentryTrace: traceData['sentry-trace'], baggage: traceData.baggage }, () => {
335-
return startSpanManual(
336-
{
337-
name: context.functionName,
338-
op: 'function.aws.lambda',
339-
attributes: {
340-
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'component',
341-
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.function.serverless',
342-
},
343-
},
344-
span => {
345-
enhanceScopeWithTransactionData(getCurrentScope(), context);
346-
347-
return processResult(span);
348-
},
349-
);
350-
});
351-
}
352-
353296
return withScope(async () => {
354-
return processResult(undefined);
297+
return processResult();
355298
});
356299
};
357300
}
358-
359-
/**
360-
* Checks if Otel's AWSLambda instrumentation successfully wrapped the handler.
361-
* Check taken from @opentelemetry/core
362-
*/
363-
function isWrappedByOtel(
364-
// eslint-disable-next-line @typescript-eslint/ban-types
365-
handler: Function & { __original?: unknown; __unwrap?: unknown; __wrapped?: boolean },
366-
): boolean {
367-
return (
368-
typeof handler === 'function' &&
369-
typeof handler.__original === 'function' &&
370-
typeof handler.__unwrap === 'function' &&
371-
handler.__wrapped === true
372-
);
373-
}

0 commit comments

Comments
 (0)