/
tracing.ts
43 lines (40 loc) · 1.36 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { RequestOptionsBase } from "@azure/core-http";
import { getTracer } from "@azure/core-tracing";
import { Span } from "@opentelemetry/api";
/**
* Creates a span using the tracer that was set by the user
* @param methodName - The name of the method creating the span.
* @param options - The options for the underlying HTTP request.
*/
export function createSpan(methodName: string, requestOptions: RequestOptionsBase = {}): Span {
const tracer = getTracer();
const span = tracer.startSpan(methodName, requestOptions && requestOptions.spanOptions);
span.setAttribute("az.namespace", "Microsoft.KeyVault");
return span;
}
/**
* Returns updated HTTP options with the given span as the parent of future spans,
* if applicable.
* @param span - The span for the current operation.
* @param options - The options for the underlying HTTP request.
*/
export function setParentSpan(span: Span, options: RequestOptionsBase = {}): RequestOptionsBase {
if (span.isRecording()) {
const spanOptions = options.spanOptions || {};
return {
...options,
spanOptions: {
...spanOptions,
parent: span.context(),
attributes: {
...spanOptions.attributes,
"az.namespace": "Microsoft.KeyVault"
}
}
};
} else {
return options;
}
}