From 2987a5962966abf90e91e3081ad42fc658f0067d Mon Sep 17 00:00:00 2001 From: Daniel Dyla Date: Wed, 12 Feb 2020 10:13:45 -0500 Subject: [PATCH] chore: use variables for context keys --- .../opentelemetry-core/src/context/context.ts | 15 ++++++++++----- .../test/ZoneScopeManager.test.ts | 4 ++-- .../test/StackScopeManager.test.ts | 4 ++-- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/packages/opentelemetry-core/src/context/context.ts b/packages/opentelemetry-core/src/context/context.ts index e1fec8b8b8..ee293556a2 100644 --- a/packages/opentelemetry-core/src/context/context.ts +++ b/packages/opentelemetry-core/src/context/context.ts @@ -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; } /** @@ -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); } /** @@ -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 ); } @@ -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); } /** @@ -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); } diff --git a/packages/opentelemetry-scope-zone-peer-dep/test/ZoneScopeManager.test.ts b/packages/opentelemetry-scope-zone-peer-dep/test/ZoneScopeManager.test.ts index 8973c7e06e..57a4694ce2 100644 --- a/packages/opentelemetry-scope-zone-peer-dep/test/ZoneScopeManager.test.ts +++ b/packages/opentelemetry-scope-zone-peer-dep/test/ZoneScopeManager.test.ts @@ -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'); }); }); }); @@ -56,7 +56,7 @@ describe('ZoneScopeManager', () => { scopeManager.with(ctx, () => { assert( scopeManager.active() === Context.ROOT_CONTEXT, - 'should has root scope' + 'should have root scope' ); }); }); diff --git a/packages/opentelemetry-web/test/StackScopeManager.test.ts b/packages/opentelemetry-web/test/StackScopeManager.test.ts index 3d419aa0cb..4e12c9f123 100644 --- a/packages/opentelemetry-web/test/StackScopeManager.test.ts +++ b/packages/opentelemetry-web/test/StackScopeManager.test.ts @@ -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' ); }); }); @@ -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' ); }); });