/
TelemetryPolicyFactory.ts
73 lines (64 loc) · 2.08 KB
/
TelemetryPolicyFactory.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import {
isNode,
RequestPolicy,
RequestPolicyFactory,
RequestPolicyOptions,
UserAgentOptions
} from "@azure/core-http";
import * as os from "os";
import { TelemetryPolicy } from "./policies/TelemetryPolicy";
import { SDK_VERSION } from "./utils/constants";
/**
* TelemetryPolicyFactory is a factory class helping generating {@link TelemetryPolicy} objects.
*
* @export
* @class TelemetryPolicyFactory
* @implements {RequestPolicyFactory}
*/
export class TelemetryPolicyFactory implements RequestPolicyFactory {
/**
* @internal
* @ignore
*/
public readonly telemetryString: string;
/**
* Creates an instance of TelemetryPolicyFactory.
* @param {UserAgentOptions} [telemetry]
* @memberof TelemetryPolicyFactory
*/
constructor(telemetry?: UserAgentOptions) {
const userAgentInfo: string[] = [];
if (isNode) {
if (telemetry) {
const telemetryString = (telemetry.userAgentPrefix || "").replace(" ", "");
if (telemetryString.length > 0 && userAgentInfo.indexOf(telemetryString) === -1) {
userAgentInfo.push(telemetryString);
}
}
// e.g. azsdk-js-storagequeue/11.0.0
const libInfo = `azsdk-js-storagequeue/${SDK_VERSION}`;
if (userAgentInfo.indexOf(libInfo) === -1) {
userAgentInfo.push(libInfo);
}
// e.g. (NODE-VERSION 4.9.1; Windows_NT 10.0.16299)
const runtimeInfo = `(NODE-VERSION ${process.version}; ${os.type()} ${os.release()})`;
if (userAgentInfo.indexOf(runtimeInfo) === -1) {
userAgentInfo.push(runtimeInfo);
}
}
this.telemetryString = userAgentInfo.join(" ");
}
/**
* Creates a {@link TelemetryPolicy} object.
*
* @param {RequestPolicy} nextPolicy
* @param {RequestPolicyOptions} options
* @returns {TelemetryPolicy}
* @memberof TelemetryPolicyFactory
*/
public create(nextPolicy: RequestPolicy, options: RequestPolicyOptions): TelemetryPolicy {
return new TelemetryPolicy(nextPolicy, options, this.telemetryString);
}
}