Skip to content

Instantly share code, notes, and snippets.

@bradoyler
Last active March 24, 2020 14:49
Show Gist options
  • Save bradoyler/84b0164be3922fa80d6d6bd0f4e79276 to your computer and use it in GitHub Desktop.
Save bradoyler/84b0164be3922fa80d6d6bd0f4e79276 to your computer and use it in GitHub Desktop.
Application Insights helper for typescript function apps

AppInsights helper

a typescript wrapper for Application Insights

Examples

Exception tracking

import { trackException } from './appinsights-helper'

trackException({ exception: error })

Trace tracking

trackTrace({ message: 'some message' ,  severity:  1 })
// severityLevels: Verbose = 0, Information = 1, Warning = 2, Error = 3, Critical = 4,

Advanced usage

HTTP request tracking:

import { markDependency, measureDependency } from './appinsights-helper'

const marker = markDependency('cosmos', 'query collection')
try {
  const url = 'https://domain.com/api/books'
  const locations = await requestPromise(url)
  measureDependency(marker, url)
} catch(ex) {
  measureDependency(marker, url, false)
  // could also trackException(ex) here
}

Cosmos query tracking:

dependency tracking is useful for measuring latency of remote calls (SQL, HTTP) and also failures

import { markDependency, measureDependency } from './appinsights-helper'

const marker = markDependency('cosmos', 'query collection')
try {
  const sql = 'select top 100 from c'
  const locations = await cosmosDb.items.query(sql) // pseudo-code cosmos query
  measureDependency(marker, sql)
} catch(ex) {
  measureDependency(marker, sql, false)
  // could also trackException(ex) here
}

Debug Event tracking

customEvents are powerful because you can query the logs by the customDimensions object

import { trackDebugEvent } from from './appinsights-helper'

// This 'debug' event will only log if the DEBUG_INSIGHTS="true" env var is set
trackDebugEvent({ name: 'new order',  properties: { order }, measurements: { productCount: order.products.length } }) 
import * as appInsights from 'applicationinsights'
import { EventTelemetry, DependencyTelemetry, ExceptionTelemetry, MetricTelemetry, RequestTelemetry, TraceTelemetry } from 'applicationinsights/out/Declarations/Contracts'
appInsights.setup(process.env.APPINSIGHTS_INSTRUMENTATIONKEY).start()
export const aiClient = appInsights.defaultClient
aiClient.context.tags[aiClient.context.keys.cloudRole] = process.env.WEBSITE_SITE_NAME || 'defaultCloudRole'
const debugInsightsEnabled = (process.env.DEBUG_INSIGHTS === 'true') || false
export function trackEvent (telemetry: EventTelemetry): void { aiClient.trackEvent(telemetry) }
export function trackException (telemetry: ExceptionTelemetry): void { aiClient.trackException(telemetry) }
export function trackDependency (telemetry: DependencyTelemetry): void { aiClient.trackDependency(telemetry) }
export function trackTrace (telemetry: TraceTelemetry): void { aiClient.trackTrace(telemetry) }
export function trackRequest (telemetry: RequestTelemetry): void { aiClient.trackRequest(telemetry) }
export function trackMetric (telemetry: MetricTelemetry): void { aiClient.trackMetric(telemetry) }
interface IMarker {
dependencyTypeName: string; // HTTP, SQL, etc
name: string;
startTime: number;
}
export function trackDebugEvent (telemetry: EventTelemetry): void {
if (debugInsightsEnabled) {
trackEvent(telemetry)
}
}
export function markDependency (dependencyTypeName: string, name: string): IMarker {
const startTime = Date.now()
return { dependencyTypeName, name, startTime }
}
export function measureDependency (marker: IMarker, data = '', success = true): void {
const { startTime, dependencyTypeName, name } = marker
const duration = Date.now() - startTime
const telemetry = { dependencyTypeName, name, duration, data, success } as DependencyTelemetry
trackDependency(telemetry)
}
@bradoyler
Copy link
Author

bradoyler commented Jan 26, 2020

Overview of Application Insights Telemetry

image

Tables we care about for func apps...

  • traces : context.log() & warn() & error()
  • requests
  • exceptions
  • dependencies: remote db & http calls
  • customEvents: allows use of logging JSON via { properties } that can be queried in Log Analytics
  • customMetrics

image

@bradoyler
Copy link
Author

bradoyler commented Feb 14, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment