Skip to content

Commit 3c41f56

Browse files
Flarnadyladan
andauthored
fix: change SpanContext.traceFlags to mandatory (open-telemetry#818)
* fix: change SpanContext.traceFlags to mandatory According to spec SpanContext represents the W3C tracestate which includes traceId, spanId and traceFlags. As a side effect a new LinkContext types was added as links don't have traceFlags according to spec. * chore: review findings, rename TraceFlags.UNSAMPLED to NONE * fix: build * fix: tests * fix: correct merge Co-authored-by: Daniel Dyla <dyladan@users.noreply.github.com>
1 parent 5c4c57e commit 3c41f56

35 files changed

Lines changed: 114 additions & 104 deletions

File tree

packages/opentelemetry-api/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export * from './metrics/ObserverResult';
3131
export * from './trace/attributes';
3232
export * from './trace/Event';
3333
export * from './trace/instrumentation/Plugin';
34+
export * from './trace/link_context';
3435
export * from './trace/link';
3536
export * from './trace/NoopSpan';
3637
export * from './trace/NoopTracer';

packages/opentelemetry-api/src/trace/NoopSpan.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export const INVALID_SPAN_ID = '0';
2626
const INVALID_SPAN_CONTEXT: SpanContext = {
2727
traceId: INVALID_TRACE_ID,
2828
spanId: INVALID_SPAN_ID,
29-
traceFlags: TraceFlags.UNSAMPLED,
29+
traceFlags: TraceFlags.NONE,
3030
};
3131

3232
/**
@@ -59,11 +59,6 @@ export class NoopSpan implements Span {
5959
return this;
6060
}
6161

62-
// By default does nothing
63-
addLink(spanContext: SpanContext, attributes?: Attributes): this {
64-
return this;
65-
}
66-
6762
// By default does nothing
6863
setStatus(status: Status): this {
6964
return this;

packages/opentelemetry-api/src/trace/link.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@
1515
*/
1616

1717
import { Attributes } from './attributes';
18-
import { SpanContext } from './span_context';
18+
import { LinkContext } from './link_context';
1919

2020
/**
2121
* A pointer from the current {@link Span} to another span in the same trace or
2222
* in a different trace. Used (for example) in batching operations, where a
2323
* single batch handler processes multiple requests from different traces.
2424
*/
2525
export interface Link {
26-
/** The {@link SpanContext} of a linked span. */
27-
spanContext: SpanContext;
26+
/** The {@link LinkContext} of a linked span. */
27+
context: LinkContext;
2828
/** A set of {@link Attributes} on the link. */
2929
attributes?: Attributes;
3030
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*!
2+
* Copyright 2020, OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { SpanContext } from './span_context';
18+
19+
/**
20+
* A pointer to another span.
21+
*/
22+
export type LinkContext = Pick<SpanContext, 'traceId' | 'spanId'>;

packages/opentelemetry-api/src/trace/span_context.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ export interface SpanContext {
4747
* caller may have recorded trace data. A caller who does not record trace
4848
* data out-of-band leaves this flag unset.
4949
*
50-
* SAMPLED = 0x1 and UNSAMPLED = 0x0;
50+
* SAMPLED = 0x1 and NONE = 0x0;
5151
*/
52-
traceFlags?: TraceFlags;
52+
traceFlags: TraceFlags;
5353
/**
5454
* Tracing-system-specific info to propagate.
5555
*

packages/opentelemetry-api/src/trace/trace_flags.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
* whether a Span should be traced. It is implemented as a bitmask.
2121
*/
2222
export enum TraceFlags {
23-
/** Bit to represent whether trace is unsampled in trace flags. */
24-
UNSAMPLED = 0x0,
23+
/** Represents no flag set. */
24+
NONE = 0x0,
2525
/** Bit to represent whether trace is sampled in trace flags. */
26-
SAMPLED = 0x1,
26+
SAMPLED = 0x1 << 0,
2727
}

packages/opentelemetry-api/test/api/api.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe('API', () => {
4242
const spanContext = {
4343
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
4444
spanId: '6e0c63257de34c92',
45-
traceFlags: TraceFlags.UNSAMPLED,
45+
traceFlags: TraceFlags.NONE,
4646
};
4747
const dummySpan = new NoopSpan(spanContext);
4848

packages/opentelemetry-api/test/noop-implementations/noop-span.test.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,6 @@ describe('NoopSpan', () => {
3939
span.addEvent('sent');
4040
span.addEvent('sent', { id: '42', key: 'value' });
4141

42-
span.addLink({
43-
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
44-
spanId: '6e0c63257de34c92',
45-
});
46-
span.addLink(
47-
{
48-
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
49-
spanId: '6e0c63257de34c92',
50-
},
51-
{ id: '42', key: 'value' }
52-
);
53-
5442
span.setStatus({ code: CanonicalCode.CANCELLED });
5543

5644
span.updateName('my-span');
@@ -59,7 +47,7 @@ describe('NoopSpan', () => {
5947
assert.deepStrictEqual(span.context(), {
6048
traceId: INVALID_TRACE_ID,
6149
spanId: INVALID_SPAN_ID,
62-
traceFlags: TraceFlags.UNSAMPLED,
50+
traceFlags: TraceFlags.NONE,
6351
});
6452
span.end();
6553
});

packages/opentelemetry-core/src/context/propagation/B3Format.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,7 @@ export class B3Format implements HttpTextFormat {
8080
traceId,
8181
spanId,
8282
isRemote: true,
83-
traceFlags: isNaN(Number(options))
84-
? TraceFlags.UNSAMPLED
85-
: Number(options),
83+
traceFlags: isNaN(Number(options)) ? TraceFlags.NONE : Number(options),
8684
});
8785
}
8886
return context;

packages/opentelemetry-core/src/context/propagation/HttpTraceContext.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export class HttpTraceContext implements HttpTextFormat {
7070

7171
const traceParent = `${VERSION}-${spanContext.traceId}-${
7272
spanContext.spanId
73-
}-0${Number(spanContext.traceFlags || TraceFlags.UNSAMPLED).toString(16)}`;
73+
}-0${Number(spanContext.traceFlags || TraceFlags.NONE).toString(16)}`;
7474

7575
setter(carrier, TRACE_PARENT_HEADER, traceParent);
7676
if (spanContext.traceState) {

0 commit comments

Comments
 (0)