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

ru.tinkoff.kora.opentelemetry.module.cache.OpentelementryCacheTracer Maven / Gradle / Ivy

There is a newer version: 1.1.15
Show newest version
package ru.tinkoff.kora.opentelemetry.module.cache;

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.api.trace.Tracer;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;
import ru.tinkoff.kora.cache.telemetry.CacheTelemetryOperation;
import ru.tinkoff.kora.cache.telemetry.CacheTracer;
import ru.tinkoff.kora.common.Context;
import ru.tinkoff.kora.opentelemetry.common.OpentelemetryContext;

public final class OpentelementryCacheTracer implements CacheTracer {

    private static final String TAG_OPERATION = "operation";
    private static final String TAG_CACHE_NAME = "cache";
    private static final String TAG_ORIGIN = "origin";

    private final Tracer tracer;

    public OpentelementryCacheTracer(Tracer tracer) {
        this.tracer = tracer;
    }

    record OpentelemetryCacheSpan(Span span, Context currentContext, OpentelemetryContext context) implements CacheSpan {

        @Override
        public void recordSuccess() {
            span.setStatus(StatusCode.OK);
            span.end();
            OpentelemetryContext.set(currentContext, context);
        }

        @Override
        public void recordFailure(@Nullable Throwable throwable) {
            if (throwable != null) {
                span.recordException(throwable);
            }
            span.setStatus(StatusCode.ERROR);
            span.end();
            OpentelemetryContext.set(currentContext, context);
        }
    }

    @Override
    public CacheSpan trace(@Nonnull CacheTelemetryOperation operation) {
        var context = Context.current();
        var traceContext = OpentelemetryContext.get(context);
        var span = this.tracer.spanBuilder("cache.call")
            .setSpanKind(SpanKind.INTERNAL)
            .setParent(traceContext.getContext())
            .setAttribute(TAG_OPERATION, operation.name())
            .setAttribute(TAG_CACHE_NAME, operation.cacheName())
            .setAttribute(TAG_ORIGIN, operation.origin())
            .startSpan();

        OpentelemetryContext.set(context, traceContext.add(span));
        return new OpentelemetryCacheSpan(span, context, traceContext);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy