forked from onebeyond/systemic-azure-metrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
94 lines (80 loc) · 2.95 KB
/
index.js
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
const debug = require('debug')('systemic-azure-metrics');
const appInsights = require('applicationinsights');
const { dummyClient } = require('./dummyClient');
module.exports = () => {
// samplingPercentage, disableAppInsights, etc
const setupConfig = (client) => (property, value) => {
debug(`Setting up config: ${property} = ${value}`);
client.config.property = value;
};
const setupTag = (client) => (property, value) => {
debug(`Setting up context tag: ${property} = ${value}`);
client.context.tags[property] = value;
};
const start = async ({ config }) => {
const {
key,
internalLogging = false,
liveMetrics = true,
traceW3C = false,
ignoreURI = ['/__/manifest'],
insightsConfig = {},
autoDependencyCorrelation = true,
context: {
tags = {}
} = {},
autoCollect: {
requests = true,
performance = true,
extendedPerformance = true,
exceptions = true,
dependencies = true,
console = true
} = {}
} = config;
if (!key) throw new Error('No insights key has been provided!');
const { disableAppInsights } = insightsConfig;
// appInsights is NEVER 100% disabled unless we use this hack; eg:
// - complains about wrong instrumentation keys
// - creates correlationKeys POSTing to Azure endpoint
// if you know of any better approach, please shout!
if (disableAppInsights) {
debug('appInsights disabled: returning dummy client');
return dummyClient;
}
const requestURIFilter = (envelope) => {
const { data: { baseType, baseData } } = envelope;
if (baseType === 'RequestData') {
const [, uri] = baseData.name.split(/[ ,]+/);
const shouldIgnore = ignoreURI.some(ignored => ignored === uri);
if (shouldIgnore) {
debug(`Ignoring URI ${uri}`);
}
return !shouldIgnore;
}
};
const distributedTracingMode = traceW3C
? appInsights.DistributedTracingModes.AI_AND_W3C
: appInsights.DistributedTracingModes.AI;
appInsights
.setup(key)
.setAutoDependencyCorrelation(autoDependencyCorrelation)
.setInternalLogging(internalLogging)
.setAutoCollectRequests(requests)
.setAutoCollectPerformance(performance, extendedPerformance)
.setAutoCollectExceptions(exceptions)
.setAutoCollectDependencies(dependencies)
.setAutoCollectConsole(console, console)
.setSendLiveMetrics(liveMetrics)
.setDistributedTracingMode(distributedTracingMode)
.start();
const { defaultClient } = appInsights;
const configureClient = setupConfig(defaultClient);
Object.keys(insightsConfig).forEach((key) => configureClient(key, insightsConfig[key]));
defaultClient.addTelemetryProcessor(requestURIFilter);
const configureTag = setupTag(defaultClient);
Object.keys(tags).forEach((key) => configureTag(key, tags[key]));
return defaultClient;
};
return { start };
};