From 15230f1aa048e0118d5aca1ba5838cdacea44233 Mon Sep 17 00:00:00 2001 From: Luca Forstner Date: Fri, 5 Jul 2024 11:29:11 +0200 Subject: [PATCH] feat(opentelemetry): Allow skipping of span data inference (#12779) --- .../src/utils/parseSpanDescription.ts | 15 ++++++++++++- .../test/utils/parseSpanDescription.test.ts | 21 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/packages/opentelemetry/src/utils/parseSpanDescription.ts b/packages/opentelemetry/src/utils/parseSpanDescription.ts index f77478ee3b8e..c9e732683c38 100644 --- a/packages/opentelemetry/src/utils/parseSpanDescription.ts +++ b/packages/opentelemetry/src/utils/parseSpanDescription.ts @@ -23,7 +23,7 @@ interface SpanDescription { op: string | undefined; description: string; source: TransactionSource; - data?: Record; + data?: Record; } /** @@ -35,6 +35,19 @@ export function parseSpanDescription(span: AbstractSpan): SpanDescription { const attributes = spanHasAttributes(span) ? span.attributes : {}; const name = spanHasName(span) ? span.name : ''; + // This attribute is intentionally exported as a SEMATTR constant because it should stay intimite API + if (attributes['sentry.skip_span_data_inference']) { + return { + op: undefined, + description: name, + source: 'custom', + data: { + // Suggest to callers of `parseSpanDescription` to wipe the hint because it is unnecessary data in the end. + 'sentry.skip_span_data_inference': undefined, + }, + }; + } + // if http.method exists, this is an http request span // // TODO: Referencing `http.request.method` is a temporary workaround until the semantic diff --git a/packages/opentelemetry/test/utils/parseSpanDescription.test.ts b/packages/opentelemetry/test/utils/parseSpanDescription.test.ts index 1e7178871ff1..cfa1a43094c4 100644 --- a/packages/opentelemetry/test/utils/parseSpanDescription.test.ts +++ b/packages/opentelemetry/test/utils/parseSpanDescription.test.ts @@ -132,6 +132,27 @@ describe('parseSpanDescription', () => { source: 'route', }, ], + [ + "should not do any data parsing when the 'sentry.skip_span_data_inference' attribute is set", + { + 'sentry.skip_span_data_inference': true, + + // All of these should be ignored + [SEMATTRS_HTTP_METHOD]: 'GET', + [SEMATTRS_DB_SYSTEM]: 'mysql', + [SEMATTRS_DB_STATEMENT]: 'SELECT * from users', + }, + 'test name', + undefined, + { + op: undefined, + description: 'test name', + source: 'custom', + data: { + 'sentry.skip_span_data_inference': undefined, + }, + }, + ], ])('%s', (_, attributes, name, kind, expected) => { const actual = parseSpanDescription({ attributes, kind, name } as unknown as Span); expect(actual).toEqual(expected);