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

Commit 324f197

Browse files
authored
feat: add function to wrap SpanContext in NonRecordingSpan #49 (#51)
1 parent b5784d3 commit 324f197

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

src/api/trace.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { ProxyTracerProvider } from '../trace/ProxyTracerProvider';
18-
import { Tracer } from '../trace/tracer';
19-
import { TracerProvider } from '../trace/tracer_provider';
20-
import { isSpanContextValid } from '../trace/spancontext-utils';
2117
import {
2218
getGlobal,
2319
registerGlobal,
2420
unregisterGlobal,
2521
} from '../internal/global-utils';
22+
import { ProxyTracerProvider } from '../trace/ProxyTracerProvider';
23+
import {
24+
isSpanContextValid,
25+
wrapSpanContext,
26+
} from '../trace/spancontext-utils';
27+
import { Tracer } from '../trace/tracer';
28+
import { TracerProvider } from '../trace/tracer_provider';
2629

2730
const API_NAME = 'trace';
2831

@@ -76,5 +79,7 @@ export class TraceAPI {
7679
this._proxyTracerProvider = new ProxyTracerProvider();
7780
}
7881

82+
public wrapSpanContext = wrapSpanContext;
83+
7984
public isSpanContextValid = isSpanContextValid;
8085
}

src/trace/spancontext-utils.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
import { NonRecordingSpan } from './NonRecordingSpan';
17+
import { Span } from './span';
1618
import { SpanContext } from './span_context';
1719
import { TraceFlags } from './trace_flags';
1820

@@ -43,3 +45,13 @@ export function isSpanContextValid(spanContext: SpanContext): boolean {
4345
isValidTraceId(spanContext.traceId) && isValidSpanId(spanContext.spanId)
4446
);
4547
}
48+
49+
/**
50+
* Wrap the given {@link SpanContext} in a new non-recording {@link Span}
51+
*
52+
* @param spanContext span context to be wrapped
53+
* @returns a new non-recording {@link Span} with the provided context
54+
*/
55+
export function wrapSpanContext(spanContext: SpanContext): Span {
56+
return new NonRecordingSpan(spanContext);
57+
}

test/trace/spancontext-utils.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,17 @@ describe('spancontext-utils', () => {
5454
};
5555
assert.ok(!context.isSpanContextValid(spanContext));
5656
});
57+
58+
it('should wrap a SpanContext in a non-recording span', () => {
59+
const spanContext = {
60+
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
61+
spanId: '6e0c63257de34c92',
62+
traceFlags: TraceFlags.NONE,
63+
};
64+
65+
const span = context.wrapSpanContext(spanContext);
66+
67+
assert.deepStrictEqual(span.spanContext(), spanContext);
68+
assert.strictEqual(span.isRecording(), false);
69+
});
5770
});

0 commit comments

Comments
 (0)