|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.instrumentation.api.incubator.semconv.messaging; |
| 7 | + |
| 8 | +import static java.util.logging.Level.FINE; |
| 9 | + |
| 10 | +import com.google.auto.value.AutoValue; |
| 11 | +import com.google.errorprone.annotations.CanIgnoreReturnValue; |
| 12 | +import io.opentelemetry.api.common.AttributeKey; |
| 13 | +import io.opentelemetry.api.common.Attributes; |
| 14 | +import io.opentelemetry.api.metrics.DoubleHistogram; |
| 15 | +import io.opentelemetry.api.metrics.DoubleHistogramBuilder; |
| 16 | +import io.opentelemetry.api.metrics.LongCounter; |
| 17 | +import io.opentelemetry.api.metrics.LongCounterBuilder; |
| 18 | +import io.opentelemetry.api.metrics.Meter; |
| 19 | +import io.opentelemetry.context.Context; |
| 20 | +import io.opentelemetry.context.ContextKey; |
| 21 | +import io.opentelemetry.instrumentation.api.instrumenter.OperationListener; |
| 22 | +import io.opentelemetry.instrumentation.api.instrumenter.OperationMetrics; |
| 23 | +import io.opentelemetry.instrumentation.api.internal.OperationMetricsUtil; |
| 24 | +import java.util.concurrent.TimeUnit; |
| 25 | +import java.util.logging.Logger; |
| 26 | + |
| 27 | +/** |
| 28 | + * {@link OperationListener} which keeps track of <a |
| 29 | + * href="https://github.com/open-telemetry/semantic-conventions/blob/v1.26.0/docs/messaging/messaging-metrics.md#consumer-metrics">Consumer |
| 30 | + * metrics</a>. |
| 31 | + */ |
| 32 | +public final class MessagingConsumerMetrics implements OperationListener { |
| 33 | + private static final double NANOS_PER_S = TimeUnit.SECONDS.toNanos(1); |
| 34 | + |
| 35 | + // copied from MessagingIncubatingAttributes |
| 36 | + private static final AttributeKey<Long> MESSAGING_BATCH_MESSAGE_COUNT = |
| 37 | + AttributeKey.longKey("messaging.batch.message_count"); |
| 38 | + private static final ContextKey<MessagingConsumerMetrics.State> MESSAGING_CONSUMER_METRICS_STATE = |
| 39 | + ContextKey.named("messaging-consumer-metrics-state"); |
| 40 | + private static final Logger logger = Logger.getLogger(MessagingConsumerMetrics.class.getName()); |
| 41 | + |
| 42 | + private final DoubleHistogram receiveDurationHistogram; |
| 43 | + private final LongCounter receiveMessageCount; |
| 44 | + |
| 45 | + private MessagingConsumerMetrics(Meter meter) { |
| 46 | + DoubleHistogramBuilder durationBuilder = |
| 47 | + meter |
| 48 | + .histogramBuilder("messaging.receive.duration") |
| 49 | + .setDescription("Measures the duration of receive operation.") |
| 50 | + .setExplicitBucketBoundariesAdvice(MessagingMetricsAdvice.DURATION_SECONDS_BUCKETS) |
| 51 | + .setUnit("s"); |
| 52 | + MessagingMetricsAdvice.applyReceiveDurationAdvice(durationBuilder); |
| 53 | + receiveDurationHistogram = durationBuilder.build(); |
| 54 | + |
| 55 | + LongCounterBuilder longCounterBuilder = |
| 56 | + meter |
| 57 | + .counterBuilder("messaging.receive.messages") |
| 58 | + .setDescription("Measures the number of received messages.") |
| 59 | + .setUnit("{message}"); |
| 60 | + MessagingMetricsAdvice.applyReceiveMessagesAdvice(longCounterBuilder); |
| 61 | + receiveMessageCount = longCounterBuilder.build(); |
| 62 | + } |
| 63 | + |
| 64 | + public static OperationMetrics get() { |
| 65 | + return OperationMetricsUtil.create("messaging consumer", MessagingConsumerMetrics::new); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + @CanIgnoreReturnValue |
| 70 | + public Context onStart(Context context, Attributes startAttributes, long startNanos) { |
| 71 | + return context.with( |
| 72 | + MESSAGING_CONSUMER_METRICS_STATE, |
| 73 | + new AutoValue_MessagingConsumerMetrics_State(startAttributes, startNanos)); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void onEnd(Context context, Attributes endAttributes, long endNanos) { |
| 78 | + MessagingConsumerMetrics.State state = context.get(MESSAGING_CONSUMER_METRICS_STATE); |
| 79 | + if (state == null) { |
| 80 | + logger.log( |
| 81 | + FINE, |
| 82 | + "No state present when ending context {0}. Cannot record consumer receive metrics.", |
| 83 | + context); |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + Attributes attributes = state.startAttributes().toBuilder().putAll(endAttributes).build(); |
| 88 | + receiveDurationHistogram.record( |
| 89 | + (endNanos - state.startTimeNanos()) / NANOS_PER_S, attributes, context); |
| 90 | + |
| 91 | + long receiveMessagesCount = getReceiveMessagesCount(state.startAttributes(), endAttributes); |
| 92 | + receiveMessageCount.add(receiveMessagesCount, attributes, context); |
| 93 | + } |
| 94 | + |
| 95 | + private static long getReceiveMessagesCount(Attributes... attributesList) { |
| 96 | + for (Attributes attributes : attributesList) { |
| 97 | + Long value = attributes.get(MESSAGING_BATCH_MESSAGE_COUNT); |
| 98 | + if (value != null) { |
| 99 | + return value; |
| 100 | + } |
| 101 | + } |
| 102 | + return 1; |
| 103 | + } |
| 104 | + |
| 105 | + @AutoValue |
| 106 | + abstract static class State { |
| 107 | + |
| 108 | + abstract Attributes startAttributes(); |
| 109 | + |
| 110 | + abstract long startTimeNanos(); |
| 111 | + } |
| 112 | +} |
0 commit comments