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

feat: make context.active() the default value for certain context functions #30

Closed
wants to merge 2 commits into from
Closed
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
12 changes: 6 additions & 6 deletions docs/tracing.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,20 @@ async function onGet(request, response) {

const userId = request.params.id;

// Create a new context from the current context which has the span "active"
const ctx = setSpan(context.active(), span);
// Create a new context and set the span
const ctx = setSpan(span);

// Call getUser with the newly created context
//
//
// context.with calls a function with an associated "active" context. Within
// the function, calling context.active() returns the currently active context.
// If there is no active context, the ROOT_CONTEXT will be returned, which
// has no key-value pairs.
//
//
// context.with requires at least 2 arguments: a context and a function to be called.
// If a third argument is provided, it will be bound to `this` `this` inside the function.
// Any additional parameters are used as arguments when calling the function.
//
//
// Return value is the value returned from getUser
// | Context to be used as the "active" context
// | | Function to be called
Expand Down
28 changes: 14 additions & 14 deletions src/context/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import { Context } from './types';
import { Baggage, Span, SpanContext } from '../';
import { Baggage, context, Span, SpanContext } from '../';
import { NoopSpan } from '../trace/NoopSpan';

/**
Expand All @@ -39,20 +39,20 @@ const BAGGAGE_KEY = createContextKey('OpenTelemetry Baggage Key');
/**
* Return the span if one exists
*
* @param context context to get span from
* @param ctx context to get span from
*/
export function getSpan(context: Context): Span | undefined {
return (context.getValue(SPAN_KEY) as Span) || undefined;
export function getSpan(ctx = context.active()): Span | undefined {
return (ctx.getValue(SPAN_KEY) as Span) || undefined;
}

/**
* Set the span on a context
*
* @param context context to use as parent
* @param span span to set active
* @param span to set
* @param ctx context to use as parent
*/
export function setSpan(context: Context, span: Span): Context {
return context.setValue(SPAN_KEY, span);
export function setSpan(span: Span, ctx = context.active()): Context {
return ctx.setValue(SPAN_KEY, span);
}

/**
Expand All @@ -66,7 +66,7 @@ export function setSpanContext(
context: Context,
spanContext: SpanContext
): Context {
return setSpan(context, new NoopSpan(spanContext));
return setSpan(new NoopSpan(spanContext), context);
}

/**
Expand All @@ -82,10 +82,10 @@ export function getSpanContext(context: Context): SpanContext | undefined {
* Sets value on context to indicate that instrumentation should
* be suppressed beyond this current scope.
*
* @param context context to set the suppress instrumentation value on.
* @param ctx context to set the suppress instrumentation value on.
*/
export function suppressInstrumentation(context: Context): Context {
return context.setValue(SUPPRESS_INSTRUMENTATION_KEY, true);
export function suppressInstrumentation(ctx = context.active()): Context {
return ctx.setValue(SUPPRESS_INSTRUMENTATION_KEY, true);
}

/**
Expand All @@ -109,15 +109,15 @@ export function isInstrumentationSuppressed(context: Context): boolean {
}

/**
* @param {Context} Context that manage all context values
* @param {Context} context that manage all context values
* @returns {Baggage} Extracted baggage from the context
*/
export function getBaggage(context: Context): Baggage | undefined {
return (context.getValue(BAGGAGE_KEY) as Baggage) || undefined;
}

/**
* @param {Context} Context that manage all context values
* @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 {
Expand Down