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

Test otel proto exporter v2 #268

Draft
wants to merge 20 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
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 @@ -2,6 +2,8 @@

import io.opentelemetry.exporter.prometheus.PrometheusCollector;
import io.opentelemetry.sdk.metrics.export.MetricReader;
import io.prometheus.client.Collector;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.exporter.MetricsServlet;
import java.io.IOException;
import javax.servlet.ServletException;
Expand All @@ -11,9 +13,13 @@
public class MetricsExporterServlet extends MetricsServlet {
private InMemoryMetricsProducer inMemoryMetricsProducer;
private MetricReader metricReader;
private Collector collector;
private static final CollectorRegistry collectorRegistry = new CollectorRegistry(false);

public MetricsExporterServlet(InMemoryMetricsProducer producer) {
metricReader = PrometheusCollector.create().apply(producer);
super(collectorRegistry);
collector = PrometheusCollector.getNewInstance(producer);
collectorRegistry.register(collector);
inMemoryMetricsProducer = producer;
}

Expand All @@ -22,6 +28,7 @@ protected void doGet(final HttpServletRequest req, final HttpServletResponse res
throws ServletException, IOException {
try {
super.doGet(req, resp);
// List<MetricFamilySamples> test = collector.collect();
inMemoryMetricsProducer.setCommitOffset();
} catch (ServletException e) {
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,14 @@ private List<io.opentelemetry.proto.common.v1.KeyValue> toAttributes(Map<String,
List<io.opentelemetry.proto.common.v1.KeyValue> attributes =
labels.entrySet().stream()
.map(
k ->
io.opentelemetry.proto.common.v1.KeyValue.newBuilder()
.setKey(k.getKey())
.setValue(AnyValue.newBuilder().setStringValue(k.getValue()).build())
.build())
k -> {
io.opentelemetry.proto.common.v1.KeyValue.Builder keyValueBuilder =
io.opentelemetry.proto.common.v1.KeyValue.newBuilder();
keyValueBuilder.setKey(k.getKey());
String value = k.getValue() != null ? k.getValue() : "";
keyValueBuilder.setValue(AnyValue.newBuilder().setStringValue(value).build());
return keyValueBuilder.build();
})
.collect(Collectors.toList());
return attributes;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
import org.apache.kafka.common.serialization.Deserializer;
import org.apache.kafka.common.serialization.Serde;
import org.apache.kafka.common.serialization.Serializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class OtelMetricsSerde implements Serde<ResourceMetrics> {
private static final Logger LOGGER = LoggerFactory.getLogger(OtelMetricsSerde.class);

@Override
public void configure(Map<String, ?> configs, boolean isKey) {}
Expand All @@ -28,16 +31,23 @@ public Deserializer<ResourceMetrics> deserializer() {
public static class Ser implements Serializer<ResourceMetrics> {
@Override
public byte[] serialize(String topic, ResourceMetrics data) {
return data.toByteArray();
try {
return data.toByteArray();
} catch (Exception e) {
LOGGER.error("serilzation error:", e);
}
return null;
}
}

public static class De implements Deserializer<ResourceMetrics> {
@Override
public ResourceMetrics deserialize(String topic, byte[] data) {
try {
LOGGER.info("deserilize:{}", OtelMetricsSerde.class.getName());
return ResourceMetrics.parseFrom(data);
} catch (InvalidProtocolBufferException e) {
LOGGER.error("error:", e);
throw new RuntimeException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@
import org.apache.kafka.streams.KeyValue;
import org.apache.kafka.streams.kstream.Transformer;
import org.apache.kafka.streams.processor.ProcessorContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MetricsEnricher
implements Transformer<byte[], ResourceMetrics, KeyValue<byte[], ResourceMetrics>> {
private static final Logger logger = LoggerFactory.getLogger(MetricsEnricher.class);

@Override
public void init(ProcessorContext context) {}

@Override
public KeyValue<byte[], ResourceMetrics> transform(byte[] key, ResourceMetrics value) {
// noop enricher for now
logger.info("got the value in MetricsEnricher");
return new KeyValue<>(key, value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
import org.apache.kafka.common.serialization.Deserializer;
import org.apache.kafka.common.serialization.Serde;
import org.apache.kafka.common.serialization.Serializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class OtlpMetricsSerde implements Serde<ResourceMetrics> {
private static final Logger logger = LoggerFactory.getLogger(OtlpMetricsSerde.class);

@Override
public void configure(Map<String, ?> configs, boolean isKey) {}
Expand Down Expand Up @@ -36,8 +39,10 @@ public static class De implements Deserializer<ResourceMetrics> {
@Override
public ResourceMetrics deserialize(String topic, byte[] data) {
try {
logger.info("Desereized from: {}", OtlpMetricsSerde.class.getName());
return ResourceMetrics.parseFrom(data);
} catch (InvalidProtocolBufferException e) {
logger.error("Failed in deserilzed", e);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
logger.error("Failed in deserilzed", e);
logger.error("Failed in deserialized", e);

throw new RuntimeException(e);
}
}
Expand Down