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

Commit

Permalink
feat: add function to wrap SpanContext in NonRecordingSpan #50
Browse files Browse the repository at this point in the history
  • Loading branch information
dyladan committed Apr 27, 2021
1 parent b5784d3 commit 767028c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/api/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@
* limitations under the License.
*/

import { ProxyTracerProvider } from '../trace/ProxyTracerProvider';
import { Tracer } from '../trace/tracer';
import { TracerProvider } from '../trace/tracer_provider';
import { isSpanContextValid } from '../trace/spancontext-utils';
import {
getGlobal,
registerGlobal,
unregisterGlobal,
} from '../internal/global-utils';
import { ProxyTracerProvider } from '../trace/ProxyTracerProvider';
import {
isSpanContextValid,
wrapSpanContext,
} from '../trace/spancontext-utils';
import { Tracer } from '../trace/tracer';
import { TracerProvider } from '../trace/tracer_provider';

const API_NAME = 'trace';

Expand Down Expand Up @@ -76,5 +79,7 @@ export class TraceAPI {
this._proxyTracerProvider = new ProxyTracerProvider();
}

public wrapSpanContext = wrapSpanContext;

public isSpanContextValid = isSpanContextValid;
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export {
isSpanContextValid,
isValidTraceId,
isValidSpanId,
wrapSpanContext,
} from './trace/spancontext-utils';

export * from './context/context';
Expand Down
11 changes: 11 additions & 0 deletions src/trace/spancontext-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { NonRecordingSpan } from './NonRecordingSpan';
import { Span } from './span';
import { SpanContext } from './span_context';
import { TraceFlags } from './trace_flags';

Expand Down Expand Up @@ -43,3 +45,12 @@ export function isSpanContextValid(spanContext: SpanContext): boolean {
isValidTraceId(spanContext.traceId) && isValidSpanId(spanContext.spanId)
);
}

/**
*
* @param spanContext span context to be wrapped
* @returns a new {@link NonRecordingSpan} with the provided context
*/
export function wrapSpanContext(spanContext: SpanContext): Span {
return new NonRecordingSpan(spanContext);
}
13 changes: 13 additions & 0 deletions test/trace/spancontext-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,17 @@ describe('spancontext-utils', () => {
};
assert.ok(!context.isSpanContextValid(spanContext));
});

it('should wrap a SpanContext in a non-recording span', () => {
const spanContext = {
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
spanId: '6e0c63257de34c92',
traceFlags: TraceFlags.NONE,
};

const span = context.wrapSpanContext(spanContext);

assert.deepStrictEqual(span.spanContext(), spanContext);
assert.strictEqual(span.isRecording(), false);
});
});

0 comments on commit 767028c

Please sign in to comment.