|
| 1 | +/** |
| 2 | + * @import { Messaging } from "@duckduckgo/messaging" |
| 3 | + */ |
| 4 | +export class Telemetry { |
| 5 | + static EVENT_REQUEST = 'TELEMETRY_EVENT_REQUEST'; |
| 6 | + static EVENT_RESPONSE = 'TELEMETRY_EVENT_RESPONSE'; |
| 7 | + static EVENT_SUBSCRIPTION = 'TELEMETRY_EVENT_SUBSCRIPTION'; |
| 8 | + static EVENT_SUBSCRIPTION_DATA = 'TELEMETRY_EVENT_SUBSCRIPTION_DATA'; |
| 9 | + static EVENT_NOTIFICATION = 'TELEMETRY_EVENT_NOTIFICATION'; |
| 10 | + static EVENT_BROADCAST = 'TELEMETRY_*'; |
| 11 | + |
| 12 | + eventTarget = new EventTarget(); |
| 13 | + /** @type {any[]} */ |
| 14 | + eventStore = []; |
| 15 | + storeEnabled = true; |
| 16 | + |
| 17 | + /** |
| 18 | + * @param now |
| 19 | + */ |
| 20 | + constructor(now = Date.now()) { |
| 21 | + this.now = now; |
| 22 | + performance.mark('ddg-telemetry-init'); |
| 23 | + this._setupMessagingMarkers(); |
| 24 | + } |
| 25 | + |
| 26 | + _setupMessagingMarkers() { |
| 27 | + this.eventTarget.addEventListener(Telemetry.EVENT_REQUEST, (/** @type {CustomEvent<any>} */ { detail }) => { |
| 28 | + const named = `ddg request ${detail.method} ${detail.timestamp}`; |
| 29 | + performance.mark(named); |
| 30 | + this.broadcast(detail); |
| 31 | + }); |
| 32 | + this.eventTarget.addEventListener(Telemetry.EVENT_RESPONSE, (/** @type {CustomEvent<any>} */ { detail }) => { |
| 33 | + const reqNamed = `ddg request ${detail.method} ${detail.timestamp}`; |
| 34 | + const resNamed = `ddg response ${detail.method} ${detail.timestamp}`; |
| 35 | + performance.mark(resNamed); |
| 36 | + performance.measure(reqNamed, reqNamed, resNamed); |
| 37 | + this.broadcast(detail); |
| 38 | + }); |
| 39 | + this.eventTarget.addEventListener(Telemetry.EVENT_SUBSCRIPTION, (/** @type {CustomEvent<any>} */ { detail }) => { |
| 40 | + const named = `ddg subscription ${detail.method} ${detail.timestamp}`; |
| 41 | + performance.mark(named); |
| 42 | + this.broadcast(detail); |
| 43 | + }); |
| 44 | + this.eventTarget.addEventListener(Telemetry.EVENT_SUBSCRIPTION_DATA, (/** @type {CustomEvent<any>} */ { detail }) => { |
| 45 | + const named = `ddg subscription data ${detail.method} ${detail.timestamp}`; |
| 46 | + performance.mark(named); |
| 47 | + this.broadcast(detail); |
| 48 | + }); |
| 49 | + this.eventTarget.addEventListener(Telemetry.EVENT_NOTIFICATION, (/** @type {CustomEvent<any>} */ { detail }) => { |
| 50 | + const named = `ddg notification ${detail.method} ${detail.timestamp}`; |
| 51 | + performance.mark(named); |
| 52 | + this.broadcast(detail); |
| 53 | + }); |
| 54 | + } |
| 55 | + |
| 56 | + broadcast(payload) { |
| 57 | + if (this.eventStore.length >= 50) { |
| 58 | + this.eventStore = []; |
| 59 | + } |
| 60 | + if (this.storeEnabled) { |
| 61 | + this.eventStore.push(structuredClone(payload)); |
| 62 | + } |
| 63 | + this.eventTarget.dispatchEvent(new CustomEvent(Telemetry.EVENT_BROADCAST, { detail: payload })); |
| 64 | + } |
| 65 | + |
| 66 | + measureFromPageLoad(marker, measure = 'measure__' + Date.now()) { |
| 67 | + if (!performance.getEntriesByName(marker).length) { |
| 68 | + performance.mark(marker); |
| 69 | + performance.measure(measure, 'ddg-telemetry-init', marker); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * @implements Messaging |
| 76 | + */ |
| 77 | +class MessagingObserver { |
| 78 | + /** @type {Map<string, number>} */ |
| 79 | + observed = new Map(); |
| 80 | + |
| 81 | + /** |
| 82 | + * @param {import("@duckduckgo/messaging").Messaging} messaging |
| 83 | + * @param {EventTarget} eventTarget |
| 84 | + */ |
| 85 | + constructor(messaging, eventTarget) { |
| 86 | + this.messaging = messaging; |
| 87 | + this.messagingContext = messaging.messagingContext; |
| 88 | + this.transport = messaging.transport; |
| 89 | + this.eventTarget = eventTarget; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @param {string} method |
| 94 | + * @param {Record<string, any>} params |
| 95 | + */ |
| 96 | + request(method, params) { |
| 97 | + const timestamp = Date.now(); |
| 98 | + const json = { |
| 99 | + kind: 'request', |
| 100 | + method, |
| 101 | + params, |
| 102 | + timestamp, |
| 103 | + }; |
| 104 | + this.record(Telemetry.EVENT_REQUEST, json); |
| 105 | + return ( |
| 106 | + this.messaging |
| 107 | + .request(method, params) |
| 108 | + // eslint-disable-next-line promise/prefer-await-to-then |
| 109 | + .then((x) => { |
| 110 | + const resJson = { |
| 111 | + kind: 'response', |
| 112 | + method, |
| 113 | + result: x, |
| 114 | + timestamp, |
| 115 | + }; |
| 116 | + this.record(Telemetry.EVENT_RESPONSE, resJson); |
| 117 | + return x; |
| 118 | + }) |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * @param {string} method |
| 124 | + * @param {Record<string, any>} params |
| 125 | + */ |
| 126 | + notify(method, params) { |
| 127 | + const json = { |
| 128 | + kind: 'notification', |
| 129 | + method, |
| 130 | + params, |
| 131 | + }; |
| 132 | + this.record(Telemetry.EVENT_NOTIFICATION, json); |
| 133 | + return this.messaging.notify(method, params); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * @param method |
| 138 | + * @param callback |
| 139 | + * @return {function(): void} |
| 140 | + */ |
| 141 | + subscribe(method, callback) { |
| 142 | + const timestamp = Date.now(); |
| 143 | + const json = { |
| 144 | + kind: 'subscription', |
| 145 | + method, |
| 146 | + timestamp, |
| 147 | + }; |
| 148 | + |
| 149 | + this.record(Telemetry.EVENT_SUBSCRIPTION, json); |
| 150 | + return this.messaging.subscribe(method, (params) => { |
| 151 | + const json = { |
| 152 | + kind: 'subscription data', |
| 153 | + method, |
| 154 | + timestamp, |
| 155 | + params, |
| 156 | + }; |
| 157 | + this.record(Telemetry.EVENT_SUBSCRIPTION_DATA, json); |
| 158 | + callback(params); |
| 159 | + }); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * @param {string} name |
| 164 | + * @param {Record<string, any>} detail |
| 165 | + */ |
| 166 | + record(name, detail) { |
| 167 | + this.eventTarget.dispatchEvent(new CustomEvent(name, { detail })); |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +/** |
| 172 | + * @param {Messaging} messaging |
| 173 | + * @return {{telemetry: Telemetry, messaging: MessagingObserver}} |
| 174 | + */ |
| 175 | +export function install(messaging) { |
| 176 | + const telemetry = new Telemetry(); |
| 177 | + const observedMessaging = new MessagingObserver(messaging, telemetry.eventTarget); |
| 178 | + return { telemetry, messaging: observedMessaging }; |
| 179 | +} |
0 commit comments