io.opentelemetry.instrumentation.rocketmqclient.v4_8.RocketMqInstrumenterFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of opentelemetry-rocketmq-client-4.8 Show documentation
Show all versions of opentelemetry-rocketmq-client-4.8 Show documentation
Instrumentation of Java libraries using OpenTelemetry.
The newest version!
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.instrumentation.rocketmqclient.v4_8;
import static io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor.constant;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.instrumentation.api.incubator.semconv.messaging.MessageOperation;
import io.opentelemetry.instrumentation.api.incubator.semconv.messaging.MessagingAttributesExtractor;
import io.opentelemetry.instrumentation.api.incubator.semconv.messaging.MessagingAttributesGetter;
import io.opentelemetry.instrumentation.api.incubator.semconv.messaging.MessagingSpanNameExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder;
import io.opentelemetry.instrumentation.api.instrumenter.SpanKindExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.SpanLinksExtractor;
import io.opentelemetry.instrumentation.api.internal.PropagatorBasedSpanLinksExtractor;
import java.util.List;
import org.apache.rocketmq.client.hook.SendMessageContext;
import org.apache.rocketmq.common.message.MessageExt;
class RocketMqInstrumenterFactory {
private static final String INSTRUMENTATION_NAME = "io.opentelemetry.rocketmq-client-4.8";
// copied from MessagingIncubatingAttributes
private static final AttributeKey MESSAGING_OPERATION =
AttributeKey.stringKey("messaging.operation");
private static final AttributeKey MESSAGING_SYSTEM =
AttributeKey.stringKey("messaging.system");
static Instrumenter createProducerInstrumenter(
OpenTelemetry openTelemetry,
List capturedHeaders,
boolean captureExperimentalSpanAttributes) {
RocketMqProducerAttributeGetter getter = RocketMqProducerAttributeGetter.INSTANCE;
MessageOperation operation = MessageOperation.PUBLISH;
InstrumenterBuilder instrumenterBuilder =
Instrumenter.builder(
openTelemetry,
INSTRUMENTATION_NAME,
MessagingSpanNameExtractor.create(getter, operation))
.addAttributesExtractor(
buildMessagingAttributesExtractor(getter, operation, capturedHeaders));
if (captureExperimentalSpanAttributes) {
instrumenterBuilder.addAttributesExtractor(
RocketMqProducerExperimentalAttributeExtractor.INSTANCE);
}
return instrumenterBuilder.buildProducerInstrumenter(MapSetter.INSTANCE);
}
static RocketMqConsumerInstrumenter createConsumerInstrumenter(
OpenTelemetry openTelemetry,
List capturedHeaders,
boolean captureExperimentalSpanAttributes) {
InstrumenterBuilder batchReceiveInstrumenterBuilder =
Instrumenter.builder(
openTelemetry, INSTRUMENTATION_NAME, RocketMqInstrumenterFactory::spanNameOnReceive)
.addAttributesExtractor(constant(MESSAGING_SYSTEM, "rocketmq"))
.addAttributesExtractor(constant(MESSAGING_OPERATION, "receive"));
return new RocketMqConsumerInstrumenter(
createProcessInstrumenter(
openTelemetry, capturedHeaders, captureExperimentalSpanAttributes, false),
createProcessInstrumenter(
openTelemetry, capturedHeaders, captureExperimentalSpanAttributes, true),
batchReceiveInstrumenterBuilder.buildInstrumenter(SpanKindExtractor.alwaysConsumer()));
}
private static Instrumenter createProcessInstrumenter(
OpenTelemetry openTelemetry,
List capturedHeaders,
boolean captureExperimentalSpanAttributes,
boolean batch) {
RocketMqConsumerAttributeGetter getter = RocketMqConsumerAttributeGetter.INSTANCE;
MessageOperation operation = MessageOperation.PROCESS;
InstrumenterBuilder builder =
Instrumenter.builder(
openTelemetry,
INSTRUMENTATION_NAME,
MessagingSpanNameExtractor.create(getter, operation));
builder.addAttributesExtractor(
buildMessagingAttributesExtractor(getter, operation, capturedHeaders));
if (captureExperimentalSpanAttributes) {
builder.addAttributesExtractor(RocketMqConsumerExperimentalAttributeExtractor.INSTANCE);
}
if (batch) {
SpanLinksExtractor spanLinksExtractor =
new PropagatorBasedSpanLinksExtractor<>(
openTelemetry.getPropagators().getTextMapPropagator(),
TextMapExtractAdapter.INSTANCE);
return builder
.addSpanLinksExtractor(spanLinksExtractor)
.buildInstrumenter(SpanKindExtractor.alwaysConsumer());
} else {
return builder.buildConsumerInstrumenter(TextMapExtractAdapter.INSTANCE);
}
}
private static AttributesExtractor buildMessagingAttributesExtractor(
MessagingAttributesGetter getter,
MessageOperation operation,
List capturedHeaders) {
return MessagingAttributesExtractor.builder(getter, operation)
.setCapturedHeaders(capturedHeaders)
.build();
}
private static String spanNameOnReceive(Void unused) {
return "multiple_sources receive";
}
private RocketMqInstrumenterFactory() {}
}