block by bradoyler 84b0164be3922fa80d6d6bd0f4e79276

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 } }) 

appinsights-helper.ts