refactor: Update Telemetry class to include new metrics for status checks

This commit is contained in:
Simon Larsen 2024-06-07 14:54:39 +01:00
parent 054592eed3
commit 931abc522b
No known key found for this signature in database
GPG Key ID: 96C5DCA24769DBCA
3 changed files with 20 additions and 18 deletions

View File

@ -19,35 +19,30 @@ export default class StatusAPI {
public static statusCheckSuccessCounter = Telemetry.getCounter({
name: 'status.check.success',
description: 'Status check counter',
labels: {},
});
// ready counter
public static stausReadySuccess = Telemetry.getCounter({
name: 'status.ready.success',
description: 'Ready check counter',
labels: {},
});
// live counter
public static stausLiveSuccess = Telemetry.getCounter({
name: 'status.live.success',
description: 'Live check counter',
labels: {},
});
// ready failed counter
public static stausReadyFailed = Telemetry.getCounter({
name: 'status.ready.failed',
description: 'Ready check counter',
labels: {},
});
// live failed counter
public static stausLiveFailed = Telemetry.getCounter({
name: 'status.live.failed',
description: 'Live check counter',
labels: {},
});
public static init(options: StatusAPIOptions): ExpressRouter {

View File

@ -5,19 +5,21 @@ import { OTLPLogExporter } from '@opentelemetry/exporter-logs-otlp-http';
import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-proto';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto';
import { AWSXRayIdGenerator } from '@opentelemetry/id-generator-aws-xray';
import { Meter, MeterProvider } from '@opentelemetry/metrics';
import { CompressionAlgorithm } from '@opentelemetry/otlp-exporter-base';
import { Resource } from '@opentelemetry/resources';
import {
BatchLogRecordProcessor,
LoggerProvider,
} from '@opentelemetry/sdk-logs';
import { PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';
import { PeriodicExportingMetricReader, MeterProvider } from '@opentelemetry/sdk-metrics';
import * as opentelemetry from '@opentelemetry/sdk-node';
import { SpanExporter } from '@opentelemetry/sdk-trace-node';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import URL from 'Common/Types/API/URL';
import Dictionary from 'Common/Types/Dictionary';
import OpenTelemetryAPI, { Meter } from '@opentelemetry/api';
// Enable this line to see debug logs
// diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG);
@ -27,8 +29,8 @@ const serviceName: string = process.env['SERVICE_NAME'] || 'oneuptime';
export default class Telemetry {
public static sdk: opentelemetry.NodeSDK | null = null;
public static logger: Logger | null = null;
public static meter: Meter | null = null;
public static meterProvider: MeterProvider | null = null;
public static getHeaders(): Dictionary<string> {
if (!process.env['OPENTELEMETRY_EXPORTER_OTLP_HEADERS']) {
@ -189,9 +191,18 @@ export default class Telemetry {
return this.logger!;
}
public static getMeterProvider(): MeterProvider {
if (!this.meterProvider) {
this.meterProvider = new MeterProvider();
OpenTelemetryAPI.metrics.setGlobalMeterProvider(this.meterProvider);
}
return this.meterProvider;
}
public static getMeter(): Meter {
if (!this.meter) {
this.meter = new MeterProvider().getMeter(serviceName);
this.meter = OpenTelemetryAPI.metrics.getMeter('default');
}
return this.meter;
@ -200,16 +211,12 @@ export default class Telemetry {
public static getCounter(data: {
name: string;
description: string;
labels: Dictionary<string>;
}): Counter {
const { name, description, labels } = data;
const counter = this.getMeter().createCounter(name, { description });
if (labels && Object.keys(labels).length > 0) {
counter.bind(labels);
}
const { name, description } = data;
const counter = this.getMeter().createCounter(name, {
description: description,
});
return counter;
}
}

View File

@ -316,7 +316,7 @@ router.post(
const metricDescription: string = metric[
'description'
] as string;
if (
metric['sum'] &&
(metric['sum'] as JSONObject)['dataPoints'] &&