Repository navigation
Expand file tree
/
Copy pathappInsight-api.js
More file actions
71 lines (63 loc) · 2.91 KB
/
Copy pathappInsight-api.js
File metadata and controls
71 lines (63 loc) · 2.91 KB
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
import * as appInsights from "applicationinsights";
if (process.env.NEXT_PUBLIC_APP_INSIGHT_CONNECTION_STRING) {
try {
// Configuration options with defaults for cost optimization
const samplingPercentageRaw = parseFloat(
process.env.APPINSIGHTS_SAMPLING_PERCENTAGE || "20"
);
// Validate sampling percentage is between 1 and 100, default to 20 if invalid
const samplingPercentage =
!isNaN(samplingPercentageRaw) &&
samplingPercentageRaw >= 1 &&
samplingPercentageRaw <= 100
? samplingPercentageRaw
: 20;
const enableConsoleCollection =
process.env.APPINSIGHTS_ENABLE_CONSOLE_COLLECTION === "true"; // Default: false
const collectNativeConsoleMethods =
process.env.APPINSIGHTS_COLLECT_NATIVE_CONSOLE === "true"; // Default: false
const enableLiveMetrics =
process.env.APPINSIGHTS_ENABLE_LIVE_METRICS === "true"; // Default: false
appInsights
.setup(process.env.NEXT_PUBLIC_APP_INSIGHT_CONNECTION_STRING)
.setAutoCollectConsole(enableConsoleCollection, collectNativeConsoleMethods) // First param: enable collection, Second param: include native console.log etc.
.setAutoCollectExceptions(true) // Always track exceptions
.setAutoCollectRequests(true)
.setDistributedTracingMode(appInsights.DistributedTracingModes.AI_AND_W3C)
.setSendLiveMetrics(enableLiveMetrics);
// Apply sampling configuration before starting
if (appInsights.defaultClient) {
appInsights.defaultClient.config.samplingPercentage = samplingPercentage;
}
appInsights.start();
// Add telemetry processor to capture user-agent on requests
if (appInsights.defaultClient) {
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"];
}
return true;
});
console.log("✅ App Insights - Server Side logging is turned on!");
console.log(` 📊 Sampling: ${samplingPercentage}%`);
console.log(` 🔍 Console Collection: ${enableConsoleCollection ? "Enabled" : "Disabled"}`);
console.log(` 📝 Native Console Methods: ${collectNativeConsoleMethods ? "Tracked" : "Not tracked"}`);
console.log(` 📡 Live Metrics: ${enableLiveMetrics ? "Enabled" : "Disabled"}`);
} 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;