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

Commit

Permalink
feat(context): add utils method to remove keys from context #66
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarchaud committed May 15, 2021
1 parent 8435e0a commit 7d2fe8c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/api/propagation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ import {
registerGlobal,
unregisterGlobal,
} from '../internal/global-utils';
import { getBaggage, createBaggage, setBaggage } from '../baggage/index';
import {
getBaggage,
createBaggage,
setBaggage,
deleteBaggage,
} from '../baggage/index';

const API_NAME = 'propagation';

Expand Down Expand Up @@ -107,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
6 changes: 6 additions & 0 deletions src/api/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import {
import { Tracer } from '../trace/tracer';
import { TracerProvider } from '../trace/tracer_provider';
import {
deleteSpan,
deleteSpanContext,
getSpan,
getSpanContext,
setSpan,
Expand Down Expand Up @@ -89,6 +91,10 @@ export class TraceAPI {

public isSpanContextValid = isSpanContextValid;

public deleteSpan = deleteSpan;

public deleteSpanContext = deleteSpanContext;

public getSpan = getSpan;

public getSpanContext = getSpanContext;
Expand Down
13 changes: 13 additions & 0 deletions src/baggage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export function createBaggage(
}

/**
* Get baggage from context if its present
*
* @param {Context} Context that manage all context values
* @returns {Baggage} Extracted baggage from the context
*/
Expand All @@ -49,13 +51,24 @@ export function getBaggage(context: Context): Baggage | undefined {
}

/**
* Set a baggage in a new context and returns it
*
* @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 baggage from the given context
*
* @param {Context} Context context to delete baggage from
*/
export function deleteBaggage(context: Context): Context {
return context.deleteValue(BAGGAGE_KEY);
}

/**
* Create a serializable BaggageEntryMetadata object from a string.
*
Expand Down
19 changes: 19 additions & 0 deletions src/trace/context-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { Context } from '../context/types';
import { Span } from './span';
import { SpanContext } from './span_context';
import { NonRecordingSpan } from './NonRecordingSpan';
import { INVALID_SPAN_CONTEXT } from './spancontext-utils';

/**
* span key
Expand All @@ -44,6 +45,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 All @@ -66,3 +76,12 @@ export function setSpanContext(
export function getSpanContext(context: Context): SpanContext | undefined {
return getSpan(context)?.spanContext();
}

/**
* Replace the span context from the context with an invalid span context
*
* @param context context to remove the span context from
*/
export function deleteSpanContext(context: Context): Context {
return setSpan(context, new NonRecordingSpan(INVALID_SPAN_CONTEXT));
}

0 comments on commit 7d2fe8c

Please sign in to comment.