io.opentracing.contrib.kafka.spring.TracingProducerFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of opentracing-kafka-spring Show documentation
Show all versions of opentracing-kafka-spring Show documentation
OpenTracing Instrumentation for Spring Kafka
/*
* Copyright 2017-2020 The OpenTracing Authors
*
* 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 io.opentracing.contrib.kafka.spring;
import static io.opentracing.contrib.kafka.SpanDecorator.STANDARD_TAGS;
import io.opentracing.Tracer;
import io.opentracing.contrib.kafka.ClientSpanNameProvider;
import io.opentracing.contrib.kafka.SpanDecorator;
import io.opentracing.contrib.kafka.TracingKafkaProducerBuilder;
import java.util.Collection;
import java.util.Collections;
import java.util.function.BiFunction;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.kafka.core.ProducerFactory;
public class TracingProducerFactory implements ProducerFactory, DisposableBean {
private final ProducerFactory producerFactory;
private final Tracer tracer;
private final Collection spanDecorators;
private final BiFunction producerSpanNameProvider;
public TracingProducerFactory(ProducerFactory producerFactory, Tracer tracer) {
this(producerFactory, tracer, null, null);
}
public TracingProducerFactory(ProducerFactory producerFactory, Tracer tracer,
Collection spanDecorators) {
this(producerFactory, tracer, spanDecorators, null);
}
public TracingProducerFactory(ProducerFactory producerFactory, Tracer tracer,
BiFunction producerSpanNameProvider) {
this(producerFactory, tracer, null, producerSpanNameProvider);
}
public TracingProducerFactory(ProducerFactory producerFactory, Tracer tracer,
Collection spanDecorators,
BiFunction producerSpanNameProvider) {
this.producerFactory = producerFactory;
this.tracer = tracer;
this.spanDecorators = (spanDecorators == null)
? Collections.singletonList(STANDARD_TAGS)
: spanDecorators;
this.producerSpanNameProvider = (producerSpanNameProvider == null)
? ClientSpanNameProvider.PRODUCER_OPERATION_NAME
: producerSpanNameProvider;
}
@Override
public Producer createProducer() {
return new TracingKafkaProducerBuilder<>(producerFactory.createProducer(), tracer)
.withDecorators(spanDecorators)
.withSpanNameProvider(producerSpanNameProvider).build();
}
@Override
public Producer createProducer(String txIdPrefix) {
return new TracingKafkaProducerBuilder<>(producerFactory.createProducer(txIdPrefix), tracer)
.withDecorators(spanDecorators).withSpanNameProvider(producerSpanNameProvider).build();
}
@Override
public boolean transactionCapable() {
return producerFactory.transactionCapable();
}
@Override
public void closeProducerFor(String transactionIdSuffix) {
producerFactory.closeProducerFor(transactionIdSuffix);
}
@Override
public boolean isProducerPerConsumerPartition() {
return producerFactory.isProducerPerConsumerPartition();
}
@Override
public void closeThreadBoundProducer() {
producerFactory.closeThreadBoundProducer();
}
@Override
public void destroy() throws Exception {
if (producerFactory instanceof DisposableBean) {
((DisposableBean) producerFactory).destroy();
}
}
}