-
Notifications
You must be signed in to change notification settings - Fork 782
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move kafka metrics to separate instrumentation module (#9862)
- Loading branch information
Showing
8 changed files
with
218 additions
and
111 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
61 changes: 61 additions & 0 deletions
61
...agent/instrumentation/kafkaclients/v0_11/metrics/KafkaMetricsConsumerInstrumentation.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,61 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.kafkaclients.v0_11.metrics; | ||
|
||
import static io.opentelemetry.javaagent.instrumentation.kafkaclients.v0_11.metrics.KafkaMetricsUtil.enhanceConfig; | ||
import static net.bytebuddy.matcher.ElementMatchers.isConstructor; | ||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
|
||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
public class KafkaMetricsConsumerInstrumentation implements TypeInstrumentation { | ||
|
||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return named("org.apache.kafka.clients.consumer.KafkaConsumer"); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
transformer.applyAdviceToMethod( | ||
isConstructor().and(takesArgument(0, Map.class)), | ||
this.getClass().getName() + "$ConstructorMapAdvice"); | ||
transformer.applyAdviceToMethod( | ||
isConstructor().and(takesArgument(0, Properties.class)), | ||
this.getClass().getName() + "$ConstructorPropertiesAdvice"); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class ConstructorMapAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void onEnter( | ||
@Advice.Argument(value = 0, readOnly = false) Map<String, Object> config) { | ||
// ensure config is a mutable map | ||
if (config.getClass() != HashMap.class) { | ||
config = new HashMap<>(config); | ||
} | ||
enhanceConfig(config); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class ConstructorPropertiesAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void onEnter(@Advice.Argument(0) Properties config) { | ||
enhanceConfig(config); | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...vaagent/instrumentation/kafkaclients/v0_11/metrics/KafkaMetricsInstrumentationModule.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,37 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.kafkaclients.v0_11.metrics; | ||
|
||
import static java.util.Arrays.asList; | ||
|
||
import com.google.auto.service.AutoService; | ||
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import java.util.List; | ||
|
||
@AutoService(InstrumentationModule.class) | ||
public class KafkaMetricsInstrumentationModule extends InstrumentationModule { | ||
public KafkaMetricsInstrumentationModule() { | ||
super( | ||
"kafka-clients-metrics", | ||
"kafka-clients", | ||
"kafka-clients-metrics-0.11", | ||
"kafka-clients-0.11", | ||
"kafka"); | ||
} | ||
|
||
@Override | ||
public boolean isIndyModule() { | ||
// OpenTelemetryMetricsReporter is not available in app class loader | ||
return false; | ||
} | ||
|
||
@Override | ||
public List<TypeInstrumentation> typeInstrumentations() { | ||
return asList( | ||
new KafkaMetricsProducerInstrumentation(), new KafkaMetricsConsumerInstrumentation()); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...agent/instrumentation/kafkaclients/v0_11/metrics/KafkaMetricsProducerInstrumentation.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,61 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.kafkaclients.v0_11.metrics; | ||
|
||
import static io.opentelemetry.javaagent.instrumentation.kafkaclients.v0_11.metrics.KafkaMetricsUtil.enhanceConfig; | ||
import static net.bytebuddy.matcher.ElementMatchers.isConstructor; | ||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
|
||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
public class KafkaMetricsProducerInstrumentation implements TypeInstrumentation { | ||
|
||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return named("org.apache.kafka.clients.producer.KafkaProducer"); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
transformer.applyAdviceToMethod( | ||
isConstructor().and(takesArgument(0, Map.class)), | ||
this.getClass().getName() + "$ConstructorMapAdvice"); | ||
transformer.applyAdviceToMethod( | ||
isConstructor().and(takesArgument(0, Properties.class)), | ||
this.getClass().getName() + "$ConstructorPropertiesAdvice"); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class ConstructorMapAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void onEnter( | ||
@Advice.Argument(value = 0, readOnly = false) Map<String, Object> config) { | ||
// ensure config is a mutable map | ||
if (config.getClass() != HashMap.class) { | ||
config = new HashMap<>(config); | ||
} | ||
enhanceConfig(config); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class ConstructorPropertiesAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void onEnter(@Advice.Argument(0) Properties config) { | ||
enhanceConfig(config); | ||
} | ||
} | ||
} |
Oops, something went wrong.