Skip to content
This repository has been archived by the owner on Nov 10, 2022. It is now read-only.

feat(context): add utils method to remove keys from context #66 #69

Merged
merged 2 commits into from
May 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion 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 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 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 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