@duffcloudservices/telemetry 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/config.ts ADDED
@@ -0,0 +1,94 @@
1
+ import type { ApplicationInsightsConfig, TelemetryConfigOptions } from './types'
2
+
3
+ /**
4
+ * The page-unload events the DCS default config tells the App Insights web SDK
5
+ * to leave un-hooked. Only the deprecated `unload` event is disabled — the SDK
6
+ * still hooks `pagehide` / `visibilitychange` (the supported flush path), so no
7
+ * telemetry is lost, back/forward-cache eligibility is restored, and the
8
+ * Lighthouse "avoid `unload` event listeners" audit passes.
9
+ */
10
+ export const DEFAULT_DISABLE_PAGE_UNLOAD_EVENTS: readonly string[] = ['unload']
11
+
12
+ /** Cloud-role default when a consumer does not name itself. */
13
+ export const DEFAULT_CLOUD_ROLE = 'dcs-site'
14
+
15
+ /** Correlation is added to first-party requests to these domains. */
16
+ const DEFAULT_CORRELATION_HEADER_DOMAINS: readonly string[] = [
17
+ 'localhost',
18
+ '127.0.0.1',
19
+ '*.duffcloudservices.com',
20
+ ]
21
+
22
+ /** Correlation headers are never attached to these third-party domains. */
23
+ const DEFAULT_CORRELATION_HEADER_EXCLUDED_DOMAINS: readonly string[] = [
24
+ '*.googleapis.com',
25
+ '*.stripe.com',
26
+ '*.auth0.com',
27
+ '*.linkedin.com',
28
+ '*.github.com',
29
+ '*.microsoft.com',
30
+ '*.microsoftonline.com',
31
+ ]
32
+
33
+ /**
34
+ * Build the App Insights web SDK config with DCS's shared defaults.
35
+ *
36
+ * The DEFAULT always includes {@link DEFAULT_DISABLE_PAGE_UNLOAD_EVENTS}
37
+ * (`disablePageUnloadEvents: ['unload']`) — the fleet-wide bfcache / Lighthouse
38
+ * best-practices fix. Callers only need to supply their connection string; every
39
+ * field is overridable via the typed options (and the escape-hatch `overrides`).
40
+ *
41
+ * This function performs no SDK import and reads no `import.meta.env` — it is
42
+ * pure, so it is safe to call from lazy/inert telemetry loaders (the SDK is only
43
+ * pulled in when the composable, or the consumer, actually constructs
44
+ * `ApplicationInsights`).
45
+ */
46
+ export function createTelemetryConfig(
47
+ options: TelemetryConfigOptions = {},
48
+ ): ApplicationInsightsConfig {
49
+ const {
50
+ connectionString,
51
+ instrumentationKey,
52
+ enableAutoRouteTracking = true,
53
+ dev = false,
54
+ disablePageUnloadEvents = [...DEFAULT_DISABLE_PAGE_UNLOAD_EVENTS],
55
+ overrides,
56
+ } = options
57
+
58
+ const config: ApplicationInsightsConfig = {
59
+ connectionString,
60
+ instrumentationKey,
61
+ // ── BP-54 fleet fix ──────────────────────────────────────────────────
62
+ // Never register a listener on the deprecated `unload` event. Keeping it
63
+ // out restores back/forward-cache eligibility and clears the Lighthouse
64
+ // "avoid unload event listeners" audit (~19 best-practices points). The
65
+ // SDK still flushes on pagehide/visibilitychange, so telemetry is intact.
66
+ disablePageUnloadEvents: [...disablePageUnloadEvents],
67
+ enableAutoRouteTracking,
68
+ enableCorsCorrelation: true,
69
+ enableRequestHeaderTracking: true,
70
+ enableResponseHeaderTracking: true,
71
+ autoTrackPageVisitTime: true,
72
+ enableAjaxErrorStatusText: true,
73
+ enableAjaxPerfTracking: true,
74
+ disableExceptionTracking: false,
75
+ disableTelemetry: false,
76
+ loggingLevelConsole: dev ? 1 : 2,
77
+ loggingLevelTelemetry: dev ? 1 : 2,
78
+ samplingPercentage: 100,
79
+ isCookieUseDisabled: false,
80
+ distributedTracingMode: 2,
81
+ correlationHeaderDomains: [...DEFAULT_CORRELATION_HEADER_DOMAINS],
82
+ correlationHeaderExcludedDomains: [...DEFAULT_CORRELATION_HEADER_EXCLUDED_DOMAINS],
83
+ ...overrides,
84
+ }
85
+
86
+ // Re-assert the unload fix AFTER the overrides spread so a caller that passes
87
+ // `overrides` can never accidentally re-enable the deprecated `unload` listener
88
+ // and regress the fleet-wide BP-54 / bfcache fix. A caller that genuinely needs
89
+ // a different unload policy sets `disablePageUnloadEvents` via the dedicated
90
+ // option (applied above), not through `overrides`.
91
+ config.disablePageUnloadEvents = [...disablePageUnloadEvents]
92
+
93
+ return config
94
+ }
package/src/index.ts ADDED
@@ -0,0 +1,27 @@
1
+ /**
2
+ * `@duffcloudservices/telemetry` — shared Azure Application Insights telemetry
3
+ * for DCS Vue sites.
4
+ *
5
+ * - {@link useTelemetry} — the full composable (init + track* API). Statically
6
+ * imports the App Insights web SDK, so `initialize()` is synchronous.
7
+ * - {@link createTelemetryConfig} — the shared App Insights config, with the
8
+ * `disablePageUnloadEvents: ['unload']` bfcache/Lighthouse fix baked into the
9
+ * default. Pure (no SDK import); also available SDK-free at
10
+ * `@duffcloudservices/telemetry/config` for lazy/inert telemetry loaders.
11
+ */
12
+ export { useTelemetry, type TelemetryApi } from './composable'
13
+ export {
14
+ createTelemetryConfig,
15
+ DEFAULT_CLOUD_ROLE,
16
+ DEFAULT_DISABLE_PAGE_UNLOAD_EVENTS,
17
+ } from './config'
18
+ export type {
19
+ ApplicationInsightsConfig,
20
+ TelemetryConfigOptions,
21
+ TelemetryDependency,
22
+ TelemetryEvent,
23
+ TelemetryException,
24
+ TelemetryMetric,
25
+ TelemetryOptions,
26
+ TelemetryPageView,
27
+ } from './types'
package/src/types.ts ADDED
@@ -0,0 +1,103 @@
1
+ import type { IConfig, IConfiguration } from '@microsoft/applicationinsights-web'
2
+
3
+ /**
4
+ * The concrete config shape accepted by the App Insights web SDK's
5
+ * `ApplicationInsights` constructor (`{ config }`). The SDK types the field as
6
+ * `IConfiguration & IConfig`; `disablePageUnloadEvents` lives on `IConfiguration`.
7
+ */
8
+ export type ApplicationInsightsConfig = IConfiguration & IConfig
9
+
10
+ export interface TelemetryEvent {
11
+ name: string
12
+ properties?: Record<string, string>
13
+ measurements?: Record<string, number>
14
+ }
15
+
16
+ export interface TelemetryPageView {
17
+ name?: string
18
+ uri?: string
19
+ properties?: Record<string, string>
20
+ measurements?: Record<string, number>
21
+ }
22
+
23
+ export interface TelemetryException {
24
+ exception: Error
25
+ properties?: Record<string, string>
26
+ measurements?: Record<string, number>
27
+ }
28
+
29
+ export interface TelemetryDependency {
30
+ name: string
31
+ data: string
32
+ duration: number
33
+ success: boolean
34
+ resultCode?: number
35
+ properties?: Record<string, string>
36
+ measurements?: Record<string, number>
37
+ }
38
+
39
+ export interface TelemetryMetric {
40
+ name: string
41
+ average: number
42
+ sampleCount?: number
43
+ min?: number
44
+ max?: number
45
+ properties?: Record<string, string>
46
+ }
47
+
48
+ /**
49
+ * Options that shape the App Insights *config object* — everything
50
+ * {@link createTelemetryConfig} needs. All fields are per-consumer variance;
51
+ * the package supplies the shared DCS defaults (including the unload fix).
52
+ */
53
+ export interface TelemetryConfigOptions {
54
+ /** App Insights connection string (preferred). Sourced from the consumer's env. */
55
+ connectionString?: string
56
+ /** Legacy instrumentation key, if a connection string is not available. */
57
+ instrumentationKey?: string
58
+ /** Enable the SDK's built-in SPA route tracking. Default `true`. */
59
+ enableAutoRouteTracking?: boolean
60
+ /**
61
+ * Dev mode — routes internal SDK logs more verbosely to the console
62
+ * (`loggingLevel* = 1` vs `2`). The consumer passes `import.meta.env.DEV`.
63
+ * Default `false`.
64
+ */
65
+ dev?: boolean
66
+ /**
67
+ * Page-unload events the SDK must NOT hook. Defaults to `['unload']`.
68
+ *
69
+ * This is the fleet-wide "BP-54" fix: the App Insights web SDK otherwise
70
+ * registers a listener on the deprecated `unload` event, which (a) blocks
71
+ * back/forward-cache eligibility and (b) trips the Lighthouse
72
+ * "avoid `unload` event listeners" best-practices audit (~19 points).
73
+ * The SDK keeps flushing telemetry on `pagehide` / `visibilitychange`
74
+ * (the supported path), so nothing is lost. See the SDK's own
75
+ * `IConfiguration.disablePageUnloadEvents` docs.
76
+ */
77
+ disablePageUnloadEvents?: string[]
78
+ /**
79
+ * Deep overrides spread over the DCS defaults *last*, so a consumer can
80
+ * change any App Insights config field (correlation domains, sampling, …).
81
+ */
82
+ overrides?: Partial<ApplicationInsightsConfig>
83
+ }
84
+
85
+ /**
86
+ * Options for the {@link useTelemetry} composable — the config options plus the
87
+ * per-app enrichment values that the SDK config itself does not carry.
88
+ */
89
+ export interface TelemetryOptions extends TelemetryConfigOptions {
90
+ /** `ai.cloud.role` for this app, e.g. `'dcs-web'`. Default `'dcs-site'`. */
91
+ cloudRole?: string
92
+ /**
93
+ * App version stamped onto every event (`app_version` property +
94
+ * `ai.application.ver` tag). The consumer passes its build-time version
95
+ * define; falls back to `window.__APP_VERSION__` then `'unknown'`.
96
+ */
97
+ appVersion?: string
98
+ /**
99
+ * Environment label added as the `environment` property, e.g. the consumer's
100
+ * `import.meta.env.MODE`. Falls back to `'unknown'`.
101
+ */
102
+ environment?: string
103
+ }