Skip to content

Commit

Permalink
Move SpanContext isValid to the API (open-telemetry#1447)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
srjames90 and dyladan committed Feb 18, 2021
1 parent 525e752 commit 0ea65d3
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 13 deletions.
3 changes: 3 additions & 0 deletions api/src/api/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { NOOP_TRACER_PROVIDER } from '../trace/NoopTracerProvider';
import { ProxyTracerProvider } from '../trace/ProxyTracerProvider';
import { Tracer } from '../trace/tracer';
import { TracerProvider } from '../trace/tracer_provider';
import { isSpanContextValid } from '../trace/spancontext-utils';
import {
API_BACKWARDS_COMPATIBILITY_VERSION,
GLOBAL_TRACE_API_KEY,
Expand Down Expand Up @@ -87,4 +88,6 @@ export class TraceAPI {
delete _global[GLOBAL_TRACE_API_KEY];
this._proxyTracerProvider = new ProxyTracerProvider();
}

public isSpanContextValid = isSpanContextValid;
}
6 changes: 6 additions & 0 deletions api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ export * from './trace/trace_state';
export * from './trace/tracer_provider';
export * from './trace/tracer';

export {
INVALID_SPANID,
INVALID_TRACEID,
INVALID_SPAN_CONTEXT,
} from './trace/spancontext-utils';

export { Context } from '@opentelemetry/context-base';

import { ContextAPI } from './api/context';
Expand Down
10 changes: 1 addition & 9 deletions api/src/trace/NoopSpan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,7 @@ import { Attributes } from './attributes';
import { Span } from './span';
import { SpanContext } from './span_context';
import { Status } from './status';
import { TraceFlags } from './trace_flags';

export const INVALID_TRACE_ID = '0';
export const INVALID_SPAN_ID = '0';
const INVALID_SPAN_CONTEXT: SpanContext = {
traceId: INVALID_TRACE_ID,
spanId: INVALID_SPAN_ID,
traceFlags: TraceFlags.NONE,
};
import { INVALID_SPAN_CONTEXT } from './spancontext-utils';

/**
* The NoopSpan is the default {@link Span} that is used when no Span
Expand Down
45 changes: 45 additions & 0 deletions api/src/trace/spancontext-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright The 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.
*/
import { SpanContext } from './span_context';
import { TraceFlags } from './trace_flags';

const VALID_TRACEID_REGEX = /^([0-9a-f]{32})$/i;
const VALID_SPANID_REGEX = /^[0-9a-f]{16}$/i;
export const INVALID_SPANID = '0000000000000000';
export const INVALID_TRACEID = '00000000000000000000000000000000';
export const INVALID_SPAN_CONTEXT: SpanContext = {
traceId: INVALID_TRACEID,
spanId: INVALID_SPANID,
traceFlags: TraceFlags.NONE,
};

export function isValidTraceId(traceId: string): boolean {
return VALID_TRACEID_REGEX.test(traceId) && traceId !== INVALID_TRACEID;
}

export function isValidSpanId(spanId: string): boolean {
return VALID_SPANID_REGEX.test(spanId) && spanId !== INVALID_SPANID;
}

/**
* Returns true if this {@link SpanContext} is valid.
* @return true if this {@link SpanContext} is valid.
*/
export function isSpanContextValid(spanContext: SpanContext): boolean {
return (
isValidTraceId(spanContext.traceId) && isValidSpanId(spanContext.spanId)
);
}
8 changes: 4 additions & 4 deletions api/test/noop-implementations/noop-span.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import * as assert from 'assert';
import {
CanonicalCode,
INVALID_SPAN_ID,
INVALID_TRACE_ID,
INVALID_SPANID,
INVALID_TRACEID,
NoopSpan,
TraceFlags,
} from '../../src';
Expand All @@ -45,8 +45,8 @@ describe('NoopSpan', () => {

assert.ok(!span.isRecording());
assert.deepStrictEqual(span.context(), {
traceId: INVALID_TRACE_ID,
spanId: INVALID_SPAN_ID,
traceId: INVALID_TRACEID,
spanId: INVALID_SPANID,
traceFlags: TraceFlags.NONE,
});
span.end();
Expand Down
57 changes: 57 additions & 0 deletions api/test/trace/spancontext-utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright The 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.
*/

import * as assert from 'assert';
import * as context from '../../src/trace/spancontext-utils';
import { TraceFlags } from '../../src';

describe('spancontext-utils', () => {
it('should return true for valid spancontext', () => {
const spanContext = {
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
spanId: '6e0c63257de34c92',
traceFlags: TraceFlags.NONE,
};
assert.ok(context.isSpanContextValid(spanContext));
});

it('should return false when traceId is invalid', () => {
const spanContext = {
traceId: context.INVALID_TRACEID,
spanId: '6e0c63257de34c92',
traceFlags: TraceFlags.NONE,
};
assert.ok(!context.isSpanContextValid(spanContext));
});

it('should return false when spanId is invalid', () => {
const spanContext = {
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
spanId: context.INVALID_SPANID,
traceFlags: TraceFlags.NONE,
};
assert.ok(!context.isSpanContextValid(spanContext));
});

it('should return false when traceId & spanId is invalid', () => {
const spanContext = {
traceId: context.INVALID_TRACEID,
spanId: context.INVALID_SPANID,
traceFlags: TraceFlags.NONE,
};
assert.ok(!context.isSpanContextValid(spanContext));
});
});

0 comments on commit 0ea65d3

Please sign in to comment.