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

com.microsoft.azure.kusto.data.instrumentation.Tracer Maven / Gradle / Ivy

There is a newer version: 5.2.0
Show newest version
package com.microsoft.azure.kusto.data.instrumentation;

import com.azure.core.util.Configuration;
import com.azure.core.util.Context;
import com.azure.core.util.tracing.ProcessKind;

import java.util.Map;

public class Tracer {
    private static final boolean IS_TRACING_DISABLED = Configuration.getGlobalConfiguration().get(Configuration.PROPERTY_AZURE_TRACING_DISABLED, false);
    private static com.azure.core.util.tracing.Tracer tracer;
    private static volatile boolean initialized = false;

    public static void initializeTracer(com.azure.core.util.tracing.Tracer tracer) {
        if (!Tracer.initialized) {
            synchronized (Tracer.class) {
                if (!Tracer.initialized) {
                    Tracer.tracer = IS_TRACING_DISABLED ? null : tracer;
                    initialized = true;
                }
            }
        }
    }

    public static Span startSpan(String spanName) {
        return startSpan(spanName, Context.NONE, ProcessKind.PROCESS, null);
    }

    public static Span startSpan(String spanName, Map attributes) {
        return startSpan(spanName, Context.NONE, ProcessKind.PROCESS, attributes);
    }

    public static Span startSpan(String spanName, Context context, ProcessKind kind, Map attributes) {
        Context span = tracer == null ? null : tracer.start(spanName, context, kind);
        return new Span(span, attributes);
    }

    public static class Span implements AutoCloseable {
        private final Context span;
        private Throwable throwable;

        private Span(Context span, Map attributes) {
            this.span = span;
            setAttributes(attributes);
        }

        @Override
        public void close() {
            if (tracer != null && span != null) {
                String errorCondition = "success";
                if (throwable != null) {
                    errorCondition = throwable.getLocalizedMessage();
                }
                tracer.end(errorCondition, throwable, span);
            }
        }

        public void addException(Exception e) {
            throwable = e;
        }

        public void setAttributes(Map attributes) {
            if (tracer != null && attributes != null && span != null) {
                attributes.forEach((k, v) -> tracer.setAttribute(k, v, span));
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy