Skip to content

Commit

Permalink
chore: apply automated fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
autofix-ci[bot] committed Sep 23, 2023
1 parent 6c876a7 commit 8372cb5
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 101 deletions.
92 changes: 48 additions & 44 deletions src/middleware.ts
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: {},
};
});
43 changes: 22 additions & 21 deletions src/plugin.ts
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,
});
});
});
64 changes: 44 additions & 20 deletions src/setup.ts
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();
}
36 changes: 20 additions & 16 deletions src/types.d.ts
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;
}
}

0 comments on commit 8372cb5

Please sign in to comment.