/
Config.ts
132 lines (111 loc) · 6.35 KB
/
Config.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import CorrelationIdManager = require('./CorrelationIdManager');
import ConnectionStringParser = require('./ConnectionStringParser');
import Constants = require('../Declarations/Constants');
import http = require('http');
import https = require('https');
import url = require('url');
class Config {
// Azure adds this prefix to all environment variables
public static ENV_azurePrefix = "APPSETTING_";
// This key is provided in the readme
public static ENV_iKey = "APPINSIGHTS_INSTRUMENTATIONKEY";
public static legacy_ENV_iKey = "APPINSIGHTS_INSTRUMENTATION_KEY";
public static ENV_profileQueryEndpoint = "APPINSIGHTS_PROFILE_QUERY_ENDPOINT";
public static ENV_quickPulseHost = "APPINSIGHTS_QUICKPULSE_HOST";
// Azure Connection String
public static ENV_connectionString = "APPLICATIONINSIGHTS_CONNECTION_STRING";
// Native Metrics Opt Outs
public static ENV_nativeMetricsDisablers = "APPLICATION_INSIGHTS_DISABLE_EXTENDED_METRIC";
public static ENV_nativeMetricsDisableAll = "APPLICATION_INSIGHTS_DISABLE_ALL_EXTENDED_METRICS"
public static ENV_http_proxy = "http_proxy";
public static ENV_https_proxy = "https_proxy";
/** An identifier for your Application Insights resource */
public instrumentationKey: string;
/** The id for cross-component correlation. READ ONLY. */
public correlationId: string;
/** The ingestion endpoint to send telemetry payloads to */
public endpointUrl: string;
/** The maximum number of telemetry items to include in a payload to the ingestion endpoint (Default 250) */
public maxBatchSize: number;
/** The maximum amount of time to wait for a payload to reach maxBatchSize (Default 15000) */
public maxBatchIntervalMs: number;
/** A flag indicating if telemetry transmission is disabled (Default false) */
public disableAppInsights: boolean;
/** The percentage of telemetry items tracked that should be transmitted (Default 100) */
public samplingPercentage: number;
/** The time to wait before retrying to retrieve the id for cross-component correlation (Default 30000) */
public correlationIdRetryIntervalMs: number;
/** A list of domains to exclude from cross-component header injection */
public correlationHeaderExcludedDomains: string[];
/** A proxy server for SDK HTTP traffic (Optional, Default pulled from `http_proxy` environment variable) */
public proxyHttpUrl: string;
/** A proxy server for SDK HTTPS traffic (Optional, Default pulled from `https_proxy` environment variable) */
public proxyHttpsUrl: string;
/** An http.Agent to use for SDK HTTP traffic (Optional, Default undefined) */
public httpAgent: http.Agent;
/** An https.Agent to use for SDK HTTPS traffic (Optional, Default undefined) */
public httpsAgent: https.Agent;
private endpointBase: string = Constants.DEFAULT_BREEZE_ENDPOINT;
private setCorrelationId: (v: string) => void;
private _profileQueryEndpoint: string;
/** Host name for quickpulse service */
private _quickPulseHost: string;
constructor(setupString?: string) {
const connectionStringEnv: string | undefined = process.env[Config.ENV_connectionString];
const csCode = ConnectionStringParser.parse(setupString);
const csEnv = ConnectionStringParser.parse(connectionStringEnv);
const iKeyCode = !csCode.instrumentationkey && Object.keys(csCode).length > 0
? null // CS was valid but instrumentation key was not provided, null and grab from env var
: setupString; // CS was invalid, so it must be an ikey
this.instrumentationKey = csCode.instrumentationkey || iKeyCode /* === instrumentationKey */ || csEnv.instrumentationkey || Config._getInstrumentationKey();
this.endpointUrl = `${csCode.ingestionendpoint || csEnv.ingestionendpoint || this.endpointBase}/v2/track`;
this.maxBatchSize = 250;
this.maxBatchIntervalMs = 15000;
this.disableAppInsights = false;
this.samplingPercentage = 100;
this.correlationIdRetryIntervalMs = 30 * 1000;
this.correlationHeaderExcludedDomains = [
"*.core.windows.net",
"*.core.chinacloudapi.cn",
"*.core.cloudapi.de",
"*.core.usgovcloudapi.net"];
this.setCorrelationId = (correlationId) => this.correlationId = correlationId;
this.proxyHttpUrl = process.env[Config.ENV_http_proxy] || undefined;
this.proxyHttpsUrl = process.env[Config.ENV_https_proxy] || undefined;
this.httpAgent = undefined;
this.httpsAgent = undefined;
this.profileQueryEndpoint = csCode.ingestionendpoint || csEnv.ingestionendpoint || process.env[Config.ENV_profileQueryEndpoint] || this.endpointBase;
this._quickPulseHost = csCode.liveendpoint || csEnv.liveendpoint || process.env[Config.ENV_quickPulseHost] || Constants.DEFAULT_LIVEMETRICS_HOST;
// Parse quickPulseHost if it startswith http(s)://
if (this._quickPulseHost.match(/^https?:\/\//)) {
this._quickPulseHost = url.parse(this._quickPulseHost).host;
}
}
public set profileQueryEndpoint(endpoint: string) {
CorrelationIdManager.cancelCorrelationIdQuery(this, this.setCorrelationId);
this._profileQueryEndpoint = endpoint;
this.correlationId = CorrelationIdManager.correlationIdPrefix; // Reset the correlationId while we wait for the new query
CorrelationIdManager.queryCorrelationId(this, this.setCorrelationId);
}
public get profileQueryEndpoint() {
return this._profileQueryEndpoint;
}
public set quickPulseHost(host: string) {
this._quickPulseHost = host;
}
public get quickPulseHost(): string {
return this._quickPulseHost;
}
private static _getInstrumentationKey(): string {
// check for both the documented env variable and the azure-prefixed variable
var iKey = process.env[Config.ENV_iKey]
|| process.env[Config.ENV_azurePrefix + Config.ENV_iKey]
|| process.env[Config.legacy_ENV_iKey]
|| process.env[Config.ENV_azurePrefix + Config.legacy_ENV_iKey];
if (!iKey || iKey == "") {
throw new Error("Instrumentation key not found, pass the key in the config to this method or set the key in the environment variable APPINSIGHTS_INSTRUMENTATIONKEY before starting the server");
}
return iKey;
}
}
export = Config;