/
tracingPolicy.ts
89 lines (79 loc) · 2.7 KB
/
tracingPolicy.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { getTracer, getTraceParentHeader } from "@azure/core-tracing";
import { SpanOptions, SpanKind } from "@opentelemetry/api";
import {
RequestPolicyFactory,
RequestPolicy,
RequestPolicyOptions,
BaseRequestPolicy
} from "./requestPolicy";
import { WebResourceLike } from "../webResource";
import { HttpOperationResponse } from "../httpOperationResponse";
import { URLBuilder } from "../url";
export interface TracingPolicyOptions {
userAgent?: string;
}
export function tracingPolicy(tracingOptions: TracingPolicyOptions = {}): RequestPolicyFactory {
return {
create(nextPolicy: RequestPolicy, options: RequestPolicyOptions) {
return new TracingPolicy(nextPolicy, options, tracingOptions);
}
};
}
export class TracingPolicy extends BaseRequestPolicy {
private userAgent?: string;
constructor(
nextPolicy: RequestPolicy,
options: RequestPolicyOptions,
tracingOptions: TracingPolicyOptions
) {
super(nextPolicy, options);
this.userAgent = tracingOptions.userAgent;
}
public async sendRequest(request: WebResourceLike): Promise<HttpOperationResponse> {
if (!request.spanOptions || !request.spanOptions.parent) {
return this._nextPolicy.sendRequest(request);
}
// create a new span
const tracer = getTracer();
const spanOptions: SpanOptions = {
...request.spanOptions,
kind: SpanKind.CLIENT
};
const path = URLBuilder.parse(request.url).getPath() || "/";
const span = tracer.startSpan(path, spanOptions);
span.setAttributes({
"http.method": request.method,
"http.url": request.url,
requestId: request.requestId
});
if (this.userAgent) {
span.setAttribute("http.user_agent", this.userAgent);
}
try {
// set headers
const spanContext = span.context();
const traceParentHeader = getTraceParentHeader(spanContext);
if (traceParentHeader) {
request.headers.set("traceparent", traceParentHeader);
const traceState = spanContext.traceState && spanContext.traceState.serialize();
// if tracestate is set, traceparent MUST be set, so only set tracestate after traceparent
if (traceState) {
request.headers.set("tracestate", traceState);
}
}
const response = await this._nextPolicy.sendRequest(request);
span.setAttribute("http.status_code", response.status);
const serviceRequestId = response.headers.get("x-ms-request-id");
if (serviceRequestId) {
span.setAttribute("serviceRequestId", serviceRequestId);
}
span.end();
return response;
} catch (err) {
span.end();
throw err;
}
}
}