Search in sources :

Example 6 with PointData

use of io.opentelemetry.sdk.metrics.data.PointData in project opentelemetry-java by open-telemetry.

the class MetricAdapter method toSamples.

// Converts a list of points from MetricData to a list of Prometheus Samples.
static List<Sample> toSamples(String name, MetricDataType type, Collection<? extends PointData> points) {
    List<Sample> samples = new ArrayList<>(estimateNumSamples(points.size(), type));
    for (PointData pointData : points) {
        Attributes attributes = pointData.getAttributes();
        List<String> labelNames = new ArrayList<>(attributes.size());
        List<String> labelValues = new ArrayList<>(attributes.size());
        attributes.forEach((key, value) -> {
            String sanitizedLabelName = sanitizer.apply(key.getKey());
            labelNames.add(sanitizedLabelName);
            // TODO: We want to create an error-log if there is overlap in toString of attribute
            // values for the same key name.
            labelValues.add(value == null ? "" : value.toString());
        });
        switch(type) {
            case DOUBLE_SUM:
            case DOUBLE_GAUGE:
                DoublePointData doublePoint = (DoublePointData) pointData;
                samples.add(createSample(name, labelNames, labelValues, doublePoint.getValue(), // Prometheus doesn't support exemplars on SUM/GAUGE
                null, doublePoint.getEpochNanos()));
                break;
            case LONG_SUM:
            case LONG_GAUGE:
                LongPointData longPoint = (LongPointData) pointData;
                samples.add(createSample(name, labelNames, labelValues, longPoint.getValue(), // Prometheus doesn't support exemplars on SUM/GAUGE
                null, longPoint.getEpochNanos()));
                break;
            case SUMMARY:
                addSummarySamples((SummaryPointData) pointData, name, labelNames, labelValues, samples);
                break;
            case HISTOGRAM:
                addHistogramSamples((HistogramPointData) pointData, name, labelNames, labelValues, samples);
                break;
            case EXPONENTIAL_HISTOGRAM:
                // todo
                break;
        }
    }
    return samples;
}
Also used : LongPointData(io.opentelemetry.sdk.metrics.data.LongPointData) DoublePointData(io.opentelemetry.sdk.metrics.data.DoublePointData) HistogramPointData(io.opentelemetry.sdk.metrics.data.HistogramPointData) SummaryPointData(io.opentelemetry.sdk.metrics.data.SummaryPointData) PointData(io.opentelemetry.sdk.metrics.data.PointData) Sample(io.prometheus.client.Collector.MetricFamilySamples.Sample) LongPointData(io.opentelemetry.sdk.metrics.data.LongPointData) ArrayList(java.util.ArrayList) Attributes(io.opentelemetry.api.common.Attributes) Collector.doubleToGoString(io.prometheus.client.Collector.doubleToGoString) DoublePointData(io.opentelemetry.sdk.metrics.data.DoublePointData)

Example 7 with PointData

use of io.opentelemetry.sdk.metrics.data.PointData in project opentelemetry-java by open-telemetry.

the class Serializer method write.

private void write(MetricData metric, Writer writer) throws IOException {
    // Not supported in specification yet.
    if (metric.getType() == MetricDataType.EXPONENTIAL_HISTOGRAM) {
        return;
    }
    PrometheusType type = PrometheusType.forMetric(metric);
    String name = NameSanitizer.INSTANCE.apply(metric.getName());
    String headerName = headerName(name, type);
    if (type == PrometheusType.COUNTER) {
        name = name + "_total";
    }
    if (!metricNameFilter.test(name)) {
        return;
    }
    writer.write("# TYPE ");
    writer.write(headerName);
    writer.write(' ');
    writer.write(type.getTypeString());
    writer.write('\n');
    writer.write("# HELP ");
    writer.write(headerName);
    writer.write(' ');
    writeHelp(writer, metric.getDescription());
    writer.write('\n');
    for (PointData point : MetricAdapter.getPoints(metric)) {
        switch(metric.getType()) {
            case DOUBLE_SUM:
            case DOUBLE_GAUGE:
                writePoint(writer, name, ((DoublePointData) point).getValue(), point.getAttributes(), point.getEpochNanos());
                break;
            case LONG_SUM:
            case LONG_GAUGE:
                writePoint(writer, name, ((LongPointData) point).getValue(), point.getAttributes(), point.getEpochNanos());
                break;
            case HISTOGRAM:
                writeHistogram(writer, name, (HistogramPointData) point);
                break;
            case SUMMARY:
                writeSummary(writer, name, (SummaryPointData) point);
                break;
            case EXPONENTIAL_HISTOGRAM:
                throw new IllegalArgumentException("Can't happen");
        }
    }
}
Also used : LongPointData(io.opentelemetry.sdk.metrics.data.LongPointData) DoublePointData(io.opentelemetry.sdk.metrics.data.DoublePointData) HistogramPointData(io.opentelemetry.sdk.metrics.data.HistogramPointData) SummaryPointData(io.opentelemetry.sdk.metrics.data.SummaryPointData) PointData(io.opentelemetry.sdk.metrics.data.PointData)

Example 8 with PointData

use of io.opentelemetry.sdk.metrics.data.PointData in project opentelemetry-java by open-telemetry.

the class SdkLongUpDownCounterTest method stressTest_WithDifferentLabelSet.

@Test
void stressTest_WithDifferentLabelSet() {
    String[] keys = { "Key_1", "Key_2", "Key_3", "Key_4" };
    String[] values = { "Value_1", "Value_2", "Value_3", "Value_4" };
    LongUpDownCounter longUpDownCounter = sdkMeter.upDownCounterBuilder("testUpDownCounter").build();
    StressTestRunner.Builder stressTestBuilder = StressTestRunner.builder().setInstrument((SdkLongUpDownCounter) longUpDownCounter).setCollectionIntervalMs(100);
    for (int i = 0; i < 4; i++) {
        stressTestBuilder.addOperation(StressTestRunner.Operation.create(1_000, 2, new OperationUpdaterDirectCall(longUpDownCounter, keys[i], values[i])));
        stressTestBuilder.addOperation(StressTestRunner.Operation.create(1_000, 2, new OperationUpdaterWithBinding(((SdkLongUpDownCounter) longUpDownCounter).bind(Attributes.builder().put(keys[i], values[i]).build()))));
    }
    stressTestBuilder.build().run();
    assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasName("testUpDownCounter").hasLongSum().isCumulative().isNotMonotonic().points().allSatisfy(point -> assertThat(point).hasStartEpochNanos(testClock.now()).hasEpochNanos(testClock.now()).hasValue(20_000)).extracting(PointData::getAttributes).containsExactlyInAnyOrder(Attributes.of(stringKey(keys[0]), values[0]), Attributes.of(stringKey(keys[1]), values[1]), Attributes.of(stringKey(keys[2]), values[2]), Attributes.of(stringKey(keys[3]), values[3])));
}
Also used : Resource(io.opentelemetry.sdk.resources.Resource) Attributes(io.opentelemetry.api.common.Attributes) InstrumentationLibraryInfo(io.opentelemetry.sdk.common.InstrumentationLibraryInfo) OperationUpdater(io.opentelemetry.sdk.metrics.StressTestRunner.OperationUpdater) Test(org.junit.jupiter.api.Test) BoundLongUpDownCounter(io.opentelemetry.sdk.metrics.internal.instrument.BoundLongUpDownCounter) PointData(io.opentelemetry.sdk.metrics.data.PointData) InMemoryMetricReader(io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) AttributeKey.stringKey(io.opentelemetry.api.common.AttributeKey.stringKey) Duration(java.time.Duration) TestClock(io.opentelemetry.sdk.testing.time.TestClock) MetricAssertions.assertThat(io.opentelemetry.sdk.testing.assertj.MetricAssertions.assertThat) Meter(io.opentelemetry.api.metrics.Meter) LongUpDownCounter(io.opentelemetry.api.metrics.LongUpDownCounter) BoundLongUpDownCounter(io.opentelemetry.sdk.metrics.internal.instrument.BoundLongUpDownCounter) LongUpDownCounter(io.opentelemetry.api.metrics.LongUpDownCounter) PointData(io.opentelemetry.sdk.metrics.data.PointData) Test(org.junit.jupiter.api.Test)

Example 9 with PointData

use of io.opentelemetry.sdk.metrics.data.PointData in project opentelemetry-java by open-telemetry.

the class NumberDataPointMarshaler method createRepeated.

static NumberDataPointMarshaler[] createRepeated(Collection<? extends PointData> points) {
    int numPoints = points.size();
    NumberDataPointMarshaler[] marshalers = new NumberDataPointMarshaler[numPoints];
    int index = 0;
    for (PointData point : points) {
        marshalers[index++] = NumberDataPointMarshaler.create(point);
    }
    return marshalers;
}
Also used : LongPointData(io.opentelemetry.sdk.metrics.data.LongPointData) PointData(io.opentelemetry.sdk.metrics.data.PointData) DoublePointData(io.opentelemetry.sdk.metrics.data.DoublePointData) NumberDataPoint(io.opentelemetry.proto.metrics.v1.internal.NumberDataPoint)

Example 10 with PointData

use of io.opentelemetry.sdk.metrics.data.PointData in project besu by hyperledger.

the class OpenTelemetrySystem method convertToObservations.

private Stream<Observation> convertToObservations(final MetricData metricData) {
    List<Observation> observations = new ArrayList<>();
    MetricCategory category = categoryNameToMetricCategory(metricData.getInstrumentationLibraryInfo().getName());
    Collection<?> points;
    switch(metricData.getType()) {
        case DOUBLE_GAUGE:
            points = metricData.getDoubleGaugeData().getPoints();
            break;
        case DOUBLE_SUM:
            points = metricData.getDoubleSumData().getPoints();
            break;
        case SUMMARY:
            points = metricData.getDoubleSummaryData().getPoints();
            break;
        case LONG_SUM:
            points = metricData.getLongSumData().getPoints();
            break;
        case HISTOGRAM:
            points = metricData.getDoubleHistogramData().getPoints();
            break;
        case LONG_GAUGE:
            points = metricData.getLongGaugeData().getPoints();
            break;
        default:
            throw new UnsupportedOperationException("Unsupported type " + metricData.getType().name());
    }
    for (Object ptObj : points) {
        PointData point = (PointData) ptObj;
        List<String> labels = new ArrayList<>();
        point.getAttributes().forEach((k, v) -> labels.add(v.toString()));
        observations.add(new Observation(category, metricData.getName(), extractValue(metricData.getType(), point), labels));
    }
    return observations.stream();
}
Also used : LongPointData(io.opentelemetry.sdk.metrics.data.LongPointData) DoubleHistogramPointData(io.opentelemetry.sdk.metrics.data.DoubleHistogramPointData) DoublePointData(io.opentelemetry.sdk.metrics.data.DoublePointData) DoubleSummaryPointData(io.opentelemetry.sdk.metrics.data.DoubleSummaryPointData) PointData(io.opentelemetry.sdk.metrics.data.PointData) Observation(org.hyperledger.besu.metrics.Observation) ArrayList(java.util.ArrayList) BesuMetricCategory(org.hyperledger.besu.metrics.BesuMetricCategory) StandardMetricCategory(org.hyperledger.besu.metrics.StandardMetricCategory) MetricCategory(org.hyperledger.besu.plugin.services.metrics.MetricCategory)

Aggregations

PointData (io.opentelemetry.sdk.metrics.data.PointData)10 Attributes (io.opentelemetry.api.common.Attributes)7 AttributeKey.stringKey (io.opentelemetry.api.common.AttributeKey.stringKey)6 Meter (io.opentelemetry.api.metrics.Meter)6 InstrumentationLibraryInfo (io.opentelemetry.sdk.common.InstrumentationLibraryInfo)6 OperationUpdater (io.opentelemetry.sdk.metrics.StressTestRunner.OperationUpdater)6 Resource (io.opentelemetry.sdk.resources.Resource)6 MetricAssertions.assertThat (io.opentelemetry.sdk.testing.assertj.MetricAssertions.assertThat)6 InMemoryMetricReader (io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader)6 TestClock (io.opentelemetry.sdk.testing.time.TestClock)6 Duration (java.time.Duration)6 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)6 Test (org.junit.jupiter.api.Test)6 LogCapturer (io.github.netmikey.logunit.api.LogCapturer)4 DoublePointData (io.opentelemetry.sdk.metrics.data.DoublePointData)4 LongPointData (io.opentelemetry.sdk.metrics.data.LongPointData)4 RegisterExtension (org.junit.jupiter.api.extension.RegisterExtension)4 SuppressLogger (io.opentelemetry.internal.testing.slf4j.SuppressLogger)3 HistogramPointData (io.opentelemetry.sdk.metrics.data.HistogramPointData)2 SummaryPointData (io.opentelemetry.sdk.metrics.data.SummaryPointData)2