-
Notifications
You must be signed in to change notification settings - Fork 945
Add Pulsar Consumer metrics #11891
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
trask
merged 35 commits into
open-telemetry:main
from
crossoverJie:pulsar-receive-duration
Aug 15, 2024
Merged
Add Pulsar Consumer metrics #11891
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
b954474
support pulsar messaging.publish.duration semantic
crossoverJie d855f97
code style
crossoverJie b5d512b
Update instrumentation-api-incubator/src/main/java/io/opentelemetry/i…
crossoverJie 3de71a8
Update instrumentation-api-incubator/src/main/java/io/opentelemetry/i…
crossoverJie 6bb2c90
Update instrumentation-api-incubator/src/test/java/io/opentelemetry/i…
crossoverJie 6f68ebd
Update instrumentation-api-incubator/src/test/java/io/opentelemetry/i…
crossoverJie 9f42598
fix with cr
crossoverJie 8b5e585
Add pulsar metrics test
crossoverJie 53b2dee
Update doc url
crossoverJie 53b4471
pulsar-receive-duration
crossoverJie df187f4
use array
crossoverJie e2d5e09
use array
crossoverJie 8651722
Merge branch 'main' into pulsar-receive-duration
crossoverJie 52405fd
pulsar-receive-duration
crossoverJie 8bfc6fd
support messaging.receive.messages
crossoverJie 61d402d
Merge remote-tracking branch 'otel-origin/main' into pulsar-receive-d…
crossoverJie fde08c4
support messaging.receive.messages
crossoverJie 7991bef
Add a test for Partition Consumer
crossoverJie 3ca21a9
fix with cr
crossoverJie e889e16
Use RecordMetadata instead of void for kafka
crossoverJie 2c7b92e
Use RecordMetadata instead of void for kafka
crossoverJie 9710b0d
Use RecordMetadata instead of void for kafka
crossoverJie 84c9fbe
Use RecordMetadata instead of void for kafka
crossoverJie a0d0cd8
check recordMetadata is null
crossoverJie 6780c4d
revert about kafka
crossoverJie dfc799c
Merge remote-tracking branch 'origin/pulsar-receive-duration' into pu…
crossoverJie fe04048
revert about kafka
crossoverJie c849a3b
revert about kafka
crossoverJie 8573d0e
revert about kafka
crossoverJie 8bb50f5
revert about kafka
crossoverJie 99c8256
revert about kafka
crossoverJie 807d60b
fix with cr
crossoverJie 06e8b8e
Update instrumentation-api-incubator/src/main/java/io/opentelemetry/i…
laurit 76b103d
fix with cr
crossoverJie cd83eed
fix with cr
crossoverJie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
...entelemetry/instrumentation/api/incubator/semconv/messaging/MessagingConsumerMetrics.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.api.incubator.semconv.messaging; | ||
|
||
import static java.util.logging.Level.FINE; | ||
|
||
import com.google.auto.value.AutoValue; | ||
import com.google.errorprone.annotations.CanIgnoreReturnValue; | ||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.api.metrics.DoubleHistogram; | ||
import io.opentelemetry.api.metrics.DoubleHistogramBuilder; | ||
import io.opentelemetry.api.metrics.LongCounter; | ||
import io.opentelemetry.api.metrics.LongCounterBuilder; | ||
import io.opentelemetry.api.metrics.Meter; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.ContextKey; | ||
import io.opentelemetry.instrumentation.api.instrumenter.OperationListener; | ||
import io.opentelemetry.instrumentation.api.instrumenter.OperationMetrics; | ||
import io.opentelemetry.instrumentation.api.internal.OperationMetricsUtil; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.logging.Logger; | ||
|
||
/** | ||
* {@link OperationListener} which keeps track of <a | ||
* href="https://github.com/open-telemetry/semantic-conventions/blob/v1.26.0/docs/messaging/messaging-metrics.md#consumer-metrics">Consumer | ||
* metrics</a>. | ||
*/ | ||
public final class MessagingConsumerMetrics implements OperationListener { | ||
private static final double NANOS_PER_S = TimeUnit.SECONDS.toNanos(1); | ||
|
||
// copied from MessagingIncubatingAttributes | ||
private static final AttributeKey<Long> MESSAGING_BATCH_MESSAGE_COUNT = | ||
AttributeKey.longKey("messaging.batch.message_count"); | ||
private static final ContextKey<MessagingConsumerMetrics.State> MESSAGING_CONSUMER_METRICS_STATE = | ||
ContextKey.named("messaging-consumer-metrics-state"); | ||
private static final Logger logger = Logger.getLogger(MessagingConsumerMetrics.class.getName()); | ||
|
||
private final DoubleHistogram receiveDurationHistogram; | ||
private final LongCounter receiveMessageCount; | ||
|
||
private MessagingConsumerMetrics(Meter meter) { | ||
DoubleHistogramBuilder durationBuilder = | ||
meter | ||
.histogramBuilder("messaging.receive.duration") | ||
.setDescription("Measures the duration of receive operation.") | ||
.setExplicitBucketBoundariesAdvice(MessagingMetricsAdvice.DURATION_SECONDS_BUCKETS) | ||
.setUnit("s"); | ||
MessagingMetricsAdvice.applyReceiveDurationAdvice(durationBuilder); | ||
receiveDurationHistogram = durationBuilder.build(); | ||
|
||
LongCounterBuilder longCounterBuilder = | ||
meter | ||
.counterBuilder("messaging.receive.messages") | ||
.setDescription("Measures the number of received messages.") | ||
.setUnit("{message}"); | ||
MessagingMetricsAdvice.applyReceiveMessagesAdvice(longCounterBuilder); | ||
receiveMessageCount = longCounterBuilder.build(); | ||
} | ||
|
||
public static OperationMetrics get() { | ||
return OperationMetricsUtil.create("messaging consumer", MessagingConsumerMetrics::new); | ||
} | ||
|
||
@Override | ||
@CanIgnoreReturnValue | ||
public Context onStart(Context context, Attributes startAttributes, long startNanos) { | ||
return context.with( | ||
MESSAGING_CONSUMER_METRICS_STATE, | ||
new AutoValue_MessagingConsumerMetrics_State(startAttributes, startNanos)); | ||
} | ||
|
||
@Override | ||
public void onEnd(Context context, Attributes endAttributes, long endNanos) { | ||
MessagingConsumerMetrics.State state = context.get(MESSAGING_CONSUMER_METRICS_STATE); | ||
if (state == null) { | ||
logger.log( | ||
FINE, | ||
"No state present when ending context {0}. Cannot record consumer receive metrics.", | ||
context); | ||
return; | ||
} | ||
|
||
Attributes attributes = state.startAttributes().toBuilder().putAll(endAttributes).build(); | ||
receiveDurationHistogram.record( | ||
(endNanos - state.startTimeNanos()) / NANOS_PER_S, attributes, context); | ||
|
||
long receiveMessagesCount = getReceiveMessagesCount(state.startAttributes(), endAttributes); | ||
receiveMessageCount.add(receiveMessagesCount, attributes, context); | ||
} | ||
|
||
private static long getReceiveMessagesCount(Attributes... attributesList) { | ||
for (Attributes attributes : attributesList) { | ||
Long value = attributes.get(MESSAGING_BATCH_MESSAGE_COUNT); | ||
if (value != null) { | ||
return value; | ||
} | ||
} | ||
return 1; | ||
} | ||
|
||
@AutoValue | ||
abstract static class State { | ||
|
||
abstract Attributes startAttributes(); | ||
|
||
abstract long startTimeNanos(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we go ahead and update to the latest spec to avoid additional churn? https://github.com/open-telemetry/semantic-conventions/blob/main/docs/messaging/messaging-metrics.md
cc @open-telemetry/semconv-messaging-approvers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
semConvVersion
hasn't been upgraded to the latest version yet. Is it okay to only upgrade some semantics?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes it's ok, if we don't have the new constants available yet, you can create static final constants in this class and reference those