Skip to content

Commit

Permalink
I've tried to adapt this from the template project implementation. Ho…
Browse files Browse the repository at this point in the history
…wever it has:

- no user information
- no tests
So I've blended the two, but feel like I could do with a really thorough review of this
  • Loading branch information
joelstobart-moj committed Apr 27, 2024
1 parent 2b9e46d commit f93a8ac
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
7 changes: 4 additions & 3 deletions server/utils/azureAppInsights.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DataTelemetry, EnvelopeTelemetry } from 'applicationinsights/out/Declarations/Contracts'
import { TelemetryItem } from 'applicationinsights/out/src/declarations/generated'
import { MonitorBase } from 'applicationinsights/out/src/declarations/generated/models'
import { addUserDataToRequests, ContextObject } from './azureAppInsights'

const user = {
Expand All @@ -10,8 +11,8 @@ const createEnvelope = (properties: Record<string, string | boolean>, baseType =
data: {
baseType,
baseData: { properties },
} as DataTelemetry,
}) as EnvelopeTelemetry
} as MonitorBase,
}) as TelemetryItem

const createContext = (username: string) =>
({
Expand Down
18 changes: 14 additions & 4 deletions server/utils/azureAppInsights.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Contracts, setup, defaultClient, TelemetryClient, DistributedTracingModes } from 'applicationinsights'
import { EnvelopeTelemetry } from 'applicationinsights/out/Declarations/Contracts'
import { defaultClient, DistributedTracingModes, setup, TelemetryClient } from 'applicationinsights'
import { TelemetryItem } from 'applicationinsights/out/src/declarations/generated'
import type { ApplicationInfo } from '../applicationInfo'

export type ContextObject = {
Expand All @@ -23,14 +23,24 @@ export function buildAppInsightsClient(
if (process.env.APPLICATIONINSIGHTS_CONNECTION_STRING) {
defaultClient.context.tags['ai.cloud.role'] = overrideName || applicationName
defaultClient.context.tags['ai.application.ver'] = buildNumber

defaultClient.addTelemetryProcessor(addUserDataToRequests)

defaultClient.addTelemetryProcessor(({ tags, data }, contextObjects) => {
const operationNameOverride = contextObjects.correlationContext?.customProperties?.getProperty('operationName')
if (operationNameOverride) {
tags['ai.operation.name'] = data.baseData.name = operationNameOverride // eslint-disable-line no-param-reassign,no-multi-assign
}
return true
})

return defaultClient
}
return null
}

export function addUserDataToRequests(envelope: EnvelopeTelemetry, contextObjects: ContextObject | undefined): boolean {
const isRequest = envelope.data.baseType === Contracts.TelemetryTypeString.Request
export function addUserDataToRequests(envelope: TelemetryItem, contextObjects: ContextObject | undefined): boolean {
const isRequest = envelope.data.baseType === 'RequestData'
if (isRequest) {
const { username } = contextObjects?.['http.ServerRequest']?.res?.locals?.user || {}
if (username) {
Expand Down

0 comments on commit f93a8ac

Please sign in to comment.