All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ru.tinkoff.kora.micrometer.module.scheduling.Opentelemetry123SchedulingMetrics Maven / Gradle / Ivy

There is a newer version: 1.1.9
Show newest version
package ru.tinkoff.kora.micrometer.module.scheduling;

import io.micrometer.core.instrument.DistributionSummary;
import io.micrometer.core.instrument.MeterRegistry;
import io.opentelemetry.semconv.SemanticAttributes;
import jakarta.annotation.Nullable;
import ru.tinkoff.kora.scheduling.common.telemetry.SchedulingMetrics;
import ru.tinkoff.kora.telemetry.common.TelemetryConfig;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class Opentelemetry123SchedulingMetrics implements SchedulingMetrics {
    private final Map, DistributionSummary> errorDuration = new ConcurrentHashMap<>();
    private final DistributionSummary successDuration;
    private final MeterRegistry meterRegistry;
    private final TelemetryConfig.MetricsConfig config;
    private final String className;
    private final String methodName;

    public Opentelemetry123SchedulingMetrics(MeterRegistry meterRegistry, TelemetryConfig.MetricsConfig config, String className, String methodName) {
        this.meterRegistry = meterRegistry;
        this.config = config;
        this.className = className;
        this.methodName = methodName;
        this.successDuration = duration(null);
    }

    private DistributionSummary duration(@Nullable Class error) {
        var builder = DistributionSummary.builder("scheduling.job.duration")
            .serviceLevelObjectives(this.config.slo(TelemetryConfig.MetricsConfig.OpentelemetrySpec.V123))
            .baseUnit("s")
            .tag(SemanticAttributes.CODE_FUNCTION.getKey(), this.methodName)
            .tag("code.class", this.className);
        if (error != null) {
            builder.tag(SemanticAttributes.ERROR_TYPE.getKey(), error.getCanonicalName());
        }
        return builder.register(this.meterRegistry);
    }

    @Override
    public void record(long processingTimeNanos, @Nullable Throwable e) {
        var procesingTime = processingTimeNanos / 1_000_000_000d;
        if (e == null) {
            this.successDuration.record(procesingTime);
        } else {
            this.errorDuration.computeIfAbsent(e.getClass(), this::duration).record(procesingTime);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy