forked from open-telemetry/opentelemetry-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
- Loading branch information
Showing
6 changed files
with
116 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); | ||
}); |