-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathConfig.ts
225 lines (206 loc) · 11.3 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import azureCoreAuth = require("@azure/core-auth");
import CorrelationIdManager = require("./CorrelationIdManager");
import ConnectionStringParser = require("./ConnectionStringParser");
import Logging = require("./Logging");
import Constants = require("../Declarations/Constants");
import http = require("http");
import https = require("https");
import url = require("url");
import { JsonConfig } from "./JsonConfig";
import { IConfig, IWebInstrumentationConfig } from "../Declarations/Interfaces";
import { DistributedTracingModes } from "../applicationinsights";
import { IDisabledExtendedMetrics } from "../AutoCollection/NativePerformance";
class Config implements IConfig {
public static ENV_azurePrefix = "APPSETTING_"; // Azure adds this prefix to all environment variables
public static ENV_iKey = "APPINSIGHTS_INSTRUMENTATIONKEY"; // This key is provided in the readme
public static legacy_ENV_iKey = "APPINSIGHTS_INSTRUMENTATION_KEY";
public static ENV_profileQueryEndpoint = "APPINSIGHTS_PROFILE_QUERY_ENDPOINT";
public static ENV_quickPulseHost = "APPINSIGHTS_QUICKPULSE_HOST";
// IConfig properties
public endpointUrl: string;
public maxBatchSize: number;
public maxBatchIntervalMs: number;
public disableAppInsights: boolean;
public samplingPercentage: number;
public correlationIdRetryIntervalMs: number;
public correlationHeaderExcludedDomains: string[];
public proxyHttpUrl: string;
public proxyHttpsUrl: string;
public httpAgent: http.Agent;
public httpsAgent: https.Agent;
public ignoreLegacyHeaders: boolean;
public aadTokenCredential?: azureCoreAuth.TokenCredential;
public aadAudience?: string;
public enableAutoCollectConsole: boolean;
public enableLoggerErrorToTrace: boolean;
public enableAutoCollectExceptions: boolean;
public enableAutoCollectPerformance: boolean;
public enableAutoCollectExternalLoggers: boolean;
public enableAutoCollectPreAggregatedMetrics: boolean;
public enableAutoCollectHeartbeat: boolean;
public enableAutoCollectRequests: boolean;
public enableAutoCollectDependencies: boolean;
public enableAutoDependencyCorrelation: boolean;
public enableAutoCollectIncomingRequestAzureFunctions: boolean;
public enableSendLiveMetrics: boolean;
public enableUseDiskRetryCaching: boolean;
public enableUseAsyncHooks: boolean;
public distributedTracingMode: DistributedTracingModes;
public enableAutoCollectExtendedMetrics: boolean | IDisabledExtendedMetrics;
public enableResendInterval: number;
public enableMaxBytesOnDisk: number;
public enableInternalDebugLogging: boolean;
public enableInternalWarningLogging: boolean;
public disableAllExtendedMetrics: boolean;
public disableStatsbeat: boolean;
public extendedMetricDisablers: string;
public quickPulseHost: string;
public enableWebInstrumentation: boolean;
public webInstrumentationConfig: IWebInstrumentationConfig[];
public webInstrumentationSrc: string;
// To Be deprecated.
public enableAutoWebSnippetInjection: boolean;
public correlationId: string; // TODO: Should be private
private _connectionString: string;
private _endpointBase: string = Constants.DEFAULT_BREEZE_ENDPOINT;
private _profileQueryEndpoint: string;
private _instrumentationKey: string;
public _webInstrumentationConnectionString: string;
constructor(setupString?: string) {
// Load config values from env variables and JSON if available
this._mergeConfig();
const connectionStringEnv: string | undefined = this._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
const instrumentationKeyEnv: string | undefined = this._instrumentationKey;
this.instrumentationKey = csCode.instrumentationkey || iKeyCode /* === instrumentationKey */ || csEnv.instrumentationkey || instrumentationKeyEnv;
let endpoint = `${this.endpointUrl || csCode.ingestionendpoint || csEnv.ingestionendpoint || this._endpointBase}`;
if (endpoint.endsWith("/")) {
// Remove extra '/' if present
endpoint = endpoint.slice(0, -1);
}
this.endpointUrl = `${endpoint}/v2.1/track`;
this.maxBatchSize = this.maxBatchSize || 250;
this.maxBatchIntervalMs = this.maxBatchIntervalMs || 15000;
this.disableAppInsights = this.disableAppInsights || false;
this.samplingPercentage = this.samplingPercentage || 100;
this.correlationIdRetryIntervalMs = this.correlationIdRetryIntervalMs || 30 * 1000;
this.enableWebInstrumentation = this.enableWebInstrumentation || this.enableAutoWebSnippetInjection || false;
this.webInstrumentationConfig = this.webInstrumentationConfig || null;
this.enableAutoWebSnippetInjection = this.enableWebInstrumentation;
this.correlationHeaderExcludedDomains =
this.correlationHeaderExcludedDomains ||
[
"*.core.windows.net",
"*.core.chinacloudapi.cn",
"*.core.cloudapi.de",
"*.core.usgovcloudapi.net",
"*.core.microsoft.scloud",
"*.core.eaglex.ic.gov"
];
this.ignoreLegacyHeaders = this.ignoreLegacyHeaders || false;
this.profileQueryEndpoint = csCode.ingestionendpoint || csEnv.ingestionendpoint || process.env[Config.ENV_profileQueryEndpoint] || this._endpointBase;
this.quickPulseHost = this.quickPulseHost || csCode.liveendpoint || csEnv.liveendpoint || process.env[Config.ENV_quickPulseHost] || Constants.DEFAULT_LIVEMETRICS_HOST;
this.webInstrumentationConnectionString = this.webInstrumentationConnectionString || this._webInstrumentationConnectionString || "";
this.webSnippetConnectionString = this.webInstrumentationConnectionString;
// Parse quickPulseHost if it starts with http(s)://
if (this.quickPulseHost.match(/^https?:\/\//)) {
this.quickPulseHost = new url.URL(this.quickPulseHost).host;
}
this.aadAudience = csCode.aadaudience || csEnv.aadaudience;
}
public set profileQueryEndpoint(endpoint: string) {
this._profileQueryEndpoint = endpoint;
this.correlationId = CorrelationIdManager.correlationIdPrefix;
}
public get profileQueryEndpoint() {
return this._profileQueryEndpoint;
}
public set instrumentationKey(iKey: string) {
if (!Config._validateInstrumentationKey(iKey)) {
Logging.warn("An invalid instrumentation key was provided. There may be resulting telemetry loss", this.instrumentationKey);
}
this._instrumentationKey = iKey;
}
public get instrumentationKey(): string {
return this._instrumentationKey;
}
public set webSnippetConnectionString(connectionString: string) {
this._webInstrumentationConnectionString = connectionString;
}
public get webSnippetConnectionString(): string {
return this._webInstrumentationConnectionString;
}
public set webInstrumentationConnectionString(connectionString: string) {
this._webInstrumentationConnectionString = connectionString;
}
public get webInstrumentationConnectionString() {
return this._webInstrumentationConnectionString;
}
private _mergeConfig() {
let jsonConfig = JsonConfig.getInstance();
this._connectionString = jsonConfig.connectionString;
this._instrumentationKey = jsonConfig.instrumentationKey;
this.correlationHeaderExcludedDomains = jsonConfig.correlationHeaderExcludedDomains;
this.correlationIdRetryIntervalMs = jsonConfig.correlationIdRetryIntervalMs;
this.disableAllExtendedMetrics = jsonConfig.disableAllExtendedMetrics;
this.disableAppInsights = jsonConfig.disableAppInsights;
this.disableStatsbeat = jsonConfig.disableStatsbeat;
this.distributedTracingMode = jsonConfig.distributedTracingMode;
this.enableAutoCollectConsole = jsonConfig.enableAutoCollectConsole;
this.enableLoggerErrorToTrace = jsonConfig.enableLoggerErrorToTrace;
this.enableAutoCollectDependencies = jsonConfig.enableAutoCollectDependencies;
this.enableAutoCollectIncomingRequestAzureFunctions = jsonConfig.enableAutoCollectIncomingRequestAzureFunctions;
this.enableAutoCollectExceptions = jsonConfig.enableAutoCollectExceptions;
this.enableAutoCollectExtendedMetrics = jsonConfig.enableAutoCollectExtendedMetrics;
this.enableAutoCollectExternalLoggers = jsonConfig.enableAutoCollectExternalLoggers;
this.enableAutoCollectHeartbeat = jsonConfig.enableAutoCollectHeartbeat;
this.enableAutoCollectPerformance = jsonConfig.enableAutoCollectPerformance;
this.enableAutoCollectPreAggregatedMetrics = jsonConfig.enableAutoCollectPreAggregatedMetrics;
this.enableAutoCollectRequests = jsonConfig.enableAutoCollectRequests;
this.enableAutoDependencyCorrelation = jsonConfig.enableAutoDependencyCorrelation;
this.enableInternalDebugLogging = jsonConfig.enableInternalDebugLogging;
this.enableInternalWarningLogging = jsonConfig.enableInternalWarningLogging;
this.enableResendInterval = jsonConfig.enableResendInterval;
this.enableMaxBytesOnDisk = jsonConfig.enableMaxBytesOnDisk;
this.enableSendLiveMetrics = jsonConfig.enableSendLiveMetrics;
this.enableUseAsyncHooks = jsonConfig.enableUseAsyncHooks;
this.enableUseDiskRetryCaching = jsonConfig.enableUseDiskRetryCaching;
this.endpointUrl = jsonConfig.endpointUrl;
this.extendedMetricDisablers = jsonConfig.extendedMetricDisablers;
this.ignoreLegacyHeaders = jsonConfig.ignoreLegacyHeaders;
this.maxBatchIntervalMs = jsonConfig.maxBatchIntervalMs;
this.maxBatchSize = jsonConfig.maxBatchSize;
this.proxyHttpUrl = jsonConfig.proxyHttpUrl;
this.proxyHttpsUrl = jsonConfig.proxyHttpsUrl;
this.quickPulseHost = jsonConfig.quickPulseHost;
this.samplingPercentage = jsonConfig.samplingPercentage;
this.enableWebInstrumentation = jsonConfig.enableWebInstrumentation;
this._webInstrumentationConnectionString = jsonConfig.webInstrumentationConnectionString;
this.webInstrumentationConfig = jsonConfig.webInstrumentationConfig;
this.webInstrumentationSrc = jsonConfig.webInstrumentationSrc;
}
/**
* Validate UUID Format
* Specs taken from breeze repo
* The definition of a VALID instrumentation key is as follows:
* Not none
* Not empty
* Every character is a hex character [0-9a-f]
* 32 characters are separated into 5 sections via 4 dashes
* First section has 8 characters
* Second section has 4 characters
* Third section has 4 characters
* Fourth section has 4 characters
* Fifth section has 12 characters
*/
private static _validateInstrumentationKey(iKey: string): boolean {
const UUID_Regex = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$";
const regexp = new RegExp(UUID_Regex);
return regexp.test(iKey);
}
}
export = Config;