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

Commit 90f3661

Browse files
committed
pipe device log messages to application insights
1 parent 876d7f1 commit 90f3661

File tree

3 files changed

+35
-15
lines changed

3 files changed

+35
-15
lines changed

packages/gateway/src/appinsights.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as mq from "./mq"
22
import * as appInsights from "applicationinsights"
33
import { ContextTagKeys } from "applicationinsights/out/Declarations/Contracts"
4-
import { registerMessageSink } from "./messages"
4+
import { registerLogSink, registerMessageSink } from "./messages"
55

66
// telemetry from devices
77
let _devsTelemetry: appInsights.TelemetryClient
@@ -14,12 +14,12 @@ export async function setup() {
1414

1515
appInsights
1616
.setup(connString)
17-
.setAutoDependencyCorrelation(true)
18-
.setAutoCollectRequests(true)
19-
.setAutoCollectPerformance(true, true)
17+
.setAutoDependencyCorrelation(false)
18+
.setAutoCollectRequests(false)
19+
.setAutoCollectPerformance(false)
2020
.setAutoCollectExceptions(true)
21-
.setAutoCollectDependencies(true)
22-
.setAutoCollectConsole(true, true)
21+
.setAutoCollectDependencies(false)
22+
.setAutoCollectConsole(false)
2323
.setUseDiskRetryCaching(true)
2424
.setAutoCollectPreAggregatedMetrics(true)
2525
.setSendLiveMetrics(false)
@@ -34,6 +34,7 @@ export async function setup() {
3434
name: "server.start",
3535
})
3636

37+
registerLogSink(async (logs, device) => device.traceTrace(logs.join("\n")))
3738
registerMessageSink({
3839
name: "app insights events",
3940
topicName: "tev",
@@ -51,7 +52,6 @@ export async function setup() {
5152
device.trackEvent(`devs.${name}`, { properties, measurements })
5253
},
5354
})
54-
5555
registerMessageSink({
5656
name: "app insights metrics",
5757
topicName: "tme",

packages/gateway/src/messages.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,12 @@ export async function ingestMessage(
5050
// dispatch
5151
await Promise.all(sinks.map(sink => sink.ingest(message, device)))
5252
}
53+
54+
export type LogSink = (logs: string[], device: ConnectedDevice) => Promise<void>
55+
const logSinks: LogSink[] = []
56+
export function registerLogSink(sink: LogSink) {
57+
logSinks.push(sink)
58+
}
59+
export async function ingestLogs(logs: string[], device: ConnectedDevice) {
60+
await Promise.all(logSinks.map(sink => sink(logs, device)))
61+
}

packages/gateway/src/wssk.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ import {
1717
MetricTelemetry,
1818
Telemetry,
1919
TelemetryType,
20+
TraceTelemetry,
2021
} from "applicationinsights/out/Declarations/Contracts"
2122
import { WsskCmd, WsskDataType } from "./interop"
22-
import { ingestMessage } from "./messages"
23+
import { ingestLogs, ingestMessage } from "./messages"
2324
import { WsskStreamingType } from "../../../devicescript/interop/src/interop"
2425

2526
const JD_AES_CCM_TAG_BYTES = 4
@@ -479,8 +480,12 @@ export class ConnectedDevice {
479480
case WsskCmd.Dmesg: {
480481
const { buf, lines } = splitLines(this.logBuffer, payload)
481482
this.logBuffer = buf
482-
if (lines.length)
483-
await this.notify({ type: "logs", logs: lines })
483+
if (lines.length) {
484+
await Promise.all([
485+
ingestLogs(lines, this),
486+
this.notify({ type: "logs", logs: lines }),
487+
])
488+
}
484489
}
485490
default:
486491
if (!(await this.deployStep(cmd, payload)))
@@ -565,11 +570,6 @@ export class ConnectedDevice {
565570
}
566571

567572
public trackMetric(name: string, options: Partial<MetricTelemetry>) {
568-
console.log(`metric`, {
569-
name,
570-
kind: "Aggregation",
571-
...options,
572-
})
573573
this.track(
574574
{
575575
name,
@@ -580,6 +580,17 @@ export class ConnectedDevice {
580580
)
581581
}
582582

583+
public traceTrace(message: string, options: Partial<TraceTelemetry> = {}) {
584+
if (!message) return
585+
this.track(
586+
{
587+
message,
588+
...options,
589+
},
590+
TelemetryType.Trace
591+
)
592+
}
593+
583594
private trackWarning(message: string) {
584595
this.trackEvent("warning", { properties: { message } })
585596
}

0 commit comments

Comments
 (0)