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.
feat(api): propagate spanContext only using API open-telemetry#1456 (o…
…pen-telemetry#1527) Co-authored-by: Bartlomiej Obecny <[email protected]> Co-authored-by: Daniel Dyla <[email protected]>
- Loading branch information
1 parent
eaa27ea
commit ea5615f
Showing
5 changed files
with
270 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* 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 { Span, SpanContext } from '../'; | ||
import { Context, createContextKey } from '@opentelemetry/context-base'; | ||
|
||
/** | ||
* Active span key | ||
*/ | ||
const ACTIVE_SPAN_KEY = createContextKey( | ||
'OpenTelemetry Context Key ACTIVE_SPAN' | ||
); | ||
const EXTRACTED_SPAN_CONTEXT_KEY = createContextKey( | ||
'OpenTelemetry Context Key EXTRACTED_SPAN_CONTEXT' | ||
); | ||
/** | ||
* Shared key for indicating if instrumentation should be suppressed beyond | ||
* this current scope. | ||
*/ | ||
const SUPPRESS_INSTRUMENTATION_KEY = createContextKey( | ||
'OpenTelemetry Context Key SUPPRESS_INSTRUMENTATION' | ||
); | ||
|
||
/** | ||
* Return the active span if one exists | ||
* | ||
* @param context context to get span from | ||
*/ | ||
export function getActiveSpan(context: Context): Span | undefined { | ||
return (context.getValue(ACTIVE_SPAN_KEY) as Span) || undefined; | ||
} | ||
|
||
/** | ||
* Set the active span on a context | ||
* | ||
* @param context context to use as parent | ||
* @param span span to set active | ||
*/ | ||
export function setActiveSpan(context: Context, span: Span): Context { | ||
return context.setValue(ACTIVE_SPAN_KEY, span); | ||
} | ||
|
||
/** | ||
* Get the extracted span context from a context | ||
* | ||
* @param context context to get span context from | ||
*/ | ||
export function getExtractedSpanContext( | ||
context: Context | ||
): SpanContext | undefined { | ||
return ( | ||
(context.getValue(EXTRACTED_SPAN_CONTEXT_KEY) as SpanContext) || undefined | ||
); | ||
} | ||
|
||
/** | ||
* Set the extracted span context on a context | ||
* | ||
* @param context context to set span context on | ||
* @param spanContext span context to set | ||
*/ | ||
export function setExtractedSpanContext( | ||
context: Context, | ||
spanContext: SpanContext | ||
): Context { | ||
return context.setValue(EXTRACTED_SPAN_CONTEXT_KEY, spanContext); | ||
} | ||
|
||
/** | ||
* Get the span context of the parent span if it exists, | ||
* or the extracted span context if there is no active | ||
* span. | ||
* | ||
* @param context context to get values from | ||
*/ | ||
export function getParentSpanContext( | ||
context: Context | ||
): SpanContext | undefined { | ||
return getActiveSpan(context)?.context() || getExtractedSpanContext(context); | ||
} | ||
|
||
/** | ||
* Sets value on context to indicate that instrumentation should | ||
* be suppressed beyond this current scope. | ||
* | ||
* @param context context to set the suppress instrumentation value on. | ||
*/ | ||
export function suppressInstrumentation(context: Context): Context { | ||
return context.setValue(SUPPRESS_INSTRUMENTATION_KEY, true); | ||
} | ||
|
||
/** | ||
* Sets value on context to indicate that instrumentation should | ||
* no-longer be suppressed beyond this current scope. | ||
* | ||
* @param context context to set the suppress instrumentation value on. | ||
*/ | ||
export function unsuppressInstrumentation(context: Context): Context { | ||
return context.setValue(SUPPRESS_INSTRUMENTATION_KEY, false); | ||
} | ||
|
||
/** | ||
* Return current suppress instrumentation value for the given context, | ||
* if it exists. | ||
* | ||
* @param context context check for the suppress instrumentation value. | ||
*/ | ||
export function isInstrumentationSuppressed(context: Context): boolean { | ||
return Boolean(context.getValue(SUPPRESS_INSTRUMENTATION_KEY)); | ||
} |
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,80 @@ | ||
/* | ||
* 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 { ROOT_CONTEXT, createContextKey } from '@opentelemetry/context-base'; | ||
import { | ||
suppressInstrumentation, | ||
unsuppressInstrumentation, | ||
isInstrumentationSuppressed, | ||
} from '../../src/context/context'; | ||
|
||
const SUPPRESS_INSTRUMENTATION_KEY = createContextKey( | ||
'OpenTelemetry Context Key SUPPRESS_INSTRUMENTATION' | ||
); | ||
|
||
describe('Context Helpers', () => { | ||
describe('suppressInstrumentation', () => { | ||
it('should set suppress to true', () => { | ||
const context = suppressInstrumentation(ROOT_CONTEXT); | ||
assert.deepStrictEqual(isInstrumentationSuppressed(context), true); | ||
}); | ||
}); | ||
|
||
describe('unsuppressInstrumentation', () => { | ||
it('should set suppress to false', () => { | ||
const context = unsuppressInstrumentation(ROOT_CONTEXT); | ||
assert.deepStrictEqual(isInstrumentationSuppressed(context), false); | ||
}); | ||
}); | ||
|
||
describe('isInstrumentationSuppressed', () => { | ||
it('should get value as bool', () => { | ||
const expectedValue = true; | ||
const context = ROOT_CONTEXT.setValue( | ||
SUPPRESS_INSTRUMENTATION_KEY, | ||
expectedValue | ||
); | ||
|
||
const value = isInstrumentationSuppressed(context); | ||
|
||
assert.equal(value, expectedValue); | ||
}); | ||
|
||
describe('when suppress instrumentation set to null', () => { | ||
const context = ROOT_CONTEXT.setValue(SUPPRESS_INSTRUMENTATION_KEY, null); | ||
|
||
it('should return false', () => { | ||
const value = isInstrumentationSuppressed(context); | ||
|
||
assert.equal(value, false); | ||
}); | ||
}); | ||
|
||
describe('when suppress instrumentation set to undefined', () => { | ||
const context = ROOT_CONTEXT.setValue( | ||
SUPPRESS_INSTRUMENTATION_KEY, | ||
undefined | ||
); | ||
|
||
it('should return false', () => { | ||
const value = isInstrumentationSuppressed(context); | ||
|
||
assert.equal(value, false); | ||
}); | ||
}); | ||
}); | ||
}); |
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