Search in sources :

Example 1 with HistogramPointData

use of io.opentelemetry.sdk.metrics.data.HistogramPointData 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 2 with HistogramPointData

use of io.opentelemetry.sdk.metrics.data.HistogramPointData 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 3 with HistogramPointData

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

the class MetricAdapter method convertHistogramPoints.

static Collection<HistogramPointData> convertHistogramPoints(Metric censusMetric) {
    boolean isGauge = censusMetric.getMetricDescriptor().getType() == MetricDescriptor.Type.GAUGE_DISTRIBUTION;
    // TODO - preallocate array to correct size.
    List<HistogramPointData> result = new ArrayList<>();
    for (TimeSeries ts : censusMetric.getTimeSeriesList()) {
        long startTimestamp = mapTimestamp(ts.getStartTimestamp());
        Attributes attributes = mapAttributes(censusMetric.getMetricDescriptor().getLabelKeys(), ts.getLabelValues());
        for (Point point : ts.getPoints()) {
            long endTimestamp = mapTimestamp(point.getTimestamp());
            HistogramPointData otelPoint = point.getValue().match(doubleValue -> null, longValue -> null, distribution -> ImmutableHistogramPointData.create(// Report Gauge histograms as DELTA with "instantaneous" time window.
            isGauge ? endTimestamp : startTimestamp, endTimestamp, attributes, distribution.getSum(), mapBoundaries(distribution.getBucketOptions()), mapCounts(distribution.getBuckets()), mapExemplars(distribution.getBuckets())), summary -> null, defaultValue -> null);
            if (otelPoint != null) {
                result.add(otelPoint);
            }
        }
    }
    return result;
}
Also used : TimeSeries(io.opencensus.metrics.export.TimeSeries) ArrayList(java.util.ArrayList) Attributes(io.opentelemetry.api.common.Attributes) HistogramPointData(io.opentelemetry.sdk.metrics.data.HistogramPointData) ImmutableHistogramPointData(io.opentelemetry.sdk.metrics.internal.data.ImmutableHistogramPointData) Point(io.opencensus.metrics.export.Point)

Example 4 with HistogramPointData

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

the class HistogramDataPointMarshaler method createRepeated.

static HistogramDataPointMarshaler[] createRepeated(Collection<HistogramPointData> points) {
    HistogramDataPointMarshaler[] marshalers = new HistogramDataPointMarshaler[points.size()];
    int index = 0;
    for (HistogramPointData point : points) {
        marshalers[index++] = HistogramDataPointMarshaler.create(point);
    }
    return marshalers;
}
Also used : HistogramPointData(io.opentelemetry.sdk.metrics.data.HistogramPointData) HistogramDataPoint(io.opentelemetry.proto.metrics.v1.internal.HistogramDataPoint)

Aggregations

HistogramPointData (io.opentelemetry.sdk.metrics.data.HistogramPointData)4 Attributes (io.opentelemetry.api.common.Attributes)2 DoublePointData (io.opentelemetry.sdk.metrics.data.DoublePointData)2 LongPointData (io.opentelemetry.sdk.metrics.data.LongPointData)2 PointData (io.opentelemetry.sdk.metrics.data.PointData)2 SummaryPointData (io.opentelemetry.sdk.metrics.data.SummaryPointData)2 ArrayList (java.util.ArrayList)2 Point (io.opencensus.metrics.export.Point)1 TimeSeries (io.opencensus.metrics.export.TimeSeries)1 HistogramDataPoint (io.opentelemetry.proto.metrics.v1.internal.HistogramDataPoint)1 ImmutableHistogramPointData (io.opentelemetry.sdk.metrics.internal.data.ImmutableHistogramPointData)1 Sample (io.prometheus.client.Collector.MetricFamilySamples.Sample)1 Collector.doubleToGoString (io.prometheus.client.Collector.doubleToGoString)1