|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +import { assert } from "chai"; |
| 5 | +import { SpanKind, TraceFlags } from "@opentelemetry/api"; |
| 6 | +import { setTracer, TestSpan, TestTracer } from "@azure/core-tracing"; |
| 7 | +import sinon from "sinon"; |
| 8 | +import { createSpanFunction } from "../src/createSpan"; |
| 9 | +import { OperationOptions } from "../src/interfaces"; |
| 10 | + |
| 11 | +const createSpan = createSpanFunction({ namespace: "Microsoft.Test", packagePrefix: "Azure.Test" }); |
| 12 | + |
| 13 | +describe("createSpan", () => { |
| 14 | + it("returns a created span with the right metadata", () => { |
| 15 | + const tracer = new TestTracer(); |
| 16 | + const testSpan = new TestSpan( |
| 17 | + tracer, |
| 18 | + "testing", |
| 19 | + { traceId: "", spanId: "", traceFlags: TraceFlags.NONE }, |
| 20 | + SpanKind.INTERNAL |
| 21 | + ); |
| 22 | + const setAttributeSpy = sinon.spy(testSpan, "setAttribute"); |
| 23 | + const startSpanStub = sinon.stub(tracer, "startSpan"); |
| 24 | + startSpanStub.returns(testSpan); |
| 25 | + setTracer(tracer); |
| 26 | + const { span } = createSpan("testMethod", {}); |
| 27 | + assert.strictEqual(span, testSpan, "Should return mocked span"); |
| 28 | + assert.isTrue(startSpanStub.calledOnce); |
| 29 | + const [name, options] = startSpanStub.firstCall.args; |
| 30 | + assert.strictEqual(name, "Azure.Test.testMethod"); |
| 31 | + assert.deepEqual(options, { kind: SpanKind.INTERNAL }); |
| 32 | + assert.isTrue(setAttributeSpy.calledOnceWithExactly("az.namespace", "Microsoft.Test")); |
| 33 | + }); |
| 34 | + |
| 35 | + it("returns updated SpanOptions", () => { |
| 36 | + const options: OperationOptions = {}; |
| 37 | + const { span, updatedOptions } = createSpan("testMethod", options); |
| 38 | + assert.isEmpty(options, "original options should not be modified"); |
| 39 | + assert.notStrictEqual(updatedOptions, options, "should return new object"); |
| 40 | + const expected: OperationOptions = { |
| 41 | + tracingOptions: { |
| 42 | + spanOptions: { |
| 43 | + parent: span.context(), |
| 44 | + attributes: { |
| 45 | + "az.namespace": "Microsoft.Test" |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + }; |
| 50 | + assert.deepEqual(updatedOptions, expected); |
| 51 | + }); |
| 52 | + |
| 53 | + it("preserves existing attributes", () => { |
| 54 | + const options: OperationOptions = { |
| 55 | + tracingOptions: { |
| 56 | + spanOptions: { |
| 57 | + attributes: { |
| 58 | + foo: "bar" |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + }; |
| 63 | + const { span, updatedOptions } = createSpan("testMethod", options); |
| 64 | + assert.notStrictEqual(updatedOptions, options, "should return new object"); |
| 65 | + const expected: OperationOptions = { |
| 66 | + tracingOptions: { |
| 67 | + spanOptions: { |
| 68 | + parent: span.context(), |
| 69 | + attributes: { |
| 70 | + "az.namespace": "Microsoft.Test", |
| 71 | + foo: "bar" |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + }; |
| 76 | + assert.deepEqual(updatedOptions, expected); |
| 77 | + }); |
| 78 | + |
| 79 | + afterEach(() => { |
| 80 | + sinon.restore(); |
| 81 | + }); |
| 82 | +}); |
0 commit comments