Skip to content

Commit fef6d3f

Browse files
committed
Adds getOtlpTracesEndpoint() helper
1 parent 4fea22d commit fef6d3f

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

packages/node-core/src/light/integrations/otlpIntegration.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { trace } from '@opentelemetry/api';
22
import type { Client, IntegrationFn } from '@sentry/core';
3-
import { debug, defineIntegration, registerExternalPropagationContext } from '@sentry/core';
3+
import {
4+
debug,
5+
defineIntegration,
6+
dsnFromString,
7+
registerExternalPropagationContext,
8+
SENTRY_API_VERSION,
9+
} from '@sentry/core';
410

511
const INTEGRATION_NAME = 'OtlpIntegration';
612

@@ -32,3 +38,25 @@ const _otlpIntegration = (() => {
3238
* error/log events to the active OTel trace context.
3339
*/
3440
export const otlpIntegration = defineIntegration(_otlpIntegration);
41+
42+
/**
43+
* Returns the OTLP traces endpoint URL and auth headers for a given Sentry DSN.
44+
* Use this to configure your own `OTLPTraceExporter`.
45+
*/
46+
export function getOtlpTracesEndpoint(dsn: string): { url: string; headers: Record<string, string> } | undefined {
47+
const parsed = dsnFromString(dsn);
48+
if (!parsed) {
49+
return undefined;
50+
}
51+
52+
const { protocol, host, port, path, projectId, publicKey } = parsed;
53+
const basePath = path ? `/${path}` : '';
54+
const portStr = port ? `:${port}` : '';
55+
56+
return {
57+
url: `${protocol}://${host}${portStr}${basePath}/api/${projectId}/integration/otlp/v1/traces/`,
58+
headers: {
59+
'X-Sentry-Auth': `Sentry sentry_version=${SENTRY_API_VERSION}, sentry_key=${publicKey}`,
60+
},
61+
};
62+
}

packages/node-core/test/light/integrations/otlpIntegration.test.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { hasExternalPropagationContext, registerExternalPropagationContext } from '@sentry/core';
22
import { afterEach, describe, expect, it } from 'vitest';
3-
import { otlpIntegration } from '../../../src/light/integrations/otlpIntegration';
3+
import { getOtlpTracesEndpoint, otlpIntegration } from '../../../src/light/integrations/otlpIntegration';
44
import { cleanupLightSdk, mockLightSdkInit } from '../../helpers/mockLightSdkInit';
55

66
describe('Light Mode | otlpIntegration', () => {
@@ -23,3 +23,32 @@ describe('Light Mode | otlpIntegration', () => {
2323
expect(hasExternalPropagationContext()).toBe(true);
2424
});
2525
});
26+
27+
describe('getOtlpTracesEndpoint', () => {
28+
it('returns correct endpoint and headers from DSN', () => {
29+
const result = getOtlpTracesEndpoint('https://abc123@o0.ingest.sentry.io/456');
30+
31+
expect(result).toEqual({
32+
url: 'https://o0.ingest.sentry.io/api/456/integration/otlp/v1/traces/',
33+
headers: {
34+
'X-Sentry-Auth': 'Sentry sentry_version=7, sentry_key=abc123',
35+
},
36+
});
37+
});
38+
39+
it('handles DSN with port and path', () => {
40+
const result = getOtlpTracesEndpoint('https://key@sentry.example.com:9000/mypath/789');
41+
42+
expect(result).toEqual({
43+
url: 'https://sentry.example.com:9000/mypath/api/789/integration/otlp/v1/traces/',
44+
headers: {
45+
'X-Sentry-Auth': 'Sentry sentry_version=7, sentry_key=key',
46+
},
47+
});
48+
});
49+
50+
it('returns undefined for invalid DSN', () => {
51+
const result = getOtlpTracesEndpoint('not-a-dsn');
52+
expect(result).toBeUndefined();
53+
});
54+
});

0 commit comments

Comments
 (0)