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

org.bdware.doip.cluster.Tracing Maven / Gradle / Ivy

The newest version!
package org.bdware.doip.cluster;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import io.opentracing.Scope;
import io.opentracing.tag.Tags;
import org.bdware.doip.codec.doipMessage.DoipMessage;
import io.jaegertracing.Configuration;
import io.jaegertracing.Configuration.ReporterConfiguration;
import io.jaegertracing.Configuration.SamplerConfiguration;
import io.jaegertracing.internal.samplers.ConstSampler;
import io.opentracing.Tracer;
import io.opentracing.Span;
import io.opentracing.SpanContext;
import io.opentracing.propagation.Format;
import io.opentracing.propagation.TextMapAdapter;

import java.util.HashMap;
import java.util.Map;

public class Tracing {
    public static final String IS_SPANNED = "jaeger_is_spanned";

    private static String serviceName = "doip-cluster";
    private static Tracer tracer = init("doip-cluster");

    private static Map spanScopeMap = new HashMap<>();

    public static void setServiceName(String service) {
        if(service != null && service.length() > 0){
            serviceName = service;
            tracer = init(serviceName);
        }
    }

    private static Tracer init(String service) {
        SamplerConfiguration samplerConfig = SamplerConfiguration.fromEnv()
                .withType(ConstSampler.TYPE)
                .withParam(1);

        ReporterConfiguration reporterConfig = ReporterConfiguration.fromEnv()
                .withLogSpans(true)
                .withSender(
                        new Configuration.SenderConfiguration()
                                .withAgentHost("localhost")
                                .withAgentPort(6831)
                );

        Configuration config = new Configuration(service)
                .withSampler(samplerConfig)
                .withReporter(reporterConfig);

        return config.getTracer();
    }

    public static Span startClientSpan(DoipMessage msg) {
        if(msg.header.parameters.id != null && !msg.header.parameters.id.isEmpty()) {
            return startClientSpan(msg, msg.header.parameters.id);
        }
        return startClientSpan(msg, "default");
    }

    public static Span startServerSpan(DoipMessage msg) {
        if(msg.header.parameters.id != null && !msg.header.parameters.id.isEmpty()) {
            return startServerSpan(msg, msg.header.parameters.id);
        }
        return startServerSpan(msg, "default");
    }

    public static Span startClientSpan(DoipMessage msg, String operationName) {
        Tracer.SpanBuilder spanBuilder = tracer.buildSpan(operationName);
        Span span = spanBuilder.start();
        span.setTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT);
        span.setTag("doipMessage_doid", msg.header.parameters.id);
        if (msg.header.parameters.operation != null && !msg.header.parameters.operation.isEmpty()) {
            span.setTag("doipMessage_opCode", msg.header.parameters.operation);
        }
        tracer.inject(span.context(), Format.Builtin.TEXT_MAP, new DoipMessageCarrier(msg));
        msg.header.parameters.attributes.addProperty(IS_SPANNED, true);
        Scope scope = tracer.scopeManager().activate(span);
        spanScopeMap.put(span.context().toSpanId(), scope);
        return span;
    }

    public static Span startServerSpan(DoipMessage reqMsg, String operationName) {
        // format the headers for extraction
        Map headers = new HashMap<>();
        JsonObject rawHeaders = reqMsg.header.parameters.attributes;
        if(rawHeaders != null) {
            for (Map.Entry entry : rawHeaders.entrySet()) {
                headers.put(entry.getKey(), entry.getValue().getAsString());
            }
        }

        Tracer.SpanBuilder spanBuilder;
        try {
            SpanContext parentSpanCtx = tracer.extract(Format.Builtin.TEXT_MAP, new TextMapAdapter(headers));
            if (parentSpanCtx != null) {
                spanBuilder = tracer.buildSpan(operationName).asChildOf(parentSpanCtx);
            } else {
                spanBuilder = tracer.buildSpan(operationName);
            }
        } catch (IllegalArgumentException e) {
            spanBuilder = tracer.buildSpan(operationName);
        }

        Span span = spanBuilder.start();

        // set some tag
        span.setTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_SERVER);
        span.setTag("doipMessage_doid", reqMsg.header.parameters.id);
        if (reqMsg.header.parameters.operation != null && !reqMsg.header.parameters.operation.isEmpty()) {
            span.setTag("doipMessage_opCode", reqMsg.header.parameters.operation);
        }
        Scope scope = tracer.scopeManager().activate(span);
        spanScopeMap.put(span.context().toSpanId(), scope);
        return span;
    }

    public static void injectSpanContext(DoipMessage msg, Span span) {
        tracer.inject(span.context(), Format.Builtin.TEXT_MAP, new DoipMessageCarrier(msg));
        msg.header.parameters.attributes.addProperty(IS_SPANNED, true);
    }

    public static Span activeSpan() {
        return tracer.activeSpan();
    }

    public static void finish(Span span) {
        if(span == null) return;
        String spanId = span.context().toSpanId();
        span.finish();
        if (spanScopeMap.containsKey(spanId) && spanScopeMap.get(spanId) != null) {
            spanScopeMap.get(spanId).close();
            spanScopeMap.remove(spanId);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy