|
| 1 | +import Rum from '@hyperdx/otel-web'; |
| 2 | +import SessionRecorder from '@hyperdx/otel-web-session-recorder'; |
| 3 | +import type { Attributes } from '@opentelemetry/api'; |
| 4 | +import type { ResourceAttributes } from '@opentelemetry/resources'; |
| 5 | + |
| 6 | +type InitConfig = { |
| 7 | + advancedNetworkCapture?: boolean; |
| 8 | + apiKey: string; |
| 9 | + blockClass?: string; |
| 10 | + consoleCapture?: boolean; |
| 11 | + debug?: boolean; |
| 12 | + disableReplay?: boolean; |
| 13 | + ignoreClass?: string; |
| 14 | + maskAllInputs?: boolean; |
| 15 | + maskAllText?: boolean; |
| 16 | + maskClass?: string; |
| 17 | + recordCanvas?: boolean; |
| 18 | + sampling?: Parameters<typeof SessionRecorder.init>[0]['sampling']; |
| 19 | + service: string; |
| 20 | + tracePropagationTargets?: (string | RegExp)[]; |
| 21 | + url?: string; |
| 22 | + tracesUrl?: string; |
| 23 | + logsUrl?: string; |
| 24 | + otelResourceAttributes?: ResourceAttributes; |
| 25 | +}; |
| 26 | + |
| 27 | +const DEFAULT_URL = 'https://in-otel.hyperdx.io'; |
| 28 | + |
| 29 | +let _advancedNetworkCapture = false; |
| 30 | + |
| 31 | +function init({ |
| 32 | + advancedNetworkCapture = false, |
| 33 | + apiKey, |
| 34 | + blockClass, |
| 35 | + consoleCapture = false, |
| 36 | + debug = false, |
| 37 | + disableReplay = false, |
| 38 | + ignoreClass, |
| 39 | + maskAllInputs = true, |
| 40 | + maskAllText = false, |
| 41 | + maskClass, |
| 42 | + recordCanvas = false, |
| 43 | + sampling, |
| 44 | + service, |
| 45 | + tracePropagationTargets, |
| 46 | + url, |
| 47 | + tracesUrl, |
| 48 | + logsUrl, |
| 49 | + otelResourceAttributes, |
| 50 | +}: InitConfig): void { |
| 51 | + if (typeof window === 'undefined') { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + const urlBase = url ?? DEFAULT_URL; |
| 56 | + const resolvedTracesUrl = tracesUrl ?? `${urlBase}/v1/traces`; |
| 57 | + const resolvedLogsUrl = logsUrl ?? `${urlBase}/v1/logs`; |
| 58 | + |
| 59 | + _advancedNetworkCapture = advancedNetworkCapture; |
| 60 | + |
| 61 | + Rum.init({ |
| 62 | + debug, |
| 63 | + url: resolvedTracesUrl, |
| 64 | + allowInsecureUrl: true, |
| 65 | + apiKey, |
| 66 | + applicationName: service, |
| 67 | + globalAttributes: otelResourceAttributes as Attributes | undefined, |
| 68 | + instrumentations: { |
| 69 | + visibility: true, |
| 70 | + console: consoleCapture, |
| 71 | + fetch: { |
| 72 | + ...(tracePropagationTargets != null |
| 73 | + ? { propagateTraceHeaderCorsUrls: tracePropagationTargets } |
| 74 | + : {}), |
| 75 | + advancedNetworkCapture: () => _advancedNetworkCapture, |
| 76 | + }, |
| 77 | + xhr: { |
| 78 | + ...(tracePropagationTargets != null |
| 79 | + ? { propagateTraceHeaderCorsUrls: tracePropagationTargets } |
| 80 | + : {}), |
| 81 | + advancedNetworkCapture: () => _advancedNetworkCapture, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }); |
| 85 | + |
| 86 | + if (!disableReplay) { |
| 87 | + SessionRecorder.init({ |
| 88 | + apiKey, |
| 89 | + blockClass, |
| 90 | + debug, |
| 91 | + ignoreClass, |
| 92 | + maskAllInputs, |
| 93 | + maskTextClass: maskClass, |
| 94 | + maskTextSelector: maskAllText ? '*' : undefined, |
| 95 | + recordCanvas, |
| 96 | + sampling, |
| 97 | + url: resolvedLogsUrl, |
| 98 | + }); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +function addAction(name: string, attributes?: Attributes): void { |
| 103 | + if (typeof window === 'undefined') return; |
| 104 | + Rum.addAction(name, attributes); |
| 105 | +} |
| 106 | + |
| 107 | +function setGlobalAttributes(attributes: Record<string, string>): void { |
| 108 | + if (typeof window === 'undefined') return; |
| 109 | + Rum.setGlobalAttributes(attributes); |
| 110 | +} |
| 111 | + |
| 112 | +function enableAdvancedNetworkCapture(): void { |
| 113 | + _advancedNetworkCapture = true; |
| 114 | +} |
| 115 | + |
| 116 | +function getSessionId(): string | undefined { |
| 117 | + return Rum.getSessionId(); |
| 118 | +} |
| 119 | + |
| 120 | +function recordException(error: any, attributes?: Attributes): void { |
| 121 | + if (typeof window === 'undefined') return; |
| 122 | + Rum.recordException(error, attributes); |
| 123 | +} |
| 124 | + |
| 125 | +const HyperDXBrowser = { |
| 126 | + init, |
| 127 | + addAction, |
| 128 | + setGlobalAttributes, |
| 129 | + enableAdvancedNetworkCapture, |
| 130 | + getSessionId, |
| 131 | + recordException, |
| 132 | +}; |
| 133 | + |
| 134 | +export default HyperDXBrowser; |
0 commit comments