|
| 1 | +import { BacktraceClientOptions } from '..'; |
| 2 | +import { currentTimestamp, getEndpointParams, post, uuid } from '../utils'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Handles Backtrace Metrics. |
| 6 | + */ |
| 7 | +export class BacktraceMetrics { |
| 8 | + private readonly universe: string; |
| 9 | + private readonly token: string; |
| 10 | + private readonly hostname: string; |
| 11 | + |
| 12 | + private summedEndpoint: string; |
| 13 | + private uniqueEndpoint: string; |
| 14 | + |
| 15 | + private sessionId: string = uuid(); |
| 16 | + |
| 17 | + constructor( |
| 18 | + configuration: BacktraceClientOptions, |
| 19 | + private readonly attributeProvider: () => object, |
| 20 | + private readonly _logger: { error(...data: any[]): void } | undefined, |
| 21 | + ) { |
| 22 | + if (!configuration.endpoint) { |
| 23 | + throw new Error(`Backtrace: missing 'endpoint' option.`); |
| 24 | + } |
| 25 | + const endpointParameters = getEndpointParams(configuration.endpoint, configuration.token); |
| 26 | + if (!endpointParameters) { |
| 27 | + throw new Error(`Invalid Backtrace submission parameters. Cannot create a submission URL to metrics support`); |
| 28 | + } |
| 29 | + const { universe, token } = endpointParameters; |
| 30 | + |
| 31 | + if (!universe) { |
| 32 | + throw new Error(`Backtrace: 'universe' could not be parsed from the endpoint.`); |
| 33 | + } |
| 34 | + |
| 35 | + if (!token) { |
| 36 | + throw new Error(`Backtrace: missing 'token' option or it could not be parsed from the endpoint.`); |
| 37 | + } |
| 38 | + |
| 39 | + this.universe = universe; |
| 40 | + this.token = token; |
| 41 | + this.hostname = configuration.metricsSubmissionUrl ?? 'https://events.backtrace.io'; |
| 42 | + |
| 43 | + this.summedEndpoint = `${this.hostname}/api/summed-events/submit?universe=${this.universe}&token=${this.token}`; |
| 44 | + this.uniqueEndpoint = `${this.hostname}/api/unique-events/submit?universe=${this.universe}&token=${this.token}`; |
| 45 | + |
| 46 | + this.handleSession(); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Handle persisting of session. When called, will create a new session. |
| 51 | + */ |
| 52 | + private handleSession(): void { |
| 53 | + // If sessionId is not set, create new session. Send unique and app launch events. |
| 54 | + this.sendUniqueEvent(); |
| 55 | + this.sendSummedEvent('Application Launches'); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Send POST to unique-events API endpoint |
| 60 | + */ |
| 61 | + public async sendUniqueEvent(): Promise<void> { |
| 62 | + const attributes = this.getEventAttributes(); |
| 63 | + const payload = { |
| 64 | + application: attributes.application, |
| 65 | + appversion: attributes['application.version'], |
| 66 | + metadata: { |
| 67 | + dropped_events: 0, |
| 68 | + }, |
| 69 | + unique_events: [ |
| 70 | + { |
| 71 | + timestamp: currentTimestamp(), |
| 72 | + unique: ['guid'], |
| 73 | + attributes: this.getEventAttributes(), |
| 74 | + }, |
| 75 | + ], |
| 76 | + }; |
| 77 | + |
| 78 | + try { |
| 79 | + await post(this.uniqueEndpoint, payload); |
| 80 | + } catch (e) { |
| 81 | + this._logger?.error(`Encountered error sending unique event: ${e?.message}`); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Send POST to summed-events API endpoint |
| 87 | + */ |
| 88 | + public async sendSummedEvent(metricGroup: string): Promise<void> { |
| 89 | + const attributes = this.getEventAttributes(); |
| 90 | + |
| 91 | + const payload = { |
| 92 | + application: attributes.application, |
| 93 | + appversion: attributes['application.version'], |
| 94 | + metadata: { |
| 95 | + dropped_events: 0, |
| 96 | + }, |
| 97 | + summed_events: [ |
| 98 | + { |
| 99 | + timestamp: currentTimestamp(), |
| 100 | + metric_group: metricGroup, |
| 101 | + attributes: this.getEventAttributes(), |
| 102 | + }, |
| 103 | + ], |
| 104 | + }; |
| 105 | + |
| 106 | + try { |
| 107 | + await post(this.summedEndpoint, payload); |
| 108 | + } catch (e) { |
| 109 | + this._logger?.error(`Encountered error sending summed event: ${e?.message}`); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private getEventAttributes(): { [index: string]: any } { |
| 114 | + const clientAttributes = this.attributeProvider() as { |
| 115 | + [index: string]: any; |
| 116 | + }; |
| 117 | + const result: { [index: string]: string } = { |
| 118 | + 'application.session': this.sessionId, |
| 119 | + }; |
| 120 | + |
| 121 | + for (const attributeName in clientAttributes) { |
| 122 | + if (Object.prototype.hasOwnProperty.call(clientAttributes, attributeName)) { |
| 123 | + const element = clientAttributes[attributeName]; |
| 124 | + const elementType = typeof element; |
| 125 | + |
| 126 | + if (elementType === 'string' || elementType === 'boolean' || elementType === 'number') { |
| 127 | + const attributeValue = element.toString(); |
| 128 | + if (attributeValue) { |
| 129 | + result[attributeName] = attributeValue; |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + return result; |
| 135 | + } |
| 136 | +} |
0 commit comments