-
Notifications
You must be signed in to change notification settings - Fork 826
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement vertx-kafka-client instrumentation; batch processing
- Loading branch information
Showing
15 changed files
with
725 additions
and
65 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
67 changes: 67 additions & 0 deletions
67
...telemetry/javaagent/instrumentation/vertx/kafka/v3_6/InstrumentedBatchRecordsHandler.java
This file contains 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,67 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.vertx.kafka.v3_6; | ||
|
||
import static io.opentelemetry.javaagent.instrumentation.vertx.kafka.v3_6.VertxKafkaSingletons.batchProcessInstrumenter; | ||
|
||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.Scope; | ||
import io.opentelemetry.instrumentation.api.field.VirtualField; | ||
import io.opentelemetry.javaagent.bootstrap.kafka.KafkaClientsConsumerProcessTracing; | ||
import io.vertx.core.Handler; | ||
import javax.annotation.Nullable; | ||
import org.apache.kafka.clients.consumer.ConsumerRecords; | ||
|
||
public final class InstrumentedBatchRecordsHandler<K, V> implements Handler<ConsumerRecords<K, V>> { | ||
|
||
private final VirtualField<ConsumerRecords<K, V>, Context> receiveContextField; | ||
@Nullable private final Handler<ConsumerRecords<K, V>> delegate; | ||
|
||
public InstrumentedBatchRecordsHandler( | ||
VirtualField<ConsumerRecords<K, V>, Context> receiveContextField, | ||
@Nullable Handler<ConsumerRecords<K, V>> delegate) { | ||
this.receiveContextField = receiveContextField; | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override | ||
public void handle(ConsumerRecords<K, V> records) { | ||
Context parentContext = getParentContext(records); | ||
|
||
if (!batchProcessInstrumenter().shouldStart(parentContext, records)) { | ||
callDelegateHandler(records); | ||
return; | ||
} | ||
|
||
// the instrumenter iterates over records when adding links, we need to suppress that | ||
boolean previousWrappingEnabled = KafkaClientsConsumerProcessTracing.setEnabled(false); | ||
|
||
Context context = batchProcessInstrumenter().start(parentContext, records); | ||
Throwable error = null; | ||
try (Scope ignored = context.makeCurrent()) { | ||
callDelegateHandler(records); | ||
} catch (Throwable t) { | ||
error = t; | ||
throw t; | ||
} finally { | ||
batchProcessInstrumenter().end(context, records, null, error); | ||
KafkaClientsConsumerProcessTracing.setEnabled(previousWrappingEnabled); | ||
} | ||
} | ||
|
||
private Context getParentContext(ConsumerRecords<K, V> records) { | ||
Context receiveContext = receiveContextField.get(records); | ||
|
||
// use the receive CONSUMER span as parent if it's available | ||
return receiveContext != null ? receiveContext : Context.current(); | ||
} | ||
|
||
private void callDelegateHandler(ConsumerRecords<K, V> records) { | ||
if (delegate != null) { | ||
delegate.handle(records); | ||
} | ||
} | ||
} |
This file contains 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 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 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.