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

Commit

Permalink
Merge branch 'main' into sampler
Browse files Browse the repository at this point in the history
  • Loading branch information
legendecas committed Aug 9, 2022
2 parents 16d13ff + 17ccb3a commit 4cb6476
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/api/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { Tracer } from '../trace/tracer';
import { TracerProvider } from '../trace/tracer_provider';
import {
deleteSpan,
getActiveSpan,
getSpan,
getSpanContext,
setSpan,
Expand Down Expand Up @@ -102,6 +103,8 @@ export class TraceAPI {

public getSpan = getSpan;

public getActiveSpan = getActiveSpan;

public getSpanContext = getSpanContext;

public setSpan = setSpan;
Expand Down
8 changes: 8 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 { ContextAPI } from '../api/context';

/**
* span key
Expand All @@ -34,6 +35,13 @@ export function getSpan(context: Context): Span | undefined {
return (context.getValue(SPAN_KEY) as Span) || undefined;
}

/**
* Gets the span from the current context, if one exists.
*/
export function getActiveSpan(): Span | undefined {
return getSpan(ContextAPI.getInstance().active());
}

/**
* Set the span on a context
*
Expand Down
11 changes: 11 additions & 0 deletions test/api/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ describe('API', () => {
assert.strictEqual(typeof tracer, 'object');
});

it('getActiveSpan should get the current span', () => {
const span = new NonRecordingSpan();
const ctx = trace.setSpan(ROOT_CONTEXT, span);
context.setGlobalContextManager({ active: () => ctx, disable: () => {} } as any);

const active = trace.getActiveSpan();
assert.strictEqual(active, span);

context.disable();
});

describe('Context', () => {
it('with should forward this, arguments and return value', () => {
function fnWithThis(this: string, a: string, b: number): string {
Expand Down

0 comments on commit 4cb6476

Please sign in to comment.