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

feat: add function to wrap SpanContext in NonRecordingSpan #49 #51

Merged
merged 3 commits into from
Apr 28, 2021
Merged
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
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;
}
12 changes: 12 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,13 @@ export function isSpanContextValid(spanContext: SpanContext): boolean {
isValidTraceId(spanContext.traceId) && isValidSpanId(spanContext.spanId)
);
}

/**
* Wrap the given {@link SpanContext} in a new non-recording {@link Span}
*
* @param spanContext span context to be wrapped
* @returns a new non-recording {@link Span} 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);
});
});