@@ -11,6 +11,27 @@ const appInsights = require('applicationinsights')
1111 * @typedef {import('applicationinsights').Contracts.TraceTelemetry } TraceTelemetry
1212 */
1313
14+ /**
15+ * Module-level client reference. In applicationinsights 3.x, defaultClient is read-only,
16+ * so we maintain our own reference to the configured client.
17+ *
18+ * @type {TelemetryClient | MockInsights | null }
19+ */
20+ let _client = null
21+
22+ /**
23+ * Mapping from KnownSeverityLevel string values to single-character abbreviations for console output.
24+ * In applicationinsights 3.x, severity is a string ('Verbose', 'Information', 'Warning', 'Error', 'Critical')
25+ * instead of a numeric enum.
26+ */
27+ const severityMap = {
28+ Verbose : 'V' ,
29+ Information : 'I' ,
30+ Warning : 'W' ,
31+ Error : 'E' ,
32+ Critical : 'C'
33+ }
34+
1435/**
1536 * Application Insights abstraction layer that provides a consistent interface for telemetry operations regardless of
1637 * whether Application Insights is configured. This wrapper class can operate in production with or without an actual
@@ -32,7 +53,17 @@ class MockInsights {
3253 }
3354
3455 /**
35- * Sets up the Application Insights abstraction layer. This method configures the global Application Insights client
56+ * Gets the configured telemetry client. Returns the module-level client that was set up
57+ * via the setup() method.
58+ *
59+ * @returns {TelemetryClient | MockInsights | null } The configured client or null if not set up
60+ */
61+ static getClient ( ) {
62+ return _client
63+ }
64+
65+ /**
66+ * Sets up the Application Insights abstraction layer. This method configures the telemetry client
3667 * based on the provided key, creating either a full Application Insights client or a console-based fallback wrapper.
3768 *
3869 * @param {string | null } [key=null] - Application Insights instrumentation key. If null, undefined, or 'mock', uses
@@ -42,12 +73,17 @@ class MockInsights {
4273 * @returns {void }
4374 */
4475 static setup ( key = null , echo = false ) {
45- // exit if we we are already setup
46- if ( appInsights . defaultClient instanceof MockInsights ) return
47- if ( ! key || key === 'mock' ) appInsights . defaultClient = /** @type {any } */ ( new MockInsights ( ) )
48- else {
76+ // exit if we are already setup
77+ if ( _client instanceof MockInsights ) return
78+ if ( ! key || key === 'mock' ) {
79+ _client = new MockInsights ( )
80+ } else {
4981 appInsights . setup ( key ) . setAutoCollectPerformance ( false ) . setAutoCollectDependencies ( true ) . start ( )
50- if ( echo ) appInsights . defaultClient = /** @type {any } */ ( new MockInsights ( appInsights . defaultClient ) )
82+ if ( echo ) {
83+ _client = new MockInsights ( appInsights . defaultClient )
84+ } else {
85+ _client = appInsights . defaultClient
86+ }
5187 }
5288 }
5389
@@ -73,11 +109,11 @@ class MockInsights {
73109 * @returns {void }
74110 */
75111 trackTrace ( traceTelemetry ) {
76- // const severities = ['Verbose', 'Info', 'Warning', 'Error', 'Critical'];
77- const severities = [ 'V' , 'I' , 'W' , 'E' , 'C' ]
78112 const hasProperties = traceTelemetry . properties && Object . keys ( traceTelemetry . properties ) . length > 0
79113 const propertyString = hasProperties ? `${ JSON . stringify ( traceTelemetry . properties ) } ` : ''
80- console . log ( `[${ severities [ traceTelemetry . severity ] } ] ${ traceTelemetry . message } ${ propertyString } ` )
114+ const severity = /** @type {keyof typeof severityMap } */ ( traceTelemetry . severity )
115+ const severityChar = severityMap [ severity ] || '?'
116+ console . log ( `[${ severityChar } ] ${ traceTelemetry . message } ${ propertyString } ` )
81117 if ( this . client ) this . client . trackTrace ( traceTelemetry )
82118 }
83119}
0 commit comments