Skip to content
This repository was archived by the owner on Aug 5, 2024. It is now read-only.

Commit 58a03f5

Browse files
committed
optional app insights
1 parent 74e2cf4 commit 58a03f5

File tree

5 files changed

+16
-7
lines changed

5 files changed

+16
-7
lines changed

src/appinsights.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1+
import * as mq from "./mq"
12
import * as appInsights from "applicationinsights"
23
import { ContextTagKeys } from "applicationinsights/out/Declarations/Contracts"
34
import { registerMessageSink } from "./messages"
45

56
// telemetry from devices
67
let _devsTelemetry: appInsights.TelemetryClient
78
export async function setup() {
9+
const connString = process.env.APPLICATIONINSIGHTS_CONNECTION_STRING
10+
if (!connString) {
11+
console.log(`no application insights connection string, skipping`)
12+
return
13+
}
14+
815
appInsights
9-
.setup()
16+
.setup(connString)
1017
.setAutoDependencyCorrelation(true)
1118
.setAutoCollectRequests(true)
1219
.setAutoCollectPerformance(true, true)
@@ -81,7 +88,7 @@ export async function setup() {
8188
}
8289

8390
export function serverTelemetry(): appInsights.TelemetryClient {
84-
return appInsights.defaultClient
91+
return _devsTelemetry ? appInsights.defaultClient : undefined
8592
}
8693

8794
export function devsTelemetry(): appInsights.TelemetryClient {

src/eventhub.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export async function setup() {
3636
correlationId,
3737
})
3838
) {
39-
serverTelemetry().trackEvent({
39+
serverTelemetry()?.trackEvent({
4040
name: "messages.eventhub.push.fail",
4141
})
4242
} else await producer.sendBatch(batch)

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ async function main() {
152152
const host = "0.0.0.0"
153153
server.listen({ port, host }, err => {
154154
if (err) {
155-
appinsights.serverTelemetry().trackException({ exception: err })
155+
appinsights.serverTelemetry()?.trackException({ exception: err })
156156
console.error(err)
157157
process.exit(1)
158158
}

src/wssk.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ export class ConnectedDevice {
371371
} else this.setDeploy(tmp)
372372
}
373373
} catch (e: any) {
374-
serverTelemetry().trackException({ exception: e })
374+
serverTelemetry()?.trackException({ exception: e })
375375
}
376376
}
377377

@@ -586,6 +586,8 @@ export class ConnectedDevice {
586586

587587
private track(telemetry: Telemetry, telemetryType: TelemetryType) {
588588
const dt = devsTelemetry()
589+
if (!dt) return
590+
589591
const { tagOverrides = {}, ...rest } = telemetry
590592
const {
591593
productId,
@@ -807,7 +809,7 @@ export async function wsskInit(server: FastifyInstance) {
807809
}
808810
} catch (e: any) {
809811
log.error(`message handler: ${e.stack}`)
810-
serverTelemetry().trackException({ exception: e })
812+
serverTelemetry()?.trackException({ exception: e })
811813
}
812814
})
813815

0 commit comments

Comments
 (0)