Search in sources :

Example 1 with Gauge

use of io.opentelemetry.proto.metrics.v1.Gauge in project hypertrace-ingester by hypertrace.

the class MetricsProcessorTest method testMetricsProcessorTopology.

@Test
@SetEnvironmentVariable(key = "SERVICE_NAME", value = "hypertrace-metrics-processor")
public void testMetricsProcessorTopology() {
    // prepare stream properties
    Map<String, Object> mergedProps = new HashMap<>();
    underTest.getBaseStreamsConfig().forEach(mergedProps::put);
    underTest.getStreamsConfig(underTestConfig).forEach(mergedProps::put);
    mergedProps.put(underTest.getJobConfigKey(), underTestConfig);
    StreamsBuilder streamsBuilder = underTest.buildTopology(mergedProps, new StreamsBuilder(), new HashMap<>());
    Properties props = new Properties();
    mergedProps.forEach(props::put);
    // create topology test driver, and i/o topics
    TopologyTestDriver topologyTestDriver = new TopologyTestDriver(streamsBuilder.build(), props);
    TestInputTopic<byte[], ResourceMetrics> inputTopic = topologyTestDriver.createInputTopic(underTestConfig.getString(MetricsProcessor.INPUT_TOPIC_CONFIG_KEY), Serdes.ByteArray().serializer(), new OtlpMetricsSerde().serializer());
    TestOutputTopic outputTopic = topologyTestDriver.createOutputTopic(underTestConfig.getString(MetricsProcessor.OUTPUT_TOPIC_CONFIG_KEY), Serdes.ByteArray().deserializer(), new OtlpMetricsSerde().deserializer());
    // create resource metrics and add into pipeline
    ResourceMetrics inputResourceMetrics = getTestMetricsGauge("num_calls", "number of calls", 20L);
    inputTopic.pipeInput(inputResourceMetrics);
    ResourceMetrics outputResourceMetrics = (ResourceMetrics) outputTopic.readValue();
    // verification
    Assertions.assertNotNull(outputResourceMetrics);
    Assertions.assertNotNull(outputResourceMetrics.getResource());
    Assertions.assertEquals(1, outputResourceMetrics.getInstrumentationLibraryMetricsCount());
    Assertions.assertEquals(1, outputResourceMetrics.getInstrumentationLibraryMetrics(0).getMetricsCount());
    Assertions.assertEquals("num_calls", outputResourceMetrics.getInstrumentationLibraryMetrics(0).getMetrics(0).getName());
    // data points verification
    Gauge outGauge = outputResourceMetrics.getInstrumentationLibraryMetrics(0).getMetrics(0).getGauge();
    Assertions.assertNotNull(outGauge);
    Assertions.assertEquals(1, outGauge.getDataPointsCount());
    Assertions.assertEquals(1634119810000L, TimeUnit.MILLISECONDS.convert(outGauge.getDataPoints(0).getTimeUnixNano(), TimeUnit.NANOSECONDS));
    Assertions.assertEquals(3, outGauge.getDataPoints(0).getAttributesCount());
    Assertions.assertEquals(20L, outGauge.getDataPoints(0).getAsInt());
}
Also used : HashMap(java.util.HashMap) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) Properties(java.util.Properties) Gauge(io.opentelemetry.proto.metrics.v1.Gauge) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) ResourceMetrics(io.opentelemetry.proto.metrics.v1.ResourceMetrics) TestOutputTopic(org.apache.kafka.streams.TestOutputTopic) SetEnvironmentVariable(org.junitpioneer.jupiter.SetEnvironmentVariable) Test(org.junit.jupiter.api.Test)

Example 2 with Gauge

use of io.opentelemetry.proto.metrics.v1.Gauge in project hypertrace-ingester by hypertrace.

the class OtlpProtoToMetricDataConverter method toGaugeMetricData.

private static MetricData toGaugeMetricData(Resource resource, InstrumentationLibraryInfo instrumentationLibraryInfo, Metric metric) {
    Gauge gaugeMetric = metric.getGauge();
    DoubleGaugeData data = DoubleGaugeData.create(toDoublePointData(gaugeMetric.getDataPointsList()));
    return MetricData.createDoubleGauge(resource, instrumentationLibraryInfo, metric.getName(), metric.getDescription(), metric.getUnit(), data);
}
Also used : DoubleGaugeData(io.opentelemetry.sdk.metrics.data.DoubleGaugeData) Gauge(io.opentelemetry.proto.metrics.v1.Gauge)

Example 3 with Gauge

use of io.opentelemetry.proto.metrics.v1.Gauge in project hypertrace-ingester by hypertrace.

the class InMemoryMetricsProducerTest method testAddMetricDataAndCollectData.

@Test
public void testAddMetricDataAndCollectData() {
    // insert 1 data
    ResourceMetrics resourceMetrics = prepareMetric("int_num_calls", "number of calls", 1, "Gauge");
    List<MetricData> inMetricData = OtlpProtoToMetricDataConverter.toMetricData(resourceMetrics);
    Assertions.assertTrue(underTest.addMetricData(inMetricData));
    // insert 2nd data
    ResourceMetrics resourceMetrics1 = prepareMetric("double_num_calls", "number of calls", 2.5, "Gauge");
    List<MetricData> inMetricData1 = OtlpProtoToMetricDataConverter.toMetricData(resourceMetrics1);
    // assert that can't add
    Assertions.assertTrue(underTest.addMetricData(inMetricData1));
    // insert 3nd data
    ResourceMetrics resourceMetrics2 = prepareMetric("double_num_calls", "number of calls", 3.5, "Gauge");
    List<MetricData> inMetricData2 = OtlpProtoToMetricDataConverter.toMetricData(resourceMetrics2);
    // assert that can't add
    Assertions.assertFalse(underTest.addMetricData(inMetricData2));
    // Now read data
    List<MetricData> outData = (List) underTest.collectAllMetrics();
    Assertions.assertEquals(1, outData.size());
    Assertions.assertEquals(inMetricData.get(0), outData.get(0));
    outData = (List) underTest.collectAllMetrics();
    Assertions.assertEquals(1, outData.size());
    Assertions.assertEquals(inMetricData1.get(0), outData.get(0));
    outData = (List) underTest.collectAllMetrics();
    Assertions.assertEquals(0, outData.size());
    // reinsert 3rd data point
    Assertions.assertTrue(underTest.addMetricData(inMetricData2));
    outData = (List) underTest.collectAllMetrics();
    Assertions.assertEquals(1, outData.size());
    Assertions.assertEquals(inMetricData2.get(0), outData.get(0));
}
Also used : ResourceMetrics(io.opentelemetry.proto.metrics.v1.ResourceMetrics) List(java.util.List) MetricData(io.opentelemetry.sdk.metrics.data.MetricData) Test(org.junit.jupiter.api.Test)

Example 4 with Gauge

use of io.opentelemetry.proto.metrics.v1.Gauge in project hypertrace-ingester by hypertrace.

the class ResourceMetricsUtils method prepareMetric.

public static ResourceMetrics prepareMetric(String metricName, String metricDesc, Number value, String type) {
    ResourceMetrics.Builder resourceMetricsBuilder = ResourceMetrics.newBuilder();
    resourceMetricsBuilder.setResource(prepareResource());
    Metric metric;
    if (type.equals("Gauge")) {
        metric = prepareGaugeMetric(metricName, metricDesc, value);
    } else {
        metric = prepareSumMetric(metricName, metricDesc, value);
    }
    resourceMetricsBuilder.addInstrumentationLibraryMetrics(InstrumentationLibraryMetrics.newBuilder().addMetrics(metric).setInstrumentationLibrary(InstrumentationLibrary.newBuilder().setName("Generated").build()).build());
    return resourceMetricsBuilder.build();
}
Also used : ResourceMetrics(io.opentelemetry.proto.metrics.v1.ResourceMetrics) Metric(io.opentelemetry.proto.metrics.v1.Metric)

Example 5 with Gauge

use of io.opentelemetry.proto.metrics.v1.Gauge in project data-prepper by opensearch-project.

the class MetricsPluginGaugeTest method test.

@Test
public void test() throws JsonProcessingException {
    NumberDataPoint.Builder p1 = NumberDataPoint.newBuilder().setAsInt(4);
    Gauge gauge = Gauge.newBuilder().addDataPoints(p1).build();
    io.opentelemetry.proto.metrics.v1.Metric.Builder metric = io.opentelemetry.proto.metrics.v1.Metric.newBuilder().setGauge(gauge).setUnit("seconds").setName("name").setDescription("description");
    InstrumentationLibraryMetrics isntLib = InstrumentationLibraryMetrics.newBuilder().addMetrics(metric).build();
    Resource resource = Resource.newBuilder().addAttributes(KeyValue.newBuilder().setKey("service.name").setValue(AnyValue.newBuilder().setStringValue("service").build())).build();
    ResourceMetrics resourceMetrics = ResourceMetrics.newBuilder().addInstrumentationLibraryMetrics(isntLib).setResource(resource).build();
    ExportMetricsServiceRequest exportMetricRequest = ExportMetricsServiceRequest.newBuilder().addResourceMetrics(resourceMetrics).build();
    Record<ExportMetricsServiceRequest> record = new Record<>(exportMetricRequest);
    Collection<Record<? extends Metric>> records = rawProcessor.doExecute(Collections.singletonList(record));
    List<Record<? extends Metric>> list = new ArrayList<>(records);
    Record<? extends Metric> dataPrepperResult = list.get(0);
    ObjectMapper objectMapper = new ObjectMapper();
    Map map = objectMapper.readValue(dataPrepperResult.getData().toJsonString(), Map.class);
    assertSumProcessing(map);
}
Also used : Resource(io.opentelemetry.proto.resource.v1.Resource) ArrayList(java.util.ArrayList) InstrumentationLibraryMetrics(io.opentelemetry.proto.metrics.v1.InstrumentationLibraryMetrics) Gauge(io.opentelemetry.proto.metrics.v1.Gauge) ResourceMetrics(io.opentelemetry.proto.metrics.v1.ResourceMetrics) NumberDataPoint(io.opentelemetry.proto.metrics.v1.NumberDataPoint) Metric(com.amazon.dataprepper.model.metric.Metric) Record(com.amazon.dataprepper.model.record.Record) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ExportMetricsServiceRequest(io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceRequest) Test(org.junit.Test)

Aggregations

ResourceMetrics (io.opentelemetry.proto.metrics.v1.ResourceMetrics)7 Test (org.junit.jupiter.api.Test)5 Gauge (io.opentelemetry.proto.metrics.v1.Gauge)4 ExportMetricsServiceRequest (io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceRequest)2 Metric (io.opentelemetry.proto.metrics.v1.Metric)2 Resource (io.opentelemetry.proto.resource.v1.Resource)2 MetricData (io.opentelemetry.sdk.metrics.data.MetricData)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Map (java.util.Map)2 Properties (java.util.Properties)2 StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)2 TestOutputTopic (org.apache.kafka.streams.TestOutputTopic)2 TopologyTestDriver (org.apache.kafka.streams.TopologyTestDriver)2 SetEnvironmentVariable (org.junitpioneer.jupiter.SetEnvironmentVariable)2 Metric (com.amazon.dataprepper.model.metric.Metric)1 Record (com.amazon.dataprepper.model.record.Record)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1