diff --git a/instrumentation/kafka/kafka-clients/kafka-clients-2.6/library/src/test/java/io/opentelemetry/instrumentation/kafka/internal/OpenTelemetryMetricsReporterTest.java b/instrumentation/kafka/kafka-clients/kafka-clients-2.6/library/src/test/java/io/opentelemetry/instrumentation/kafka/internal/OpenTelemetryMetricsReporterTest.java index 244a6426aaaf..ae1be7da6127 100644 --- a/instrumentation/kafka/kafka-clients/kafka-clients-2.6/library/src/test/java/io/opentelemetry/instrumentation/kafka/internal/OpenTelemetryMetricsReporterTest.java +++ b/instrumentation/kafka/kafka-clients/kafka-clients-2.6/library/src/test/java/io/opentelemetry/instrumentation/kafka/internal/OpenTelemetryMetricsReporterTest.java @@ -5,6 +5,7 @@ package io.opentelemetry.instrumentation.kafka.internal; +import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import io.opentelemetry.instrumentation.kafkaclients.KafkaTelemetry; @@ -13,8 +14,10 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.InputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; +import java.io.ObjectStreamClass; import java.util.Map; import org.apache.kafka.clients.consumer.KafkaConsumer; import org.apache.kafka.clients.producer.KafkaProducer; @@ -126,15 +129,39 @@ void serializableConfig() throws IOException, ClassNotFoundException { } @SuppressWarnings("unchecked") - private static Map testSerialize(Map map) + private static void testSerialize(Map map) throws IOException, ClassNotFoundException { + OpenTelemetrySupplier supplier = + (OpenTelemetrySupplier) + map.get(OpenTelemetryMetricsReporter.CONFIG_KEY_OPENTELEMETRY_SUPPLIER); + assertThat(supplier).isNotNull(); + assertThat(supplier.get()).isNotNull(); ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); try (ObjectOutputStream outputStream = new ObjectOutputStream(byteOutputStream)) { outputStream.writeObject(map); } + + class CustomObjectInputStream extends ObjectInputStream { + CustomObjectInputStream(InputStream inputStream) throws IOException { + super(inputStream); + } + + @Override + protected Class resolveClass(ObjectStreamClass desc) + throws IOException, ClassNotFoundException { + if (desc.getName().startsWith("io.opentelemetry.")) { + throw new IllegalStateException( + "Serial form contains opentelemetry class " + desc.getName()); + } + return super.resolveClass(desc); + } + } + try (ObjectInputStream inputStream = - new ObjectInputStream(new ByteArrayInputStream(byteOutputStream.toByteArray()))) { - return (Map) inputStream.readObject(); + new CustomObjectInputStream(new ByteArrayInputStream(byteOutputStream.toByteArray()))) { + Map result = (Map) inputStream.readObject(); + assertThat(result.get(OpenTelemetryMetricsReporter.CONFIG_KEY_OPENTELEMETRY_SUPPLIER)) + .isNull(); } } } diff --git a/instrumentation/kafka/kafka-clients/kafka-clients-common/library/src/main/java/io/opentelemetry/instrumentation/kafka/internal/OpenTelemetrySupplier.java b/instrumentation/kafka/kafka-clients/kafka-clients-common/library/src/main/java/io/opentelemetry/instrumentation/kafka/internal/OpenTelemetrySupplier.java index b3809370f902..6e2a09fb7a75 100644 --- a/instrumentation/kafka/kafka-clients/kafka-clients-common/library/src/main/java/io/opentelemetry/instrumentation/kafka/internal/OpenTelemetrySupplier.java +++ b/instrumentation/kafka/kafka-clients/kafka-clients-common/library/src/main/java/io/opentelemetry/instrumentation/kafka/internal/OpenTelemetrySupplier.java @@ -30,4 +30,9 @@ public OpenTelemetrySupplier(OpenTelemetry openTelemetry) { public OpenTelemetry get() { return openTelemetry; } + + private Object writeReplace() { + // serialize this object to null + return null; + } }