-
Notifications
You must be signed in to change notification settings - Fork 1k
Add configurable OpenTelemetry kafka-clients interceptors #14929
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
Open
trask
wants to merge
51
commits into
open-telemetry:main
Choose a base branch
from
trask:kafka-config
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 31 commits
Commits
Show all changes
51 commits
Select commit
Hold shift + click to select a range
2a012ae
Initial plan
Copilot 34d68ec
Initial plan for refactoring kafka interceptors
Copilot 3b68915
Refactor kafka interceptors to be configurable
Copilot ce0eafc
Update README with interceptor configuration documentation
Copilot b187791
Follow OpenTelemetryMetricsReporter pattern for property validation
Copilot 85ec089
Avoid changing KafkaTelemetry - move config keys to interceptors
Copilot 5f6294b
Use OpenTelemetrySupplier instead of KafkaTelemetrySupplier
Copilot e5ca107
Clean up unused code and use imports instead of FQCN
Copilot e791fc8
Add helper methods to KafkaTelemetry for interceptor config
Copilot 97cbf9d
Include INTERCEPTOR_CLASSES_CONFIG in helper methods
Copilot 4f3d30d
Use KafkaTelemetrySupplier to pass exact KafkaTelemetry instance
Copilot a31210c
Remove experimental from config key, simplify fallback, add test
Copilot 84e69ad
Refactor kafka-clients interceptors to be configurable
Copilot 5419f03
Restore system property checks in fallback with TODO comment
Copilot f4e568a
Use import instead of FQCN for Supplier in test
Copilot 82248c4
Apply suggestions from code review
trask f1e7dde
./gradlew spotlessApply
otelbot[bot] 7703b1d
Create new OpenTelemetry interceptors, keep old Tracing ones for back…
Copilot 95d1723
Mark TracingConsumerInterceptor and TracingProducerInterceptor as dep…
Copilot 3fdd463
Update instrumentation/kafka/kafka-clients/kafka-clients-2.6/library/…
trask f72ae47
Update instrumentation/kafka/kafka-clients/kafka-clients-2.6/library/…
trask 2bc5492
Test deprecated classes until they're removed
trask f24a0a4
fix
trask 45c7510
fix
trask 85aaaaa
fix
trask 9b9b075
./gradlew spotlessApply
otelbot[bot] 6aa6306
fix
trask a541ee6
fix
trask 06184d1
docs
trask 30bc6dd
internal
trask b207b25
simplify
trask a2b44cc
Apply suggestions from code review
trask d861b88
KafkaHelper
trask 3c4f97f
fixes
trask ff96054
split
trask 61813b5
split2
trask 41c767c
split test
trask 810c222
more
trask a2286d8
up
trask 536c1cd
up
trask bbd21cb
fix
trask 385bc15
up
trask 84c9212
up
trask 686de0c
up
trask eb7ecf9
up
trask 3fe741b
up
trask 4f58c8d
revert
trask 45c3e11
more
trask 96bc1c9
./gradlew spotlessApply
otelbot[bot] 44975a6
javadoc
trask d31e213
Merge remote-tracking branch 'upstream/main' into kafka-config
trask 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
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
38 changes: 38 additions & 0 deletions
38
...a/io/opentelemetry/instrumentation/kafkaclients/v2_6/internal/KafkaTelemetrySupplier.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,38 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.kafkaclients.v2_6.internal; | ||
|
||
import io.opentelemetry.instrumentation.kafkaclients.v2_6.KafkaTelemetry; | ||
import java.io.Serializable; | ||
import java.util.Objects; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Wrapper for KafkaTelemetry that can be injected into kafka configuration without breaking | ||
* serialization. | ||
* | ||
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change | ||
* at any time. | ||
*/ | ||
public final class KafkaTelemetrySupplier implements Supplier<KafkaTelemetry>, Serializable { | ||
private static final long serialVersionUID = 1L; | ||
private final transient KafkaTelemetry kafkaTelemetry; | ||
|
||
public KafkaTelemetrySupplier(KafkaTelemetry kafkaTelemetry) { | ||
Objects.requireNonNull(kafkaTelemetry); | ||
this.kafkaTelemetry = kafkaTelemetry; | ||
} | ||
|
||
@Override | ||
public KafkaTelemetry get() { | ||
return kafkaTelemetry; | ||
} | ||
|
||
private Object writeReplace() { | ||
// serialize this object to null | ||
return null; | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
...elemetry/instrumentation/kafkaclients/v2_6/internal/OpenTelemetryConsumerInterceptor.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,88 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.kafkaclients.v2_6.internal; | ||
|
||
import com.google.errorprone.annotations.CanIgnoreReturnValue; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.instrumentation.api.internal.Timer; | ||
import io.opentelemetry.instrumentation.kafkaclients.common.v0_11.internal.KafkaConsumerContext; | ||
import io.opentelemetry.instrumentation.kafkaclients.common.v0_11.internal.KafkaConsumerContextUtil; | ||
import io.opentelemetry.instrumentation.kafkaclients.v2_6.KafkaTelemetry; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import javax.annotation.Nullable; | ||
import org.apache.kafka.clients.consumer.ConsumerConfig; | ||
import org.apache.kafka.clients.consumer.ConsumerInterceptor; | ||
import org.apache.kafka.clients.consumer.ConsumerRecords; | ||
import org.apache.kafka.clients.consumer.OffsetAndMetadata; | ||
import org.apache.kafka.common.TopicPartition; | ||
|
||
/** | ||
* A ConsumerInterceptor that adds OpenTelemetry instrumentation. | ||
* | ||
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change | ||
* at any time. | ||
*/ | ||
public class OpenTelemetryConsumerInterceptor<K, V> implements ConsumerInterceptor<K, V> { | ||
|
||
public static final String CONFIG_KEY_KAFKA_TELEMETRY_SUPPLIER = | ||
"opentelemetry.kafka-telemetry.supplier"; | ||
|
||
@Nullable private KafkaTelemetry telemetry; | ||
private String consumerGroup; | ||
private String clientId; | ||
|
||
@Override | ||
@CanIgnoreReturnValue | ||
public ConsumerRecords<K, V> onConsume(ConsumerRecords<K, V> records) { | ||
if (telemetry == null) { | ||
return records; | ||
} | ||
// timer should be started before fetching ConsumerRecords, but there is no callback for that | ||
Timer timer = Timer.start(); | ||
Context receiveContext = telemetry.buildAndFinishSpan(records, consumerGroup, clientId, timer); | ||
if (receiveContext == null) { | ||
receiveContext = Context.current(); | ||
} | ||
KafkaConsumerContext consumerContext = | ||
KafkaConsumerContextUtil.create(receiveContext, consumerGroup, clientId); | ||
return telemetry.addTracing(records, consumerContext); | ||
} | ||
|
||
@Override | ||
public void onCommit(Map<TopicPartition, OffsetAndMetadata> offsets) {} | ||
|
||
@Override | ||
public void close() {} | ||
|
||
@Override | ||
public void configure(Map<String, ?> configs) { | ||
consumerGroup = Objects.toString(configs.get(ConsumerConfig.GROUP_ID_CONFIG), null); | ||
clientId = Objects.toString(configs.get(ConsumerConfig.CLIENT_ID_CONFIG), null); | ||
|
||
Object telemetrySupplier = configs.get(CONFIG_KEY_KAFKA_TELEMETRY_SUPPLIER); | ||
if (telemetrySupplier == null) { | ||
return; | ||
} | ||
|
||
KafkaTelemetrySupplier supplier = | ||
getProperty(configs, CONFIG_KEY_KAFKA_TELEMETRY_SUPPLIER, KafkaTelemetrySupplier.class); | ||
this.telemetry = supplier.get(); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private static <T> T getProperty(Map<String, ?> configs, String key, Class<T> requiredType) { | ||
Object value = configs.get(key); | ||
if (value == null) { | ||
throw new IllegalStateException("Missing required configuration property: " + key); | ||
} | ||
if (!requiredType.isInstance(value)) { | ||
throw new IllegalStateException( | ||
"Configuration property " + key + " is not instance of " + requiredType.getSimpleName()); | ||
} | ||
return (T) value; | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
...elemetry/instrumentation/kafkaclients/v2_6/internal/OpenTelemetryProducerInterceptor.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,73 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.kafkaclients.v2_6.internal; | ||
|
||
import com.google.errorprone.annotations.CanIgnoreReturnValue; | ||
import io.opentelemetry.instrumentation.kafkaclients.v2_6.KafkaTelemetry; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import javax.annotation.Nullable; | ||
import org.apache.kafka.clients.producer.ProducerConfig; | ||
import org.apache.kafka.clients.producer.ProducerInterceptor; | ||
import org.apache.kafka.clients.producer.ProducerRecord; | ||
import org.apache.kafka.clients.producer.RecordMetadata; | ||
|
||
/** | ||
* A ProducerInterceptor that adds OpenTelemetry instrumentation. | ||
* | ||
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change | ||
* at any time. | ||
*/ | ||
public class OpenTelemetryProducerInterceptor<K, V> implements ProducerInterceptor<K, V> { | ||
|
||
public static final String CONFIG_KEY_KAFKA_TELEMETRY_SUPPLIER = | ||
"opentelemetry.kafka-telemetry.supplier"; | ||
|
||
@Nullable private KafkaTelemetry telemetry; | ||
@Nullable private String clientId; | ||
|
||
@Override | ||
@CanIgnoreReturnValue | ||
public ProducerRecord<K, V> onSend(ProducerRecord<K, V> producerRecord) { | ||
if (telemetry != null) { | ||
telemetry.buildAndInjectSpan(producerRecord, clientId); | ||
} | ||
return producerRecord; | ||
} | ||
|
||
@Override | ||
public void onAcknowledgement(RecordMetadata recordMetadata, Exception e) {} | ||
|
||
@Override | ||
public void close() {} | ||
|
||
@Override | ||
public void configure(Map<String, ?> configs) { | ||
clientId = Objects.toString(configs.get(ProducerConfig.CLIENT_ID_CONFIG), null); | ||
|
||
Object telemetrySupplier = configs.get(CONFIG_KEY_KAFKA_TELEMETRY_SUPPLIER); | ||
if (telemetrySupplier == null) { | ||
return; | ||
} | ||
|
||
trask marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
KafkaTelemetrySupplier supplier = | ||
getProperty(configs, CONFIG_KEY_KAFKA_TELEMETRY_SUPPLIER, KafkaTelemetrySupplier.class); | ||
this.telemetry = supplier.get(); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private static <T> T getProperty(Map<String, ?> configs, String key, Class<T> requiredType) { | ||
Object value = configs.get(key); | ||
if (value == null) { | ||
throw new IllegalStateException("Missing required configuration property: " + key); | ||
} | ||
if (!requiredType.isInstance(value)) { | ||
throw new IllegalStateException( | ||
"Configuration property " + key + " is not instance of " + requiredType.getSimpleName()); | ||
} | ||
return (T) value; | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.