Wrap Snapshot Telemetry generation in an APM transaction (#180846)

This commit is contained in:
Alejandro Fernández Haro 2024-04-16 14:56:32 +02:00 committed by GitHub
parent 057da1e09d
commit 591bbff92e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -21,6 +21,7 @@ import type {
} from '@kbn/core/server';
import { firstValueFrom, ReplaySubject } from 'rxjs';
import apm from 'elastic-apm-node';
import type {
TelemetryCollectionManagerPluginSetup,
TelemetryCollectionManagerPluginStart,
@ -259,7 +260,16 @@ export class TelemetryCollectionManagerPlugin
config: EncryptedStatsGetterConfig
): Promise<Array<{ clusterUuid: string; stats: string }>>;
private async getStats(config: StatsGetterConfig) {
const retrieveSnapshotTelemetryTransaction = apm.startTransaction(
'Retrieve Snapshot Telemetry',
'telemetry'
);
retrieveSnapshotTelemetryTransaction.addLabels({
unencrypted: config.unencrypted,
refresh: config.refreshCache,
});
if (!this.usageCollection) {
retrieveSnapshotTelemetryTransaction.end('skipped');
return [];
}
const collection = this.collectionStrategy;
@ -268,10 +278,12 @@ export class TelemetryCollectionManagerPlugin
const statsCollectionConfig = this.getStatsCollectionConfig(config, this.usageCollection);
if (statsCollectionConfig) {
try {
retrieveSnapshotTelemetryTransaction.startSpan('Fetch usage');
const usageData = await this.getUsageForCollection(collection, statsCollectionConfig);
this.logger.debug(`Received Usage using ${collection.title} collection.`);
return await Promise.all(
retrieveSnapshotTelemetryTransaction.startSpan('Prepare response');
const results = await Promise.all(
usageData.map(async (clusterStats) => {
const { cluster_uuid: clusterUuid } = clusterStats.cluster_stats as Record<
string,
@ -288,14 +300,20 @@ export class TelemetryCollectionManagerPlugin
};
})
);
retrieveSnapshotTelemetryTransaction.end('success');
return results;
} catch (err) {
this.logger.debug(
`Failed to collect any usage with registered collection ${collection.title}.`
);
retrieveSnapshotTelemetryTransaction.end('error');
return [];
}
}
}
retrieveSnapshotTelemetryTransaction.end('skipped');
return [];
}