Skip to content
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

Second attempt at fixing serializing kafka configuration #7789

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -126,15 +129,36 @@ void serializableConfig() throws IOException, ClassNotFoundException {
}

@SuppressWarnings("unchecked")
private static Map<String, Object> testSerialize(Map<String, Object> map)
private static void testSerialize(Map<String, Object> 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<String, Object>) inputStream.readObject();
new CustomObjectInputStream(new ByteArrayInputStream(byteOutputStream.toByteArray()))) {
Map<String, Object> result = (Map<String, Object>) inputStream.readObject();
assertThat(result.get(OpenTelemetryMetricsReporter.CONFIG_KEY_OPENTELEMETRY_SUPPLIER))
.isNull();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ public OpenTelemetrySupplier(OpenTelemetry openTelemetry) {
public OpenTelemetry get() {
return openTelemetry;
}

private Object writeReplace() {
// serialize this object to null
return null;
}
}