-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(otel): Ignore outgoing Sentry HTTP requests from otel integration (
- Loading branch information
Showing
4 changed files
with
154 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
packages/opentelemetry-node/src/utils/is-sentry-request.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Span as OtelSpan } from '@opentelemetry/sdk-trace-base'; | ||
import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; | ||
import { getCurrentHub } from '@sentry/core'; | ||
|
||
/** | ||
* | ||
* @param otelSpan Checks wheter a given OTEL Span is an http request to sentry. | ||
* @returns boolean | ||
*/ | ||
export function isSentryRequestSpan(otelSpan: OtelSpan): boolean { | ||
const { attributes } = otelSpan; | ||
|
||
const httpUrl = attributes[SemanticAttributes.HTTP_URL]; | ||
|
||
if (!httpUrl) { | ||
return false; | ||
} | ||
|
||
return isSentryRequestUrl(httpUrl.toString()); | ||
} | ||
|
||
/** | ||
* Checks whether given url points to Sentry server | ||
* @param url url to verify | ||
*/ | ||
function isSentryRequestUrl(url: string): boolean { | ||
const dsn = getCurrentHub().getClient()?.getDsn(); | ||
return dsn ? url.includes(dsn.host) : false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,12 +4,16 @@ import { Resource } from '@opentelemetry/resources'; | |
import { Span as OtelSpan } from '@opentelemetry/sdk-trace-base'; | ||
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'; | ||
import { SemanticAttributes, SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'; | ||
import { Hub, makeMain } from '@sentry/core'; | ||
import { createTransport, Hub, makeMain } from '@sentry/core'; | ||
import { NodeClient } from '@sentry/node'; | ||
import { addExtensionMethods, Span as SentrySpan, SpanStatusType, Transaction } from '@sentry/tracing'; | ||
import { Contexts, Scope } from '@sentry/types'; | ||
import { resolvedSyncPromise } from '@sentry/utils'; | ||
|
||
import { SENTRY_SPAN_PROCESSOR_MAP, SentrySpanProcessor } from '../src/spanprocessor'; | ||
|
||
const SENTRY_DSN = 'https://[email protected]/0'; | ||
|
||
// Integration Test of SentrySpanProcessor | ||
|
||
beforeAll(() => { | ||
|
@@ -22,7 +26,13 @@ describe('SentrySpanProcessor', () => { | |
let spanProcessor: SentrySpanProcessor; | ||
|
||
beforeEach(() => { | ||
hub = new Hub(); | ||
const client = new NodeClient({ | ||
dsn: SENTRY_DSN, | ||
integrations: [], | ||
transport: () => createTransport({ recordDroppedEvent: () => undefined }, _ => resolvedSyncPromise({})), | ||
stackParser: () => [], | ||
}); | ||
hub = new Hub(client); | ||
makeMain(hub); | ||
|
||
spanProcessor = new SentrySpanProcessor(); | ||
|
@@ -561,6 +571,106 @@ describe('SentrySpanProcessor', () => { | |
}); | ||
}); | ||
}); | ||
|
||
describe('skip sentry requests', () => { | ||
it('does not finish transaction for Sentry request', async () => { | ||
const otelSpan = provider.getTracer('default').startSpan('POST to sentry', { | ||
attributes: { | ||
[SemanticAttributes.HTTP_METHOD]: 'POST', | ||
[SemanticAttributes.HTTP_URL]: `${SENTRY_DSN}/sub/route`, | ||
}, | ||
}) as OtelSpan; | ||
|
||
const sentrySpanTransaction = getSpanForOtelSpan(otelSpan) as Transaction | undefined; | ||
expect(sentrySpanTransaction).toBeDefined(); | ||
|
||
otelSpan.end(); | ||
|
||
expect(sentrySpanTransaction?.endTimestamp).toBeUndefined(); | ||
|
||
// Ensure it is still removed from map! | ||
expect(getSpanForOtelSpan(otelSpan)).toBeUndefined(); | ||
}); | ||
|
||
it('finishes transaction for non-Sentry request', async () => { | ||
const otelSpan = provider.getTracer('default').startSpan('POST to sentry', { | ||
attributes: { | ||
[SemanticAttributes.HTTP_METHOD]: 'POST', | ||
[SemanticAttributes.HTTP_URL]: 'https://other.sentry.io/sub/route', | ||
}, | ||
}) as OtelSpan; | ||
|
||
const sentrySpanTransaction = getSpanForOtelSpan(otelSpan) as Transaction | undefined; | ||
expect(sentrySpanTransaction).toBeDefined(); | ||
|
||
otelSpan.end(); | ||
|
||
expect(sentrySpanTransaction?.endTimestamp).toBeDefined(); | ||
}); | ||
|
||
it('does not finish spans for Sentry request', async () => { | ||
const tracer = provider.getTracer('default'); | ||
|
||
tracer.startActiveSpan('GET /users', () => { | ||
tracer.startActiveSpan( | ||
'SELECT * FROM users;', | ||
{ | ||
attributes: { | ||
[SemanticAttributes.HTTP_METHOD]: 'POST', | ||
[SemanticAttributes.HTTP_URL]: `${SENTRY_DSN}/sub/route`, | ||
}, | ||
}, | ||
child => { | ||
const childOtelSpan = child as OtelSpan; | ||
|
||
const sentrySpan = getSpanForOtelSpan(childOtelSpan); | ||
expect(sentrySpan).toBeDefined(); | ||
|
||
childOtelSpan.end(); | ||
|
||
expect(sentrySpan?.endTimestamp).toBeUndefined(); | ||
|
||
// Ensure it is still removed from map! | ||
expect(getSpanForOtelSpan(childOtelSpan)).toBeUndefined(); | ||
}, | ||
); | ||
}); | ||
}); | ||
|
||
it('handles child spans of Sentry requests normally', async () => { | ||
const tracer = provider.getTracer('default'); | ||
|
||
tracer.startActiveSpan('GET /users', () => { | ||
tracer.startActiveSpan( | ||
'SELECT * FROM users;', | ||
{ | ||
attributes: { | ||
[SemanticAttributes.HTTP_METHOD]: 'POST', | ||
[SemanticAttributes.HTTP_URL]: `${SENTRY_DSN}/sub/route`, | ||
}, | ||
}, | ||
child => { | ||
const childOtelSpan = child as OtelSpan; | ||
|
||
const grandchildSpan = tracer.startSpan('child 1'); | ||
|
||
const sentrySpan = getSpanForOtelSpan(childOtelSpan); | ||
expect(sentrySpan).toBeDefined(); | ||
|
||
const sentryGrandchildSpan = getSpanForOtelSpan(grandchildSpan); | ||
expect(sentryGrandchildSpan).toBeDefined(); | ||
|
||
grandchildSpan.end(); | ||
|
||
childOtelSpan.end(); | ||
|
||
expect(sentryGrandchildSpan?.endTimestamp).toBeDefined(); | ||
expect(sentrySpan?.endTimestamp).toBeUndefined(); | ||
}, | ||
); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
// OTEL expects a custom date format | ||
|