Skip to content

Commit a1e3373

Browse files
srjames90dyladan
andcommitted
Move SpanContext isValid to the API (open-telemetry#1447)
* refactor: make spanContext into class with isValid * refactor: use class instantiation for spanContext * refactor: moves test and fix tests * fix: add check for isValid * revert: the changes to spanContext * refactor: move spancontext-utils to api package * fix: lint and invalid psan id and invalid trace id * fix: make isValid more robust * refactor: rename isValid to isSpanContextValid * refactor: update regex and checks * fix: export isSpanContextValid from api.trace * fix: run npm run lint:fix * fix: run lint fix in api * refactor: prevent function overhead * fix: lint * fix: remove unused import Co-authored-by: Daniel Dyla <[email protected]>
1 parent 3b22223 commit a1e3373

File tree

6 files changed

+116
-13
lines changed

6 files changed

+116
-13
lines changed

api/src/api/trace.ts

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { NOOP_TRACER_PROVIDER } from '../trace/NoopTracerProvider';
1818
import { ProxyTracerProvider } from '../trace/ProxyTracerProvider';
1919
import { Tracer } from '../trace/tracer';
2020
import { TracerProvider } from '../trace/tracer_provider';
21+
import { isSpanContextValid } from '../trace/spancontext-utils';
2122
import {
2223
API_BACKWARDS_COMPATIBILITY_VERSION,
2324
GLOBAL_TRACE_API_KEY,
@@ -87,4 +88,6 @@ export class TraceAPI {
8788
delete _global[GLOBAL_TRACE_API_KEY];
8889
this._proxyTracerProvider = new ProxyTracerProvider();
8990
}
91+
92+
public isSpanContextValid = isSpanContextValid;
9093
}

api/src/index.ts

+6
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ export * from './trace/trace_state';
5555
export * from './trace/tracer_provider';
5656
export * from './trace/tracer';
5757

58+
export {
59+
INVALID_SPANID,
60+
INVALID_TRACEID,
61+
INVALID_SPAN_CONTEXT,
62+
} from './trace/spancontext-utils';
63+
5864
export { Context } from '@opentelemetry/context-base';
5965

6066
import { ContextAPI } from './api/context';

api/src/trace/NoopSpan.ts

+1-9
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,7 @@ import { Attributes } from './attributes';
2020
import { Span } from './span';
2121
import { SpanContext } from './span_context';
2222
import { Status } from './status';
23-
import { TraceFlags } from './trace_flags';
24-
25-
export const INVALID_TRACE_ID = '0';
26-
export const INVALID_SPAN_ID = '0';
27-
const INVALID_SPAN_CONTEXT: SpanContext = {
28-
traceId: INVALID_TRACE_ID,
29-
spanId: INVALID_SPAN_ID,
30-
traceFlags: TraceFlags.NONE,
31-
};
23+
import { INVALID_SPAN_CONTEXT } from './spancontext-utils';
3224

3325
/**
3426
* The NoopSpan is the default {@link Span} that is used when no Span

api/src/trace/spancontext-utils.ts

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright The 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+
import { SpanContext } from './span_context';
17+
import { TraceFlags } from './trace_flags';
18+
19+
const VALID_TRACEID_REGEX = /^([0-9a-f]{32})$/i;
20+
const VALID_SPANID_REGEX = /^[0-9a-f]{16}$/i;
21+
export const INVALID_SPANID = '0000000000000000';
22+
export const INVALID_TRACEID = '00000000000000000000000000000000';
23+
export const INVALID_SPAN_CONTEXT: SpanContext = {
24+
traceId: INVALID_TRACEID,
25+
spanId: INVALID_SPANID,
26+
traceFlags: TraceFlags.NONE,
27+
};
28+
29+
export function isValidTraceId(traceId: string): boolean {
30+
return VALID_TRACEID_REGEX.test(traceId) && traceId !== INVALID_TRACEID;
31+
}
32+
33+
export function isValidSpanId(spanId: string): boolean {
34+
return VALID_SPANID_REGEX.test(spanId) && spanId !== INVALID_SPANID;
35+
}
36+
37+
/**
38+
* Returns true if this {@link SpanContext} is valid.
39+
* @return true if this {@link SpanContext} is valid.
40+
*/
41+
export function isSpanContextValid(spanContext: SpanContext): boolean {
42+
return (
43+
isValidTraceId(spanContext.traceId) && isValidSpanId(spanContext.spanId)
44+
);
45+
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
import * as assert from 'assert';
1818
import {
1919
CanonicalCode,
20-
INVALID_SPAN_ID,
21-
INVALID_TRACE_ID,
20+
INVALID_SPANID,
21+
INVALID_TRACEID,
2222
NoopSpan,
2323
TraceFlags,
2424
} from '../../src';
@@ -45,8 +45,8 @@ describe('NoopSpan', () => {
4545

4646
assert.ok(!span.isRecording());
4747
assert.deepStrictEqual(span.context(), {
48-
traceId: INVALID_TRACE_ID,
49-
spanId: INVALID_SPAN_ID,
48+
traceId: INVALID_TRACEID,
49+
spanId: INVALID_SPANID,
5050
traceFlags: TraceFlags.NONE,
5151
});
5252
span.end();
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright The 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 * as assert from 'assert';
18+
import * as context from '../../src/trace/spancontext-utils';
19+
import { TraceFlags } from '../../src';
20+
21+
describe('spancontext-utils', () => {
22+
it('should return true for valid spancontext', () => {
23+
const spanContext = {
24+
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
25+
spanId: '6e0c63257de34c92',
26+
traceFlags: TraceFlags.NONE,
27+
};
28+
assert.ok(context.isSpanContextValid(spanContext));
29+
});
30+
31+
it('should return false when traceId is invalid', () => {
32+
const spanContext = {
33+
traceId: context.INVALID_TRACEID,
34+
spanId: '6e0c63257de34c92',
35+
traceFlags: TraceFlags.NONE,
36+
};
37+
assert.ok(!context.isSpanContextValid(spanContext));
38+
});
39+
40+
it('should return false when spanId is invalid', () => {
41+
const spanContext = {
42+
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
43+
spanId: context.INVALID_SPANID,
44+
traceFlags: TraceFlags.NONE,
45+
};
46+
assert.ok(!context.isSpanContextValid(spanContext));
47+
});
48+
49+
it('should return false when traceId & spanId is invalid', () => {
50+
const spanContext = {
51+
traceId: context.INVALID_TRACEID,
52+
spanId: context.INVALID_SPANID,
53+
traceFlags: TraceFlags.NONE,
54+
};
55+
assert.ok(!context.isSpanContextValid(spanContext));
56+
});
57+
});

0 commit comments

Comments
 (0)