Skip to content

Commit

Permalink
simplify otel implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Lms24 committed Aug 9, 2024
1 parent 3ce020d commit 35cb4c3
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const Sentry = require('@sentry/node');
Sentry.init({
dsn: 'https://[email protected]/1337',
transport: loggingTransport,
debug: true,
});

// express must be required after Sentry is initialized
Expand All @@ -30,5 +29,4 @@ app.get('/test', (_req, res) => {

Sentry.setupExpressErrorHandler(app);

// TODO: remove port again
startExpressServerAndSendPortToRunner(app, 3000);
startExpressServerAndSendPortToRunner(app);
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ describe('getTraceMetaTags', () => {
// @ts-ignore - response is defined, types just don't reflect it
const html = response?.response as unknown as string;

console.log(html);

const [_, traceId, spanId] = html.match(/<meta name="sentry-trace" content="([a-f0-9]{32})-([a-f0-9]{16})"\/>/) || [
const [, traceId, spanId] = html.match(/<meta name="sentry-trace" content="([a-f0-9]{32})-([a-f0-9]{16})"\/>/) || [
undefined,
undefined,
undefined,
Expand Down
24 changes: 7 additions & 17 deletions packages/opentelemetry/src/utils/getTraceData.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,29 @@
import * as api from '@opentelemetry/api';
import { getCurrentScope } from '@sentry/core';
import type { SerializedTraceData } from '@sentry/types';
import { getPropagationContextFromSpan } from '../propagator';
import { generateSpanContextForPropagationContext } from './generateSpanContextForPropagationContext';
import { dropUndefinedKeys } from '@sentry/utils';

/**
* Otel-specific implementation of `getTraceData`.
* @see `@sentry/core` version of `getTraceData` for more information
*/
export function getTraceData(): SerializedTraceData {
const ctx = api.context.active();
const spanToUse = api.trace.getSpan(ctx);
const context = api.context.active();

// This should never happen, given we always create an ambient non-recording span if there's no active span.
if (!spanToUse) {
if (!context) {
return {};
}
const headersObject: Record<string, string> = {};

const propagationContext = spanToUse
? getPropagationContextFromSpan(spanToUse)
: getCurrentScope().getPropagationContext();

const spanContext = generateSpanContextForPropagationContext(propagationContext);

const context = api.trace.setSpanContext(ctx, spanContext);
const headersObject: Record<string, string> = {};

api.propagation.inject(context, headersObject);

if (!headersObject['sentry-trace']) {
return {};
}

return {
return dropUndefinedKeys({
'sentry-trace': headersObject['sentry-trace'],
baggage: headersObject['baggage'],
};
baggage: headersObject.baggage,
});
}

0 comments on commit 35cb4c3

Please sign in to comment.