generated from unjs/template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6c876a7
commit 8372cb5
Showing
4 changed files
with
134 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,51 @@ | ||
import { defineEventHandler, getHeader, getCookie } from "h3" | ||
import Traceparent from "applicationinsights/out/Library/Traceparent" | ||
import { TelemetryClient } from "applicationinsights" | ||
import { useNitroApp } from "nitropack/dist/runtime/app" | ||
|
||
import { defineEventHandler, getHeader, getCookie } from "h3"; | ||
import Traceparent from "applicationinsights/out/Library/Traceparent"; | ||
import { TelemetryClient } from "applicationinsights"; | ||
import { useNitroApp } from "nitropack/dist/runtime/app"; | ||
|
||
export default defineEventHandler(async (event) => { | ||
const nitro = useNitroApp() | ||
const traceParent = getHeader(event, 'Traceparent') | ||
|
||
const trace = new Traceparent(traceParent) | ||
const client = new TelemetryClient() | ||
|
||
// context should contain Contract tags | ||
client.addTelemetryProcessor((envelope, context) => { | ||
if(context) { | ||
for (const [key, val] of Object.entries(context)) { | ||
envelope.tags[key] = val | ||
} | ||
} | ||
return true | ||
}) | ||
|
||
// initial traceId for this request | ||
trace.updateSpanId() | ||
|
||
// TODO get cookies list | ||
const aiUser = getCookie(event, 'ai_user') | ||
const aiSession = getCookie(event, 'ai_session') | ||
const aiDevice = getCookie(event, 'ai_device') | ||
|
||
Object.assign(client.context.tags, { | ||
[client.context.keys.sessionId]: aiSession, | ||
[client.context.keys.userId]: aiUser, | ||
[client.context.keys.deviceId]: aiDevice, | ||
}) | ||
|
||
await nitro.hooks.callHook('applicationinsights:context:tags', client, client.context.tags, { event }) | ||
|
||
event.__appInsights = { | ||
startTime: Date.now(), | ||
client, | ||
initialTrace: traceParent ?? trace.toString(), | ||
trace, | ||
properties: {} | ||
const nitro = useNitroApp(); | ||
const traceParent = getHeader(event, "Traceparent"); | ||
|
||
const trace = new Traceparent(traceParent); | ||
const client = new TelemetryClient(); | ||
|
||
// context should contain Contract tags | ||
client.addTelemetryProcessor((envelope, context) => { | ||
if (context) { | ||
for (const [key, val] of Object.entries(context)) { | ||
envelope.tags[key] = val; | ||
} | ||
} | ||
}) | ||
return true; | ||
}); | ||
|
||
// initial traceId for this request | ||
trace.updateSpanId(); | ||
|
||
// TODO get cookies list | ||
const aiUser = getCookie(event, "ai_user"); | ||
const aiSession = getCookie(event, "ai_session"); | ||
const aiDevice = getCookie(event, "ai_device"); | ||
|
||
Object.assign(client.context.tags, { | ||
[client.context.keys.sessionId]: aiSession, | ||
[client.context.keys.userId]: aiUser, | ||
[client.context.keys.deviceId]: aiDevice, | ||
}); | ||
|
||
await nitro.hooks.callHook( | ||
"applicationinsights:context:tags", | ||
client, | ||
client.context.tags, | ||
{ event }, | ||
); | ||
|
||
event.__appInsights = { | ||
startTime: Date.now(), | ||
client, | ||
initialTrace: traceParent ?? trace.toString(), | ||
trace, | ||
properties: {}, | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,25 @@ | ||
import { defineNitroPlugin } from "nitropack/dist/runtime/plugin"; | ||
import { setup } from "./setup" | ||
import { setup } from "./setup"; | ||
|
||
export default defineNitroPlugin(nitro => { | ||
setup() | ||
export default defineNitroPlugin((nitro) => { | ||
setup(); | ||
|
||
nitro.hooks.hook('render:response', (response, { event }) => { | ||
event.__appInsights.client.trackRequest({ | ||
name: `${event.method}: ${event.path}`, | ||
url: event.path, | ||
resultCode: response.statusCode ?? 0, | ||
duration: Date.now() - event.__appInsights.startTime, | ||
success: response.statusCode ? response.statusCode < 400 : false, | ||
properties: event.__appInsights.properties, | ||
contextObjects: { | ||
...event.__appInsights.client.context.tags, | ||
// needed ? | ||
// [event.__appInsights.client.context.keys.operationId]: event.__appInsights.trace.traceId, | ||
[event.__appInsights.client.context.keys.operationParentId]: event.__appInsights.trace.parentId, | ||
}, | ||
id: event.__appInsights.trace.traceId | ||
}) | ||
}) | ||
}) | ||
nitro.hooks.hook("render:response", (response, { event }) => { | ||
event.__appInsights.client.trackRequest({ | ||
name: `${event.method}: ${event.path}`, | ||
url: event.path, | ||
resultCode: response.statusCode ?? 0, | ||
duration: Date.now() - event.__appInsights.startTime, | ||
success: response.statusCode ? response.statusCode < 400 : false, | ||
properties: event.__appInsights.properties, | ||
contextObjects: { | ||
...event.__appInsights.client.context.tags, | ||
// needed ? | ||
// [event.__appInsights.client.context.keys.operationId]: event.__appInsights.trace.traceId, | ||
[event.__appInsights.client.context.keys.operationParentId]: | ||
event.__appInsights.trace.parentId, | ||
}, | ||
id: event.__appInsights.trace.traceId, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,46 @@ | ||
import * as applicationInsights from "applicationinsights" | ||
import * as applicationInsights from "applicationinsights"; | ||
|
||
export function setup() { | ||
// Setup Application Insights using the instrumentation key from the environment variables | ||
return applicationInsights | ||
.setup() | ||
.setAutoCollectRequests(Boolean(process.env.APPINSIGHTS_AUTO_COLLECT_REQUESTS)) | ||
.setAutoCollectConsole(Boolean(process.env.APPINSIGHTS_AUTO_COLLECT_CONSOLE)) | ||
.setAutoCollectDependencies(Boolean(process.env.APPINSIGHTS_AUTO_COLLECT_DEPENDENCIES)) | ||
.setAutoCollectExceptions(Boolean(process.env.APPINSIGHTS_AUTO_COLLECT_EXCEPTIONS)) | ||
.setAutoCollectPerformance(Boolean(process.env.APPINSIGHTS_AUTO_COLLECT_PERFORMANCE)) | ||
.setAutoCollectHeartbeat(Boolean(process.env.APPINSIGHTS_AUTO_COLLECT_HEARTBEAT)) | ||
.setAutoCollectIncomingRequestAzureFunctions(Boolean(process.env.APPINSIGHTS_AUTO_COLLECT_AZURE_FUNCTIONS)) | ||
.setAutoCollectPreAggregatedMetrics(Boolean(process.env.APPINSIGHTS_AUTO_COLLECT_PREAGGREGATEDMETRICS)) | ||
.setAutoDependencyCorrelation(Boolean(process.env.APPINSIGHTS_AUTO_COLLECT_DEPENDENCY_CORRELATION)) | ||
.enableWebInstrumentation(Boolean(process.env.APPINSIGHTS_ENABLE_WEB_INSTRUMENTATION)) | ||
.setDistributedTracingMode(applicationInsights.DistributedTracingModes.AI_AND_W3C) | ||
.setSendLiveMetrics(Boolean(process.env.APPINSIGHTS_LIVE_METRICS)) | ||
.setInternalLogging(Boolean(process.env.APPINSIGHTS_ENABLE_VERBOSE_LOGGING)) | ||
.setUseDiskRetryCaching(Boolean(process.env.APPINSIGHTS_USE_DISK_RETRY_CACHING)) | ||
.start() | ||
} | ||
// Setup Application Insights using the instrumentation key from the environment variables | ||
return applicationInsights | ||
.setup() | ||
.setAutoCollectRequests( | ||
Boolean(process.env.APPINSIGHTS_AUTO_COLLECT_REQUESTS), | ||
) | ||
.setAutoCollectConsole( | ||
Boolean(process.env.APPINSIGHTS_AUTO_COLLECT_CONSOLE), | ||
) | ||
.setAutoCollectDependencies( | ||
Boolean(process.env.APPINSIGHTS_AUTO_COLLECT_DEPENDENCIES), | ||
) | ||
.setAutoCollectExceptions( | ||
Boolean(process.env.APPINSIGHTS_AUTO_COLLECT_EXCEPTIONS), | ||
) | ||
.setAutoCollectPerformance( | ||
Boolean(process.env.APPINSIGHTS_AUTO_COLLECT_PERFORMANCE), | ||
) | ||
.setAutoCollectHeartbeat( | ||
Boolean(process.env.APPINSIGHTS_AUTO_COLLECT_HEARTBEAT), | ||
) | ||
.setAutoCollectIncomingRequestAzureFunctions( | ||
Boolean(process.env.APPINSIGHTS_AUTO_COLLECT_AZURE_FUNCTIONS), | ||
) | ||
.setAutoCollectPreAggregatedMetrics( | ||
Boolean(process.env.APPINSIGHTS_AUTO_COLLECT_PREAGGREGATEDMETRICS), | ||
) | ||
.setAutoDependencyCorrelation( | ||
Boolean(process.env.APPINSIGHTS_AUTO_COLLECT_DEPENDENCY_CORRELATION), | ||
) | ||
.enableWebInstrumentation( | ||
Boolean(process.env.APPINSIGHTS_ENABLE_WEB_INSTRUMENTATION), | ||
) | ||
.setDistributedTracingMode( | ||
applicationInsights.DistributedTracingModes.AI_AND_W3C, | ||
) | ||
.setSendLiveMetrics(Boolean(process.env.APPINSIGHTS_LIVE_METRICS)) | ||
.setInternalLogging(Boolean(process.env.APPINSIGHTS_ENABLE_VERBOSE_LOGGING)) | ||
.setUseDiskRetryCaching( | ||
Boolean(process.env.APPINSIGHTS_USE_DISK_RETRY_CACHING), | ||
) | ||
.start(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,25 @@ | ||
import type { TelemetryClient } from "applicationinsights"; | ||
import type Traceparent from "applicationinsights/out/Library/Traceparent"; | ||
import type { H3Event } from "h3" | ||
import type { H3Event } from "h3"; | ||
|
||
declare module 'h3' { | ||
interface H3Event { | ||
__appInsights: { | ||
startTime: number | ||
client: TelemetryClient | ||
trace: Traceparent | ||
initialTrace: string | ||
properties: Record<string, string> | ||
} | ||
} | ||
declare module "h3" { | ||
interface H3Event { | ||
__appInsights: { | ||
startTime: number; | ||
client: TelemetryClient; | ||
trace: Traceparent; | ||
initialTrace: string; | ||
properties: Record<string, string>; | ||
}; | ||
} | ||
} | ||
|
||
declare module 'nitropack/dist/runtime/types.d.ts' { | ||
interface NitroRuntimeHooks { | ||
'applicationinsights:context:tags': (client: TelemetryClient, tags: Record<string, string>, context: { event: H3Event }) => void | ||
} | ||
} | ||
declare module "nitropack/dist/runtime/types.d.ts" { | ||
interface NitroRuntimeHooks { | ||
"applicationinsights:context:tags": ( | ||
client: TelemetryClient, | ||
tags: Record<string, string>, | ||
context: { event: H3Event }, | ||
) => void; | ||
} | ||
} |