UNPKG

@microsoft/compose-language-service

Version:
101 lines 4.53 kB
/*!-------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See LICENSE in the project root for license information. *--------------------------------------------------------------------------------------------*/ import * as util from 'util'; import { initEvent } from '../../../common/TelemetryEvent'; import { logNormal } from './logNormal'; // One minute flush interval by default const FlushIntervalMilliseconds = 60 * 1000; export class TelemetryAggregator { connection; interval; eventBuffer = []; timer; constructor(connection, interval = FlushIntervalMilliseconds) { this.connection = connection; this.interval = interval; this.timer = setInterval(() => this.flush(), this.interval); } dispose() { clearInterval(this.timer); // Flush one last time this.flush(); } logEvent(event) { if (event.suppressAll) { // Do nothing, this event is suppressed } else if (event.properties.result === 'Succeeded' && event.suppressIfSuccessful) { // Do nothing, the event succeeded and has suppressIfSuccessful === true } else if (event.properties.result === 'Failed') { // Event is an error, send immediately rather than buffering this.connection.telemetry.logEvent(event); } else { // Add it to the event buffer to be flushed on the interval this.eventBuffer.push(event); } } flush() { try { for (const evt of this.getAggregatedEvents()) { this.connection.telemetry.logEvent(evt); } } catch (err) { const error = err instanceof Error ? err : Error(util.inspect(err)); const telemetryFailedEvent = initEvent('telemetryaggregatorfailure'); telemetryFailedEvent.properties.result = 'Failed'; telemetryFailedEvent.properties.error = error.name; telemetryFailedEvent.properties.errorMessage = error.message; telemetryFailedEvent.properties.stack = error.stack; this.connection.telemetry.logEvent(telemetryFailedEvent); } finally { // Finally, clear out the buffer this.eventBuffer = []; } } getAggregatedEvents() { const aggregated = []; const eventGroups = new Map(); // Group events according to their grouping strategy for (const evt of this.eventBuffer) { let key; switch (evt.groupingStrategy) { case 'eventNameAndProperties': key = evt.eventName + JSON.stringify(evt.properties); break; case 'eventName': default: key = evt.eventName; break; } if (!eventGroups.has(key)) { eventGroups.set(key, []); } // eslint-disable-next-line @typescript-eslint/no-non-null-assertion eventGroups.get(key).push(evt); } // For each group, aggregate properties and add performance statistics, to get one aggregated event per group for (const events of eventGroups.values()) { const eventName = events[0].eventName; const aggregatedEvent = initEvent(eventName); // Aggregate the performance statistics const durations = events.map(e => e.measurements.duration).filter((d) => d !== undefined); const stats = logNormal(durations); aggregatedEvent.measurements.count = events.length; aggregatedEvent.measurements.durationMu = stats.mu; aggregatedEvent.measurements.durationSigma = stats.sigma; aggregatedEvent.measurements.durationMedian = stats.median; // Aggregate the properties--this will apply all properties from all events, with the recent events overriding prior events if there is a conflict // If the grouping strategy is 'eventNameAndProperties', there will inherently never be conflicts, since their values must be identical events.forEach(evt => Object.assign(aggregatedEvent.properties, evt.properties)); aggregated.push(aggregatedEvent); } return aggregated; } } //# sourceMappingURL=TelemetryAggregator.js.map