Skip to content

Commit

Permalink
chore: use variables for context keys
Browse files Browse the repository at this point in the history
  • Loading branch information
dyladan committed Feb 12, 2020
1 parent 4d05383 commit 2987a59
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
15 changes: 10 additions & 5 deletions packages/opentelemetry-core/src/context/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
import { Span, SpanContext } from '@opentelemetry/api';
import { Context } from '@opentelemetry/scope-base';

const ACTIVE_SPAN_KEY = 'ACTIVE_SPAN';
const EXTRACTED_SPAN_CONTEXT_KEY = 'EXTRACTED_SPAN_CONTEXT';

/**
* 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') as Span) || undefined;
return (context.getValue(ACTIVE_SPAN_KEY) as Span) || undefined;
}

/**
Expand All @@ -33,7 +36,7 @@ export function getActiveSpan(context: Context): Span | undefined {
* @param span span to set active
*/
export function setActiveSpan(context: Context, span: Span): Context {
return context.setValue('ACTIVE_SPAN', span);
return context.setValue(ACTIVE_SPAN_KEY, span);
}

/**
Expand All @@ -45,7 +48,7 @@ export function getExtractedSpanContext(
context: Context
): SpanContext | undefined {
return (
(context.getValue('EXTRACTED_SPAN_CONTEXT') as SpanContext) || undefined
(context.getValue(EXTRACTED_SPAN_CONTEXT_KEY) as SpanContext) || undefined
);
}

Expand All @@ -59,7 +62,7 @@ export function setExtractedSpanContext(
context: Context,
spanContext: SpanContext
): Context {
return context.setValue('EXTRACTED_SPAN_CONTEXT', spanContext);
return context.setValue(EXTRACTED_SPAN_CONTEXT_KEY, spanContext);
}

/**
Expand All @@ -69,6 +72,8 @@ export function setExtractedSpanContext(
*
* @param context context to get values from
*/
export function getParentSpanContext(context: Context) {
export function getParentSpanContext(
context: Context
): SpanContext | undefined {
return getActiveSpan(context)?.context() || getExtractedSpanContext(context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('ZoneScopeManager', () => {
assert.doesNotThrow(() => {
assert(scopeManager.enable() === scopeManager, 'should return this');
scopeManager.with(ctx, () => {
assert(scopeManager.active() === ctx, 'should has root scope');
assert(scopeManager.active() === ctx, 'should have root scope');
});
});
});
Expand All @@ -56,7 +56,7 @@ describe('ZoneScopeManager', () => {
scopeManager.with(ctx, () => {
assert(
scopeManager.active() === Context.ROOT_CONTEXT,
'should has root scope'
'should have root scope'
);
});
});
Expand Down
4 changes: 2 additions & 2 deletions packages/opentelemetry-web/test/StackScopeManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('StackScopeManager', () => {
assert(scopeManager.enable() === scopeManager, 'should return this');
assert(
scopeManager.active() === Context.ROOT_CONTEXT,
'should has root scope'
'should have root scope'
);
});
});
Expand All @@ -48,7 +48,7 @@ describe('StackScopeManager', () => {
assert(scopeManager.disable() === scopeManager, 'should return this');
assert(
scopeManager.active() === Context.ROOT_CONTEXT,
'should has no scope'
'should have no scope'
);
});
});
Expand Down

0 comments on commit 2987a59

Please sign in to comment.