Skip to content

Commit fa4e3d1

Browse files
authored
[instrumentation] - Add integration tests (Azure#20462)
### Packages impacted by this PR @azure/opentelemetry-instrumentation-azure-sdk ### Issues associated with this PR Resolves Azure#18738 ### Describe the problem that is addressed by this PR This PR solves the following: 1. I would like to have an end-to-end test that uses our pipeline and ensures tracing works as expected. The idea here is to ensure we do not regress in the future when making core changes. I added a node-only integration test that uses core-rest-pipeline and a fake client. 1. We currently maintain a TestTracer, TestTracerProvider, and TestSpan independently in multiple places. OpenTelemetry already provides testing utils in their base package. I replaced our Test* objects with an InMemorySpanExporter which allows us to grab all exported spans and inspect them. 1. In-passing, I realized that for a bit there could be a drift between client packages that use the new naming conventions and core packages that use the old naming conventions to pull az.namespace into a span. While it doesn't feel appropriate to add compatibility code in our core packages, this package will be in beta for quite some time so I think it's fine to add it here temporarily.
1 parent adb695e commit fa4e3d1

File tree

14 files changed

+286
-427
lines changed

14 files changed

+286
-427
lines changed

common/config/rush/pnpm-lock.yaml

Lines changed: 59 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/instrumentation/opentelemetry-instrumentation-azure-sdk/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"main": "dist/index.js",
77
"module": "dist-esm/src/index.js",
88
"browser": {
9-
"./dist-esm/src/instrumentation.js": "./dist-esm/src/instrumentation.browser.js"
9+
"./dist-esm/src/instrumentation.js": "./dist-esm/src/instrumentation.browser.js",
10+
"./dist-esm/test/public/util/tracerProvider.js": "./dist-esm/test/public/util/tracerProvider.browser.js"
1011
},
1112
"//metadata": {
1213
"constantPaths": [
@@ -86,9 +87,12 @@
8687
},
8788
"devDependencies": {
8889
"@azure-tools/test-recorder": "^1.0.0",
90+
"@azure/core-rest-pipeline": "^1.5.1",
8991
"@azure/dev-tool": "^1.0.0",
9092
"@azure/eslint-plugin-azure-sdk": "^3.0.0",
9193
"@microsoft/api-extractor": "^7.18.11",
94+
"@opentelemetry/sdk-trace-base": "^1.0.1",
95+
"@opentelemetry/sdk-trace-node": "^1.0.1",
9296
"@types/chai": "^4.1.6",
9397
"@types/mocha": "^7.0.2",
9498
"@types/node": "^12.0.0",

sdk/instrumentation/opentelemetry-instrumentation-azure-sdk/src/instrumenter.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,15 @@ export class OpenTelemetryInstrumenter implements Instrumenter {
2525
.getTracer(spanOptions.packageName, spanOptions.packageVersion)
2626
.startSpan(name, toSpanOptions(spanOptions));
2727

28-
const ctx = spanOptions?.tracingContext || context.active();
28+
let ctx = spanOptions?.tracingContext || context.active();
29+
30+
// COMPAT: remove when core-rest-pipeline has upgraded to core-tracing 1.0
31+
// https://github.com/Azure/azure-sdk-for-js/issues/20567
32+
const newNamespaceKey = Symbol.for("@azure/core-tracing namespace");
33+
const oldNamespaceKey = Symbol.for("az.namespace");
34+
if (!ctx.getValue(oldNamespaceKey) && ctx.getValue(newNamespaceKey)) {
35+
ctx = ctx.setValue(oldNamespaceKey, ctx.getValue(newNamespaceKey));
36+
}
2937

3038
return {
3139
span: new OpenTelemetrySpanWrapper(span),
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
import { SpanKind, SpanStatusCode } from "@opentelemetry/api";
5+
import { TestClient, tracingClientAttributes } from "../../public/util/testClient";
6+
7+
import { assert } from "chai";
8+
import { inMemoryExporter } from "../../public/util/setup";
9+
10+
describe("instrumentation end-to-end tests", () => {
11+
beforeEach(() => {
12+
inMemoryExporter.reset();
13+
});
14+
15+
// This is node-only since we use the BasicTracerProvider in the browser
16+
// which does not set up a context manager. Altenatively we can always pull in
17+
// @opentelemetry/sdk-trace-web but it did not feel necessary at this time.
18+
describe("with a configured client", () => {
19+
it("works when using withSpan", async () => {
20+
await new TestClient().exampleOperation();
21+
const spans = inMemoryExporter.getFinishedSpans();
22+
assert.lengthOf(spans, 3);
23+
const [coreRestPipeline, inner, outer] = spans;
24+
25+
// Check parenting chain
26+
assert.equal(coreRestPipeline.parentSpanId, inner.spanContext().spanId);
27+
assert.equal(inner.parentSpanId, outer.spanContext().spanId);
28+
assert.notExists(outer.parentSpanId);
29+
30+
// Check default span kind
31+
assert.equal(outer.kind, SpanKind.INTERNAL);
32+
33+
// Check specified span kind
34+
assert.equal(inner.kind, SpanKind.CLIENT);
35+
36+
// Check status
37+
assert.deepEqual(coreRestPipeline.status, { code: SpanStatusCode.OK });
38+
assert.deepEqual(inner.status, { code: SpanStatusCode.OK });
39+
assert.deepEqual(outer.status, { code: SpanStatusCode.OK });
40+
41+
// Check instrumentationLibrary
42+
assert.deepEqual(outer.instrumentationLibrary, {
43+
name: tracingClientAttributes.packageName,
44+
version: tracingClientAttributes.packageVersion,
45+
});
46+
47+
// Check attributes on all spans
48+
assert.equal(coreRestPipeline.attributes["az.namespace"], tracingClientAttributes.namespace);
49+
assert.equal(inner.attributes["az.namespace"], tracingClientAttributes.namespace);
50+
assert.equal(outer.attributes["az.namespace"], tracingClientAttributes.namespace);
51+
});
52+
});
53+
});

sdk/instrumentation/opentelemetry-instrumentation-azure-sdk/test/public/instrumenter.spec.ts

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
import { OpenTelemetryInstrumenter, propagator } from "../../src/instrumenter";
55
import { SpanKind, context, trace } from "@opentelemetry/api";
66
import { TracingSpan, TracingSpanKind } from "@azure/core-tracing";
7-
import { resetTracer, setTracer } from "./util/testTracerProvider";
7+
88
import { Context } from "mocha";
99
import { OpenTelemetrySpanWrapper } from "../../src/spanWrapper";
10-
import { TestSpan } from "./util/testSpan";
11-
import { TestTracer } from "./util/testTracer";
10+
import { Span } from "@opentelemetry/sdk-trace-base";
1211
import { assert } from "chai";
12+
import { inMemoryExporter } from "./util/setup";
1313
import sinon from "sinon";
1414

15-
function unwrap(span: TracingSpan): TestSpan {
16-
return (span as OpenTelemetrySpanWrapper).unwrap() as TestSpan;
15+
function unwrap(span: TracingSpan): Span {
16+
return (span as OpenTelemetrySpanWrapper).unwrap() as Span;
1717
}
1818

1919
describe("OpenTelemetryInstrumenter", () => {
@@ -26,7 +26,7 @@ describe("OpenTelemetryInstrumenter", () => {
2626

2727
it("uses the passed in context if it exists", () => {
2828
const propagationSpy = sinon.spy(propagator);
29-
const span = new TestTracer().startSpan("test");
29+
const span = trace.getTracer("test").startSpan("test");
3030
const tracingContext = trace.setSpan(context.active(), span);
3131
instrumenter.createRequestHeaders(tracingContext);
3232
assert.isTrue(propagationSpy.inject.calledWith(tracingContext));
@@ -40,24 +40,20 @@ describe("OpenTelemetryInstrumenter", () => {
4040
});
4141
});
4242

43-
// TODO: the following still uses existing test support for OTel.
44-
// Once the new APIs are available we should move away from those.
4543
describe("#startSpan", () => {
46-
let tracer: TestTracer;
4744
const packageName = "test-package";
4845
const packageVersion = "test-version";
49-
beforeEach(() => {
50-
tracer = setTracer(tracer);
51-
});
5246

53-
afterEach(() => {
54-
resetTracer();
47+
beforeEach(() => {
48+
inMemoryExporter.reset();
5549
});
5650

5751
it("returns a newly started TracingSpan", () => {
5852
const { span } = instrumenter.startSpan("test", { packageName, packageVersion });
53+
span.end();
5954
const otSpan = unwrap(span);
60-
assert.equal(otSpan, tracer.getActiveSpans()[0]);
55+
assert.lengthOf(inMemoryExporter.getFinishedSpans(), 1);
56+
assert.equal(otSpan, inMemoryExporter.getFinishedSpans()[0]);
6157
assert.equal(otSpan.kind, SpanKind.INTERNAL);
6258
});
6359

@@ -90,6 +86,19 @@ describe("OpenTelemetryInstrumenter", () => {
9086

9187
assert.equal(trace.getSpan(tracingContext), unwrap(span));
9288
});
89+
90+
it("adds az.namespace as a context attribute for compatibility", async () => {
91+
const currentContext = context
92+
.active()
93+
.setValue(Symbol.for("@azure/core-tracing namespace"), "test-namespace");
94+
95+
const { tracingContext } = instrumenter.startSpan("test", {
96+
tracingContext: currentContext,
97+
packageName,
98+
});
99+
100+
assert.equal(tracingContext.getValue(Symbol.for("az.namespace")), "test-namespace");
101+
});
93102
});
94103

95104
describe("when a context is not provided", () => {
@@ -208,7 +217,7 @@ describe("OpenTelemetryInstrumenter", () => {
208217

209218
// Function syntax
210219
instrumenter.withContext(context.active(), function (this: any) {
211-
assert.isUndefined(this);
220+
assert.notExists(this);
212221
});
213222
instrumenter.withContext(
214223
context.active(),

0 commit comments

Comments
 (0)