|
| 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 | +} |
0 commit comments