Skip to content

Commit 7e48a8a

Browse files
authored
Convert lib/mockInsights to TypeScript (#1536)
1 parent 8865af1 commit 7e48a8a

6 files changed

Lines changed: 97 additions & 185 deletions

File tree

‎lib/mockInsights.d.ts‎

Lines changed: 0 additions & 48 deletions
This file was deleted.

‎lib/mockInsights.js‎

Lines changed: 0 additions & 126 deletions
This file was deleted.

‎lib/mockInsights.ts‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright (c) Microsoft Corporation and others. Licensed under the MIT license.
2+
// SPDX-License-Identifier: MIT
3+
4+
import type { Contracts, TelemetryClient } from 'applicationinsights'
5+
import * as appInsights from 'applicationinsights'
6+
7+
/** Common interface for telemetry clients (real or mock). */
8+
export interface InsightsClient {
9+
trackException(telemetry: Contracts.ExceptionTelemetry): void
10+
trackTrace(telemetry: Contracts.TraceTelemetry): void
11+
}
12+
13+
/**
14+
* Module-level client reference. In applicationinsights 3.x, defaultClient is read-only,
15+
* so we maintain our own reference to the configured client.
16+
*/
17+
let _client: InsightsClient | null = null
18+
19+
/**
20+
* Mapping from KnownSeverityLevel string values to single-character abbreviations for console output.
21+
* In applicationinsights 3.x, severity is a string ('Verbose', 'Information', 'Warning', 'Error', 'Critical')
22+
* instead of a numeric enum.
23+
*/
24+
const severityMap = {
25+
Verbose: 'V',
26+
Information: 'I',
27+
Warning: 'W',
28+
Error: 'E',
29+
Critical: 'C'
30+
} as const
31+
32+
/**
33+
* Application Insights abstraction layer that provides a consistent interface for telemetry operations
34+
* regardless of whether Application Insights is configured. Provides console-based fallback logging
35+
* when no client is available.
36+
*/
37+
export class MockInsights implements InsightsClient {
38+
client: TelemetryClient | null
39+
40+
constructor(client: TelemetryClient | null = null) {
41+
this.client = client
42+
}
43+
44+
static getClient(): InsightsClient | null {
45+
return _client
46+
}
47+
48+
static setup(connectionString: string | null = null, echo = false): void {
49+
if (_client instanceof MockInsights) {
50+
return
51+
}
52+
if (!connectionString || connectionString === 'mock') {
53+
_client = new MockInsights()
54+
} else {
55+
appInsights
56+
.setup(connectionString ?? undefined)
57+
.setAutoCollectPerformance(false, false)
58+
.setAutoCollectDependencies(true)
59+
.start()
60+
if (echo) {
61+
_client = new MockInsights(appInsights.defaultClient)
62+
} else {
63+
_client = appInsights.defaultClient
64+
}
65+
}
66+
}
67+
68+
trackException(exceptionTelemetry: Contracts.ExceptionTelemetry): void {
69+
console.log('Exception: ')
70+
console.dir(exceptionTelemetry.exception)
71+
if (this.client) {
72+
this.client.trackException(exceptionTelemetry)
73+
}
74+
}
75+
76+
trackTrace(traceTelemetry: Contracts.TraceTelemetry): void {
77+
const hasProperties = traceTelemetry.properties && Object.keys(traceTelemetry.properties).length > 0
78+
const propertyString = hasProperties ? `${JSON.stringify(traceTelemetry.properties)}` : ''
79+
const severity = traceTelemetry.severity as keyof typeof severityMap
80+
const severityChar = severityMap[severity] || '?'
81+
console.log(`[${severityChar}] ${traceTelemetry.message}${propertyString}`)
82+
if (this.client) {
83+
this.client.trackTrace(traceTelemetry)
84+
}
85+
}
86+
}

‎providers/logging/winstonConfig.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import appInsights from 'applicationinsights'
55
import config from 'painless-config'
66
import winston from 'winston'
7-
import mockInsights from '../../lib/mockInsights.js'
7+
import { MockInsights } from '../../lib/mockInsights.ts'
88

99
const SENSITIVE_HEADERS = ['x-api-key', 'authorization', 'proxy-authorization', 'cookie']
1010

@@ -111,7 +111,7 @@ function factory(options) {
111111
...options
112112
}
113113

114-
mockInsights.setup(realOptions.connectionString || 'mock', realOptions.echo)
114+
MockInsights.setup(realOptions.connectionString || 'mock', realOptions.echo)
115115

116116
const logFormat = winston.format.combine(
117117
sanitizeMeta(),
@@ -145,7 +145,7 @@ function factory(options) {
145145
]
146146
})
147147

148-
const aiClient = mockInsights.getClient()
148+
const aiClient = MockInsights.getClient()
149149

150150
// Pipe Winston logs to Application Insights
151151
logger.on('data', info => {

‎test/providers/logging/winstonConfig.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import { expect } from 'chai'
55
import sinon from 'sinon'
6-
import MockInsights from '../../../lib/mockInsights.js'
6+
import { MockInsights } from '../../../lib/mockInsights.ts'
77
import winstonFactory, {
88
buildProperties,
99
sanitizeHeaders,

‎tsconfig.json‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
"app.d.ts",
1515
"app.js",
1616
"bin/www",
17-
"bin/**/*.{js,d.ts}",
18-
"business/**/*.{js,d.ts}",
19-
"lib/**/*.{js,d.ts}",
20-
"middleware/**/*.{js,d.ts}",
21-
"providers/**/*.{js,d.ts}",
22-
"routes/**/*.{js,d.ts}",
23-
"schemas/**/*.{js,d.ts}"
17+
"bin/**/*.{js,ts,d.ts}",
18+
"business/**/*.{js,ts,d.ts}",
19+
"lib/**/*.{js,ts,d.ts}",
20+
"middleware/**/*.{js,ts,d.ts}",
21+
"providers/**/*.{js,ts,d.ts}",
22+
"routes/**/*.{js,ts,d.ts}",
23+
"schemas/**/*.{js,ts,d.ts}"
2424
]
2525
}

0 commit comments

Comments
 (0)