Skip to content

Commit

Permalink
feat(context): add utils method to remove keys from context open-tele…
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarchaud authored May 21, 2021
1 parent ee577dc commit 698e484
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
8 changes: 7 additions & 1 deletion api/src/api/propagation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ import {
registerGlobal,
unregisterGlobal,
} from '../internal/global-utils';
import { getBaggage, setBaggage } from '../baggage/context-helpers';
import {
getBaggage,
setBaggage,
deleteBaggage,
} from '../baggage/context-helpers';
import { createBaggage } from '../baggage/utils';

const API_NAME = 'propagation';
Expand Down Expand Up @@ -108,6 +112,8 @@ export class PropagationAPI {

public setBaggage = setBaggage;

public deleteBaggage = deleteBaggage;

private _getGlobalPropagator(): TextMapPropagator {
return getGlobal(API_NAME) || NOOP_TEXT_MAP_PROPAGATOR;
}
Expand Down
3 changes: 3 additions & 0 deletions api/src/api/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
import { Tracer } from '../trace/tracer';
import { TracerProvider } from '../trace/tracer_provider';
import {
deleteSpan,
getSpan,
getSpanContext,
setSpan,
Expand Down Expand Up @@ -89,6 +90,8 @@ export class TraceAPI {

public isSpanContextValid = isSpanContextValid;

public deleteSpan = deleteSpan;

public getSpan = getSpan;

public getSpanContext = getSpanContext;
Expand Down
13 changes: 13 additions & 0 deletions api/src/baggage/context-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import { Baggage } from './types';
const BAGGAGE_KEY = createContextKey('OpenTelemetry Baggage Key');

/**
* Retrieve the current baggage from the given context
*
* @param {Context} Context that manage all context values
* @returns {Baggage} Extracted baggage from the context
*/
Expand All @@ -32,9 +34,20 @@ export function getBaggage(context: Context): Baggage | undefined {
}

/**
* Store a baggage in the given context
*
* @param {Context} Context that manage all context values
* @param {Baggage} baggage that will be set in the actual context
*/
export function setBaggage(context: Context, baggage: Baggage): Context {
return context.setValue(BAGGAGE_KEY, baggage);
}

/**
* Delete the baggage stored in the given context
*
* @param {Context} Context that manage all context values
*/
export function deleteBaggage(context: Context): Context {
return context.deleteValue(BAGGAGE_KEY);
}
9 changes: 9 additions & 0 deletions api/src/trace/context-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ export function setSpan(context: Context, span: Span): Context {
return context.setValue(SPAN_KEY, span);
}

/**
* Remove current span stored in the context
*
* @param context context to delete span from
*/
export function deleteSpan(context: Context): Context {
return context.deleteValue(SPAN_KEY);
}

/**
* Wrap span context in a NoopSpan and set as span in a new
* context
Expand Down

0 comments on commit 698e484

Please sign in to comment.