|
3 | 3 |
|
4 | 4 | const appInsights = require('applicationinsights') |
5 | 5 |
|
| 6 | +/** |
| 7 | + * @typedef {import('applicationinsights').TelemetryClient} TelemetryClient |
| 8 | + * |
| 9 | + * @typedef {import('applicationinsights').Contracts.ExceptionTelemetry} ExceptionTelemetry |
| 10 | + * |
| 11 | + * @typedef {import('applicationinsights').Contracts.TraceTelemetry} TraceTelemetry |
| 12 | + */ |
| 13 | + |
| 14 | +/** |
| 15 | + * Mock implementation of Application Insights client for testing and development. This class provides a lightweight |
| 16 | + * alternative to the full Application Insights client, allowing for console-based logging of telemetry data during |
| 17 | + * development and testing. |
| 18 | + */ |
6 | 19 | class MockInsights { |
| 20 | + /** |
| 21 | + * Creates a new MockInsights instance |
| 22 | + * |
| 23 | + * @param {TelemetryClient | null} [client=null] - Optional Application Insights client to wrap. Default is `null` |
| 24 | + */ |
7 | 25 | constructor(client = null) { |
| 26 | + /** |
| 27 | + * The underlying Application Insights client, if any |
| 28 | + * |
| 29 | + * @type {TelemetryClient | null} |
| 30 | + */ |
8 | 31 | this.client = client |
9 | 32 | } |
10 | 33 |
|
| 34 | + /** |
| 35 | + * Sets up the mock insights client or configures Application Insights. This method configures the global Application |
| 36 | + * Insights client based on the provided key. |
| 37 | + * |
| 38 | + * @param {string | null} [key=null] - Application Insights instrumentation key or 'mock' for testing. Default is |
| 39 | + * `null` |
| 40 | + * @param {boolean} [echo=false] - Whether to echo telemetry to both mock and real client. Default is `false` |
| 41 | + * @returns {void} |
| 42 | + */ |
11 | 43 | static setup(key = null, echo = false) { |
12 | 44 | // exit if we we are already setup |
13 | 45 | if (appInsights.defaultClient instanceof MockInsights) return |
14 | | - if (!key || key === 'mock') appInsights.defaultClient = new MockInsights() |
| 46 | + if (!key || key === 'mock') appInsights.defaultClient = /** @type {any} */ (new MockInsights()) |
15 | 47 | else { |
16 | 48 | appInsights.setup(key).setAutoCollectPerformance(false).setAutoCollectDependencies(true).start() |
17 | | - if (echo) appInsights.defaultClient = new MockInsights(appInsights.defaultClient) |
| 49 | + if (echo) appInsights.defaultClient = /** @type {any} */ (new MockInsights(appInsights.defaultClient)) |
18 | 50 | } |
19 | 51 | } |
20 | 52 |
|
| 53 | + /** |
| 54 | + * Tracks an exception by logging it to the console and optionally forwarding to Application Insights. |
| 55 | + * |
| 56 | + * @param {ExceptionTelemetry} exceptionTelemetry - The exception telemetry data |
| 57 | + * @returns {void} |
| 58 | + */ |
21 | 59 | trackException(exceptionTelemetry) { |
22 | 60 | console.log('Exception: ') |
23 | 61 | console.dir(exceptionTelemetry.exception) |
24 | | - if (this.client) this.client.trackException(exceptionTelemetry) |
| 62 | + if (this.client) this.client.trackException(/** @type {ExceptionTelemetry} */ (exceptionTelemetry)) |
25 | 63 | } |
26 | 64 |
|
| 65 | + /** |
| 66 | + * Tracks a trace message by logging it to the console and optionally forwarding to Application Insights. The trace is |
| 67 | + * formatted with severity level indicators for easy console reading. |
| 68 | + * |
| 69 | + * @param {TraceTelemetry} traceTelemetry - The trace telemetry data |
| 70 | + * @returns {void} |
| 71 | + */ |
27 | 72 | trackTrace(traceTelemetry) { |
28 | 73 | // const severities = ['Verbose', 'Info', 'Warning', 'Error', 'Critical']; |
29 | 74 | const severities = ['V', 'I', 'W', 'E', 'C'] |
30 | 75 | const hasProperties = traceTelemetry.properties && Object.keys(traceTelemetry.properties).length > 0 |
31 | 76 | const propertyString = hasProperties ? `${JSON.stringify(traceTelemetry.properties)}` : '' |
32 | 77 | console.log(`[${severities[traceTelemetry.severity]}] ${traceTelemetry.message}${propertyString}`) |
33 | | - if (this.client) this.client.trackTrace(traceTelemetry) |
| 78 | + if (this.client) this.client.trackTrace(/** @type {TraceTelemetry} */ (traceTelemetry)) |
34 | 79 | } |
35 | 80 | } |
36 | 81 | module.exports = MockInsights |
0 commit comments