|
| 1 | +/** |
| 2 | + * Copyright (c) 2022 Gitpod GmbH. All rights reserved. |
| 3 | + * Licensed under the GNU Affero General Public License (AGPL). |
| 4 | + * See License-AGPL.txt in the project root for license information. |
| 5 | + */ |
| 6 | + |
| 7 | +export * from "../lib/idemetrics_pb"; |
| 8 | +export * from "../lib/idemetrics_pb_service"; |
| 9 | + |
| 10 | +import { MetricsServiceClient } from "../lib/idemetrics_pb_service"; |
| 11 | +import { AddCounterRequest, ObserveHistogramRequest } from "../lib/idemetrics_pb"; |
| 12 | +export interface IDEMetric { |
| 13 | + kind: "counter" | "histogram"; |
| 14 | + name: string; |
| 15 | + labels: Record<string, string>; |
| 16 | + value?: number; |
| 17 | +} |
| 18 | + |
| 19 | +export function sendMetrics(client: MetricsServiceClient, metrics: IDEMetric[]): Promise<PromiseSettledResult<void>[]> { |
| 20 | + return Promise.allSettled( |
| 21 | + metrics.map(async (metric) => { |
| 22 | + if (metric.kind === "counter") { |
| 23 | + const req = new AddCounterRequest(); |
| 24 | + req.setName(metric.name); |
| 25 | + for (const key in metric.labels) { |
| 26 | + req.getLabelsMap().set(key, metric.labels[key]); |
| 27 | + } |
| 28 | + if (metric.value !== undefined) { |
| 29 | + req.setValue(metric.value); |
| 30 | + } |
| 31 | + await new Promise((resolve, reject) => |
| 32 | + client.addCounter(req, (e) => { |
| 33 | + if (e) { |
| 34 | + reject(e); |
| 35 | + } else { |
| 36 | + resolve(undefined); |
| 37 | + } |
| 38 | + }), |
| 39 | + ); |
| 40 | + } else { |
| 41 | + const req = new ObserveHistogramRequest(); |
| 42 | + req.setName(metric.name); |
| 43 | + for (const key in metric.labels) { |
| 44 | + req.getLabelsMap().set(key, metric.labels[key]); |
| 45 | + } |
| 46 | + if (metric.value !== undefined) { |
| 47 | + req.setValue(metric.value); |
| 48 | + } |
| 49 | + await new Promise((resolve, reject) => |
| 50 | + client.observeHistogram(req, (e) => { |
| 51 | + if (e) { |
| 52 | + reject(e); |
| 53 | + } else { |
| 54 | + resolve(undefined); |
| 55 | + } |
| 56 | + }), |
| 57 | + ); |
| 58 | + } |
| 59 | + }), |
| 60 | + ); |
| 61 | +} |
0 commit comments