/
tracing.ts
57 lines (48 loc) · 1.55 KB
/
tracing.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { OperationOptions } from "@azure/core-http";
import { getTracer } from "@azure/core-tracing";
import { Span, SpanOptions, SpanKind } from "@opentelemetry/api";
type OperationTracingOptions = OperationOptions["tracingOptions"];
/**
* Creates a span using the global tracer.
* @ignore
* @param name The name of the operation being performed.
* @param tracingOptions The options for the underlying http request.
*/
export function createSpan<T extends OperationOptions>(
operationName: string,
operationOptions: T
): { span: Span; updatedOptions: T } {
const tracer = getTracer();
const tracingOptions = operationOptions.tracingOptions || {};
const spanOptions: SpanOptions = {
...tracingOptions.spanOptions,
kind: SpanKind.INTERNAL
};
const span = tracer.startSpan(`Azure.Communication.${operationName}`, spanOptions);
span.setAttribute("az.namespace", "Microsoft.Communication");
let newSpanOptions = tracingOptions.spanOptions || {};
if (span.isRecording()) {
newSpanOptions = {
...tracingOptions.spanOptions,
parent: span.context(),
attributes: {
...spanOptions.attributes,
"az.namespace": "Microsoft.Communication"
}
};
}
const newTracingOptions: OperationTracingOptions = {
...tracingOptions,
spanOptions: newSpanOptions
};
const newOperationOptions: T = {
...operationOptions,
tracingOptions: newTracingOptions
};
return {
span,
updatedOptions: newOperationOptions
};
}