Skip to content

Commit a4c84aa

Browse files
committed
WIP 1
1 parent b59ce07 commit a4c84aa

File tree

2 files changed

+23
-49
lines changed

2 files changed

+23
-49
lines changed

packages/opentelemetry/src/trace.ts

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import type { Client, DynamicSamplingContext, Scope, Span as SentrySpan, TraceCo
1919
import { continueTraceAsRemoteSpan } from './propagator';
2020

2121
import type { OpenTelemetryClient, OpenTelemetrySpanContext } from './types';
22-
import { getContextFromScope, getScopesFromContext } from './utils/contextData';
22+
import { getContextFromScope } from './utils/contextData';
2323
import { getSamplingDecision } from './utils/getSamplingDecision';
2424
import { makeTraceState } from './utils/makeTraceState';
2525

@@ -179,39 +179,11 @@ function ensureTimestampInMilliseconds(timestamp: number): number {
179179

180180
function getContext(scope: Scope | undefined, forceTransaction: boolean | undefined): Context {
181181
const ctx = getContextForScope(scope);
182-
// Note: If the context is the ROOT_CONTEXT, no scope is attached
183-
// Thus we will not use the propagation context in this case, which is desired
184-
const actualScope = getScopesFromContext(ctx)?.scope;
185182
const parentSpan = trace.getSpan(ctx);
186183

187-
// In the case that we have no parent span, we need to "simulate" one to ensure the propagation context is correct
184+
// In the case that we have no parent span, we start a new trace
185+
// Note that if we continue a trace, we'll always have a remote parent span here anyhow
188186
if (!parentSpan) {
189-
const client = getClient();
190-
191-
if (actualScope && client) {
192-
const propagationContext = actualScope.getPropagationContext();
193-
194-
// We store the DSC as OTEL trace state on the span context
195-
const traceState = makeTraceState({
196-
parentSpanId: propagationContext.parentSpanId,
197-
// Not defined yet, we want to pick this up on-demand only
198-
dsc: undefined,
199-
sampled: propagationContext.sampled,
200-
});
201-
202-
const spanOptions: SpanContext = {
203-
traceId: propagationContext.traceId,
204-
spanId: propagationContext.parentSpanId || propagationContext.spanId,
205-
isRemote: true,
206-
traceFlags: propagationContext.sampled ? TraceFlags.SAMPLED : TraceFlags.NONE,
207-
traceState,
208-
};
209-
210-
// Add remote parent span context,
211-
return trace.setSpanContext(ctx, spanOptions);
212-
}
213-
214-
// if we have no scope or client, we just return the context as-is
215187
return ctx;
216188
}
217189

packages/opentelemetry/test/trace.test.ts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,7 @@ describe('trace', () => {
329329
it('allows to pass parentSpan=null', () => {
330330
startSpan({ name: 'GET users/[id' }, () => {
331331
startSpan({ name: 'child', parentSpan: null }, span => {
332-
// Due to the way we propagate the scope in OTEL,
333-
// the parent_span_id is not actually undefined here, but comes from the propagation context
334-
expect(spanToJSON(span).parent_span_id).toBe(getCurrentScope().getPropagationContext().spanId);
332+
expect(spanToJSON(span).parent_span_id).toBe(undefined);
335333
});
336334
});
337335
});
@@ -591,10 +589,7 @@ describe('trace', () => {
591589
it('allows to pass parentSpan=null', () => {
592590
startSpan({ name: 'outer' }, () => {
593591
const span = startInactiveSpan({ name: 'test span', parentSpan: null });
594-
595-
// Due to the way we propagate the scope in OTEL,
596-
// the parent_span_id is not actually undefined here, but comes from the propagation context
597-
expect(spanToJSON(span).parent_span_id).toBe(getCurrentScope().getPropagationContext().spanId);
592+
expect(spanToJSON(span).parent_span_id).toBe(undefined);
598593
span.end();
599594
});
600595
});
@@ -881,9 +876,7 @@ describe('trace', () => {
881876
it('allows to pass parentSpan=null', () => {
882877
startSpan({ name: 'outer' }, () => {
883878
startSpanManual({ name: 'GET users/[id]', parentSpan: null }, span => {
884-
// Due to the way we propagate the scope in OTEL,
885-
// the parent_span_id is not actually undefined here, but comes from the propagation context
886-
expect(spanToJSON(span).parent_span_id).toBe(getCurrentScope().getPropagationContext().spanId);
879+
expect(spanToJSON(span).parent_span_id).toBe(undefined);
887880
span.end();
888881
});
889882
});
@@ -1016,36 +1009,45 @@ describe('trace', () => {
10161009
});
10171010

10181011
describe('propagation', () => {
1019-
it('picks up the trace context from the scope, if there is no parent', () => {
1012+
it('starts new trace, if there is no parent', () => {
10201013
withScope(scope => {
10211014
const propagationContext = scope.getPropagationContext();
10221015
const span = startInactiveSpan({ name: 'test span' });
10231016

10241017
expect(span).toBeDefined();
1025-
expect(spanToJSON(span).trace_id).toEqual(propagationContext.traceId);
1026-
expect(spanToJSON(span).parent_span_id).toEqual(propagationContext.spanId);
1018+
const traceId = spanToJSON(span).trace_id;
1019+
expect(traceId).toMatch(/[a-f0-9]{32}/);
1020+
expect(spanToJSON(span).parent_span_id).toBe(undefined);
1021+
expect(spanToJSON(span).trace_id).not.toEqual(propagationContext.traceId);
10271022

10281023
expect(getDynamicSamplingContextFromSpan(span)).toEqual({
1029-
...getDynamicSamplingContextFromClient(propagationContext.traceId, getClient()!),
1024+
trace_id: traceId,
1025+
environment: 'production',
1026+
public_key: 'username',
10301027
sample_rate: '1',
10311028
sampled: 'true',
10321029
transaction: 'test span',
10331030
});
10341031
});
10351032
});
10361033

1037-
it('picks up the trace context from the scope, including parentSpanId, if there is no parent', () => {
1034+
// Note: This _should_ never happen, when we have an incoming trace, we should always have a parent span
1035+
it('starts new trace, ignoring parentSpanId, if there is no parent', () => {
10381036
withScope(scope => {
10391037
const propagationContext = scope.getPropagationContext();
10401038
propagationContext.parentSpanId = '1121201211212012';
10411039
const span = startInactiveSpan({ name: 'test span' });
10421040

10431041
expect(span).toBeDefined();
1044-
expect(spanToJSON(span).trace_id).toEqual(propagationContext.traceId);
1045-
expect(spanToJSON(span).parent_span_id).toEqual('1121201211212012');
1042+
const traceId = spanToJSON(span).trace_id;
1043+
expect(traceId).toMatch(/[a-f0-9]{32}/);
1044+
expect(spanToJSON(span).parent_span_id).toBe(undefined);
1045+
expect(spanToJSON(span).trace_id).not.toEqual(propagationContext.traceId);
10461046

10471047
expect(getDynamicSamplingContextFromSpan(span)).toEqual({
1048-
...getDynamicSamplingContextFromClient(propagationContext.traceId, getClient()!),
1048+
environment: 'production',
1049+
public_key: 'username',
1050+
trace_id: traceId,
10491051
sample_rate: '1',
10501052
sampled: 'true',
10511053
transaction: 'test span',

0 commit comments

Comments
 (0)