Skip to content

Commit

Permalink
fix: change SpanContext.traceFlags to mandatory
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Flarna committed Feb 26, 2020
1 parent e2f971d commit 0d174bf
Show file tree
Hide file tree
Showing 28 changed files with 98 additions and 77 deletions.
1 change: 1 addition & 0 deletions packages/opentelemetry-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export * from './metrics/NoopMeterProvider';
export * from './trace/attributes';
export * from './trace/Event';
export * from './trace/instrumentation/Plugin';
export * from './trace/link_context';
export * from './trace/link';
export * from './trace/NoopSpan';
export * from './trace/NoopTracer';
Expand Down
5 changes: 0 additions & 5 deletions packages/opentelemetry-api/src/trace/NoopSpan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ export class NoopSpan implements Span {
return this;
}

// By default does nothing
addLink(spanContext: SpanContext, attributes?: Attributes): this {
return this;
}

// By default does nothing
setStatus(status: Status): this {
return this;
Expand Down
4 changes: 2 additions & 2 deletions packages/opentelemetry-api/src/trace/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import { Attributes } from './attributes';
import { SpanContext } from './span_context';
import { LinkContext } from './link_context';

/**
* A pointer from the current {@link Span} to another span in the same trace or
Expand All @@ -24,7 +24,7 @@ import { SpanContext } from './span_context';
*/
export interface Link {
/** The {@link SpanContext} of a linked span. */
spanContext: SpanContext;
linkContext: LinkContext;
/** A set of {@link Attributes} on the link. */
attributes?: Attributes;
}
34 changes: 34 additions & 0 deletions packages/opentelemetry-api/src/trace/link_context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*!
* Copyright 2020, OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* A pointer to another span.
*/
export interface LinkContext {
/**
* The ID of the trace that this span belongs to. It is worldwide unique
* with practically sufficient probability by being made as 16 randomly
* generated bytes, encoded as a 32 lowercase hex characters corresponding to
* 128 bits.
*/
traceId: string;
/**
* The ID of the Span. It is globally unique with practically sufficient
* probability by being made as 8 randomly generated bytes, encoded as a 16
* lowercase hex characters corresponding to 64 bits.
*/
spanId: string;
}
2 changes: 1 addition & 1 deletion packages/opentelemetry-api/src/trace/span_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export interface SpanContext {
*
* SAMPLED = 0x1 and UNSAMPLED = 0x0;
*/
traceFlags?: TraceFlags;
traceFlags: TraceFlags;
/**
* Tracing-system-specific info to propagate.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,6 @@ describe('NoopSpan', () => {
span.addEvent('sent');
span.addEvent('sent', { id: '42', key: 'value' });

span.addLink({
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
spanId: '6e0c63257de34c92',
});
span.addLink(
{
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
spanId: '6e0c63257de34c92',
},
{ id: '42', key: 'value' }
);

span.setStatus({ code: CanonicalCode.CANCELLED });

span.updateName('my-span');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { Context } from '@opentelemetry/scope-base';

describe('NoopTracer', () => {
it('should not crash', () => {
const spanContext = { traceId: '', spanId: '' };
const spanContext = { traceId: '', spanId: '', traceFlags: 0 };
const tracer = new NoopTracer();

assert.deepStrictEqual(tracer.startSpan('span-name'), NOOP_SPAN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class BinaryTraceContext implements BinaryFormat {
}

fromBytes(buf: Uint8Array): SpanContext | null {
const result: SpanContext = { traceId: '', spanId: '' };
const result: SpanContext = { traceId: '', spanId: '', traceFlags: 0 };
// Length must be 29.
if (buf.length !== FORMAT_LENGTH) return null;
// Check version and field numbers.
Expand Down
19 changes: 1 addition & 18 deletions packages/opentelemetry-core/test/context/B3Format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ describe('B3Format', () => {
const emptySpanContext = {
traceId: '',
spanId: '',
traceFlags: 0,
};
b3Format.inject(
setExtractedSpanContext(Context.ROOT_CONTEXT, emptySpanContext),
Expand All @@ -90,24 +91,6 @@ describe('B3Format', () => {
assert.deepStrictEqual(carrier[X_B3_TRACE_ID], undefined);
assert.deepStrictEqual(carrier[X_B3_SPAN_ID], undefined);
});

it('should handle absence of sampling decision', () => {
const spanContext: SpanContext = {
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
spanId: '6e0c63257de34c92',
};

b3Format.inject(
setExtractedSpanContext(Context.ROOT_CONTEXT, spanContext),
carrier
);
assert.deepStrictEqual(
carrier[X_B3_TRACE_ID],
'd4cda95b652f4a1592b449d5929fda1b'
);
assert.deepStrictEqual(carrier[X_B3_SPAN_ID], '6e0c63257de34c92');
assert.deepStrictEqual(carrier[X_B3_SAMPLED], undefined);
});
});

describe('.extract()', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ describe('BinaryTraceContext', () => {
description: 'span context with 64-bit span ID',
},
{
structured: { traceId: commonTraceId, spanId: commonSpanId },
structured: {
traceId: commonTraceId,
spanId: commonSpanId,
traceFlags: 0,
},
binary: new Uint8Array([
0,
0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,7 @@ import {
describe('ProbabilitySampler', () => {
it('should return a always sampler for 1', () => {
const sampler = new ProbabilitySampler(1);
assert.strictEqual(
sampler.shouldSample({
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
spanId: '6e0c63257de34c92',
}),
true
);
assert.strictEqual(sampler.shouldSample(), true);
});

it('should return a always sampler for >1', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe('spancontext-utils', () => {
const spanContext = {
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
spanId: '6e0c63257de34c92',
traceFlags: 0,
};
assert.ok(context.isValid(spanContext));
});
Expand All @@ -30,6 +31,7 @@ describe('spancontext-utils', () => {
const spanContext = {
traceId: context.INVALID_TRACEID,
spanId: '6e0c63257de34c92',
traceFlags: 0,
};
assert.ok(!context.isValid(spanContext));
});
Expand All @@ -38,6 +40,7 @@ describe('spancontext-utils', () => {
const spanContext = {
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
spanId: context.INVALID_SPANID,
traceFlags: 0,
};
assert.ok(!context.isValid(spanContext));
});
Expand All @@ -46,6 +49,7 @@ describe('spancontext-utils', () => {
const spanContext = {
traceId: context.INVALID_TRACEID,
spanId: context.INVALID_SPANID,
traceFlags: 0,
};
assert.ok(!context.isValid(spanContext));
});
Expand Down
8 changes: 4 additions & 4 deletions packages/opentelemetry-exporter-collector/src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ export function toCollectorLinkType(
span: ReadableSpan,
link: Link
): collectorTypes.LinkType {
const linkSpanId = link.spanContext.spanId;
const linkTraceId = link.spanContext.traceId;
const linkSpanId = link.linkContext.spanId;
const linkTraceId = link.linkContext.traceId;
const spanParentId = span.parentSpanId;
const spanTraceId = span.spanContext.traceId;

Expand All @@ -158,8 +158,8 @@ export function toCollectorLinkType(
export function toCollectorLinks(span: ReadableSpan): collectorTypes.Links {
const collectorLinks: collectorTypes.Link[] = span.links.map((link: Link) => {
const collectorLink: collectorTypes.Link = {
traceId: hexToBase64(link.spanContext.traceId),
spanId: hexToBase64(link.spanContext.spanId),
traceId: hexToBase64(link.linkContext.traceId),
spanId: hexToBase64(link.linkContext.spanId),
type: toCollectorLinkType(span, link),
};

Expand Down
3 changes: 1 addition & 2 deletions packages/opentelemetry-exporter-collector/test/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ export const mockedReadableSpan: ReadableSpan = {
attributes: { component: 'document-load' },
links: [
{
spanContext: {
linkContext: {
traceId: '1f1008dc8e270e85c40a0d7c3939b278',
spanId: '78a8915098864388',
traceFlags: 1,
},
attributes: { component: 'document-load' },
},
Expand Down
6 changes: 3 additions & 3 deletions packages/opentelemetry-exporter-jaeger/src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ function spanLinksToThriftRefs(
): ThriftReference[] {
return links
.map((link): ThriftReference | null => {
if (link.spanContext.spanId === parentSpanId) {
if (link.linkContext.spanId === parentSpanId) {
const refType = ThriftReferenceType.CHILD_OF;
const traceId = link.spanContext.traceId;
const traceId = link.linkContext.traceId;
const traceIdHigh = Utils.encodeInt64(traceId.slice(0, 16));
const traceIdLow = Utils.encodeInt64(traceId.slice(16));
const spanId = Utils.encodeInt64(link.spanContext.spanId);
const spanId = Utils.encodeInt64(link.linkContext.spanId);
return { traceIdLow, traceIdHigh, spanId, refType };
}
return null;
Expand Down
1 change: 1 addition & 0 deletions packages/opentelemetry-exporter-jaeger/test/jaeger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ describe('JaegerExporter', () => {
const spanContext = {
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
spanId: '6e0c63257de34c92',
traceFlags: 0,
};
const readableSpan: ReadableSpan = {
name: 'my-span1',
Expand Down
6 changes: 4 additions & 2 deletions packages/opentelemetry-exporter-jaeger/test/transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe('transform', () => {
const spanContext = {
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
spanId: '6e0c63257de34c92',
traceFlags: 0,
};

describe('spanToThrift', () => {
Expand All @@ -45,7 +46,7 @@ describe('transform', () => {
},
links: [
{
spanContext: {
linkContext: {
traceId: 'a4cda95b652f4a1592b449d5929fda1b',
spanId: '3e0c63257de34c92',
},
Expand Down Expand Up @@ -194,7 +195,7 @@ describe('transform', () => {
parentSpanId: '3e0c63257de34c92',
links: [
{
spanContext: {
linkContext: {
traceId: 'a4cda95b652f4a1592b449d5929fda1b',
spanId: '3e0c63257de34c92',
},
Expand Down Expand Up @@ -227,6 +228,7 @@ describe('transform', () => {
spanContext: {
traceId: '92b449d5929fda1b',
spanId: '6e0c63257de34c92',
traceFlags: 0,
},
startTime: [1566156729, 709],
endTime: [1566156731, 709],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ export function getReadableSpanTransformer(
function transformLink(link: ot.Link): Link {
return {
attributes: transformAttributes(link.attributes),
spanId: link.spanContext.spanId,
traceId: link.spanContext.traceId,
spanId: link.linkContext.spanId,
traceId: link.linkContext.traceId,
type: LinkType.UNSPECIFIED,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ describe('Stackdriver Trace Exporter', () => {
spanContext: {
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
spanId: '6e0c63257de34c92',
traceFlags: 0,
isRemote: true,
},
status: { code: types.CanonicalCode.OK },
Expand Down Expand Up @@ -147,6 +148,7 @@ describe('Stackdriver Trace Exporter', () => {
spanContext: {
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
spanId: '6e0c63257de34c92',
traceFlags: 0,
isRemote: true,
},
status: { code: types.CanonicalCode.OK },
Expand Down Expand Up @@ -178,6 +180,7 @@ describe('Stackdriver Trace Exporter', () => {
spanContext: {
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
spanId: '6e0c63257de34c92',
traceFlags: 0,
isRemote: true,
},
status: { code: types.CanonicalCode.OK },
Expand Down Expand Up @@ -207,6 +210,7 @@ describe('Stackdriver Trace Exporter', () => {
spanContext: {
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
spanId: '6e0c63257de34c92',
traceFlags: 0,
isRemote: true,
},
status: { code: types.CanonicalCode.OK },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe('transform', () => {
spanContext = {
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
spanId: '6e0c63257de34c92',
traceFlags: 0,
isRemote: true,
};

Expand Down Expand Up @@ -131,11 +132,9 @@ describe('transform', () => {

it('should transform links', () => {
readableSpan.links.push({
spanContext: {
linkContext: {
traceId: 'a4cda95b652f4a1592b449d5929fda1b',
spanId: '3e0c63257de34c92',
isRemote: true,
traceFlags: types.TraceFlags.SAMPLED,
},
});

Expand All @@ -158,11 +157,9 @@ describe('transform', () => {

it('should transform links with attributes', () => {
readableSpan.links.push({
spanContext: {
linkContext: {
traceId: 'a4cda95b652f4a1592b449d5929fda1b',
spanId: '3e0c63257de34c92',
isRemote: true,
traceFlags: types.TraceFlags.SAMPLED,
},
attributes: {
testAttr: 'value',
Expand Down
Loading

0 comments on commit 0d174bf

Please sign in to comment.