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

com.expediagroup.rhapsody.rabbitmq.message.TracingRabbitMessageSendInterceptor Maven / Gradle / Ivy

There is a newer version: 0.5.16
Show newest version
/**
 * Copyright 2019 Expedia, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.expediagroup.rhapsody.rabbitmq.message;

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

import com.rabbitmq.client.AMQP;

import io.opentracing.Scope;
import io.opentracing.SpanContext;
import io.opentracing.Tracer;
import io.opentracing.propagation.Format;
import io.opentracing.propagation.TextMapExtractAdapter;
import io.opentracing.propagation.TextMapInjectAdapter;

public class TracingRabbitMessageSendInterceptor extends ConfigurableRabbitMessageTracing implements RabbitMessageSendInterceptor {

    public static final String REFERENCE_PARENT_SPAN_CONFIG = "tracing-interceptor-reference-parent-span";

    @Override
    public RabbitMessage onSend(RabbitMessage rabbitMessage) {
        Map headers = RabbitMessageExtraction.extractStringifiedHeaders(rabbitMessage);

        SpanContext spanContext = tracer.extract(Format.Builtin.TEXT_MAP, new TextMapExtractAdapter(headers));
        Map tags = createTags(rabbitMessage, headers);
        Map baggage = spanContext != null ? extractBaggage(headers, tags, spanContext) : extractBaggage(headers, tags);

        Tracer.SpanBuilder spanBuilder = spanContext != null && referenceParentSpan ? buildSpan().asChildOf(spanContext) : buildSpan(!referenceParentSpan);
        tags.forEach(spanBuilder::withTag);

        try (Scope scope = spanBuilder.startActive(true)) {
            baggage.forEach(scope.span()::setBaggageItem);
            return injectSpanContext(rabbitMessage, scope.span().context());
        }
    }

    @Override
    protected String getReferenceParentSpanConfig() {
        return REFERENCE_PARENT_SPAN_CONFIG;
    }

    @Override
    protected String getOperationName() {
        return "create-rabbitmessage";
    }

    private RabbitMessage injectSpanContext(RabbitMessage rabbitMessage, SpanContext context) {
        Map injectedContext = new HashMap<>();
        tracer.inject(context, Format.Builtin.TEXT_MAP, new TextMapInjectAdapter(injectedContext));

        AMQP.BasicProperties properties = rabbitMessage.getProperties();
        Map headers = new HashMap<>(properties.getHeaders() == null ? Collections.emptyMap() : properties.getHeaders());
        headers.putAll(injectedContext);

        return new RabbitMessage<>(rabbitMessage.getExchange(), rabbitMessage.getRoutingKey(), properties.builder().headers(headers).build(), rabbitMessage.getBody());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy