-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathappInsight-api.js
38 lines (36 loc) · 1.32 KB
/
appInsight-api.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
import * as appInsights from "applicationinsights";
if (process.env.NEXT_PUBLIC_APP_INSIGHT_CONNECTION_STRING) {
try {
appInsights
.setup(process.env.NEXT_PUBLIC_APP_INSIGHT_CONNECTION_STRING)
.setAutoCollectConsole(true)
.setAutoCollectExceptions(true)
.setAutoCollectRequests(true)
.setDistributedTracingMode(appInsights.DistributedTracingModes.AI_AND_W3C)
.setSendLiveMetrics(true);
appInsights.defaultClient?.addTelemetryProcessor((envelope, context) => {
if (envelope.data.baseType === "RequestData") {
envelope.data.baseData.properties ??= {};
if (context["http.ServerRequest"]?.headers["user-agent"])
envelope.data.baseData.properties.userAgent =
context["http.ServerRequest"].headers["user-agent"];
}
});
appInsights.start();
if (appInsights.defaultClient) {
console.log("✅ App Insights - Server Side logging is turned on!");
} else {
console.error("❌ App Insights - Failed to initialize!");
}
} catch (error) {
console.error(
"🚨 App Insights - An error occurred while initializing:",
error
);
}
} else {
console.log(
"🚨 App Insights - Server Side logging is not turned on, Please add NEXT_PUBLIC_APP_INSIGHT_CONNECTION_STRING variable!"
);
}
export default appInsights;