Search in sources :

Example 1 with ValueAtPercentile

use of io.opencensus.metrics.export.Summary.Snapshot.ValueAtPercentile in project instrumentation-java by census-instrumentation.

the class DropWizardMetrics method collectSnapshotAndCount.

/**
 * Returns a {@code Metric} collected from {@link Snapshot}.
 *
 * @param metricName the metric name.
 * @param metricDescription the metric description.
 * @param labelKeys metric label keys
 * @param labelValues metric label values
 * @param codahaleSnapshot the snapshot object to collect
 * @param count the value or count
 * @return a {@code Metric}.
 */
private Metric collectSnapshotAndCount(String metricName, String metricDescription, List<LabelKey> labelKeys, List<LabelValue> labelValues, String unit, io.dropwizard.metrics5.Snapshot codahaleSnapshot, long count) {
    List<ValueAtPercentile> valueAtPercentiles = Arrays.asList(ValueAtPercentile.create(50.0, codahaleSnapshot.getMedian()), ValueAtPercentile.create(75.0, codahaleSnapshot.get75thPercentile()), ValueAtPercentile.create(98.0, codahaleSnapshot.get98thPercentile()), ValueAtPercentile.create(99.0, codahaleSnapshot.get99thPercentile()), ValueAtPercentile.create(99.9, codahaleSnapshot.get999thPercentile()));
    Snapshot snapshot = Snapshot.create((long) codahaleSnapshot.size(), 0.0, valueAtPercentiles);
    Point point = Point.create(Value.summaryValue(Summary.create(count, 0.0, snapshot)), clock.now());
    // TODO(mayurkale): OPTIMIZATION: Cache the MetricDescriptor objects.
    MetricDescriptor metricDescriptor = MetricDescriptor.create(metricName, metricDescription, unit, Type.SUMMARY, labelKeys);
    TimeSeries timeSeries = TimeSeries.createWithOnePoint(labelValues, point, cumulativeStartTimestamp);
    return Metric.createWithOneTimeSeries(metricDescriptor, timeSeries);
}
Also used : ValueAtPercentile(io.opencensus.metrics.export.Summary.Snapshot.ValueAtPercentile) Snapshot(io.opencensus.metrics.export.Summary.Snapshot) MetricDescriptor(io.opencensus.metrics.export.MetricDescriptor) TimeSeries(io.opencensus.metrics.export.TimeSeries) Point(io.opencensus.metrics.export.Point)

Example 2 with ValueAtPercentile

use of io.opencensus.metrics.export.Summary.Snapshot.ValueAtPercentile in project instrumentation-java by census-instrumentation.

the class DropWizardMetrics method collectSnapshotAndCount.

/**
 * Returns a {@code Metric} collected from {@link Snapshot}.
 *
 * @param metricName the metric name.
 * @param metricDescription the metric description.
 * @param unit the metric descriptor unit.
 * @param codahaleSnapshot the snapshot object to collect
 * @param count the value or count
 * @return a {@code Metric}.
 */
private Metric collectSnapshotAndCount(String metricName, String metricDescription, String unit, com.codahale.metrics.Snapshot codahaleSnapshot, long count) {
    List<ValueAtPercentile> valueAtPercentiles = Arrays.asList(ValueAtPercentile.create(50.0, codahaleSnapshot.getMedian()), ValueAtPercentile.create(75.0, codahaleSnapshot.get75thPercentile()), ValueAtPercentile.create(98.0, codahaleSnapshot.get98thPercentile()), ValueAtPercentile.create(99.0, codahaleSnapshot.get99thPercentile()), ValueAtPercentile.create(99.9, codahaleSnapshot.get999thPercentile()));
    Snapshot snapshot = Snapshot.create((long) codahaleSnapshot.size(), 0.0, valueAtPercentiles);
    Point point = Point.create(Value.summaryValue(Summary.create(count, 0.0, snapshot)), clock.now());
    // TODO(mayurkale): OPTIMIZATION: Cache the MetricDescriptor objects.
    MetricDescriptor metricDescriptor = MetricDescriptor.create(metricName, metricDescription, unit, Type.SUMMARY, Collections.<LabelKey>emptyList());
    TimeSeries timeSeries = TimeSeries.createWithOnePoint(Collections.<LabelValue>emptyList(), point, cumulativeStartTimestamp);
    return Metric.createWithOneTimeSeries(metricDescriptor, timeSeries);
}
Also used : ValueAtPercentile(io.opencensus.metrics.export.Summary.Snapshot.ValueAtPercentile) Snapshot(io.opencensus.metrics.export.Summary.Snapshot) MetricDescriptor(io.opencensus.metrics.export.MetricDescriptor) TimeSeries(io.opencensus.metrics.export.TimeSeries) Point(io.opencensus.metrics.export.Point)

Example 3 with ValueAtPercentile

use of io.opencensus.metrics.export.Summary.Snapshot.ValueAtPercentile in project instrumentation-java by census-instrumentation.

the class SummaryTest method createAndGet_ValueAtPercentile.

@Test
public void createAndGet_ValueAtPercentile() {
    ValueAtPercentile valueAtPercentile = ValueAtPercentile.create(99.5, 10.2);
    assertThat(valueAtPercentile.getPercentile()).isWithin(TOLERANCE).of(99.5);
    assertThat(valueAtPercentile.getValue()).isWithin(TOLERANCE).of(10.2);
}
Also used : ValueAtPercentile(io.opencensus.metrics.export.Summary.Snapshot.ValueAtPercentile) Test(org.junit.Test)

Example 4 with ValueAtPercentile

use of io.opencensus.metrics.export.Summary.Snapshot.ValueAtPercentile in project instrumentation-java by census-instrumentation.

the class PrometheusExportUtils method getSamples.

// Converts a point value in Metric to a list of Prometheus Samples.
@VisibleForTesting
static List<Sample> getSamples(final String name, final List<String> labelNames, List<LabelValue> labelValuesList, Value value) {
    Preconditions.checkArgument(labelNames.size() == labelValuesList.size(), "Keys and Values don't have same size.");
    final List<Sample> samples = Lists.newArrayList();
    final List<String> labelValues = new ArrayList<String>(labelValuesList.size());
    for (LabelValue labelValue : labelValuesList) {
        String val = labelValue == null ? "" : labelValue.getValue();
        labelValues.add(val == null ? "" : val);
    }
    return value.match(new Function<Double, List<Sample>>() {

        @Override
        public List<Sample> apply(Double arg) {
            samples.add(new Sample(name, labelNames, labelValues, arg));
            return samples;
        }
    }, new Function<Long, List<Sample>>() {

        @Override
        public List<Sample> apply(Long arg) {
            samples.add(new Sample(name, labelNames, labelValues, arg));
            return samples;
        }
    }, new Function<Distribution, List<Sample>>() {

        @Override
        public List<Sample> apply(final Distribution arg) {
            BucketOptions bucketOptions = arg.getBucketOptions();
            List<Double> boundaries = new ArrayList<>();
            if (bucketOptions != null) {
                boundaries = bucketOptions.match(new Function<ExplicitOptions, List<Double>>() {

                    @Override
                    public List<Double> apply(ExplicitOptions arg) {
                        return arg.getBucketBoundaries();
                    }
                }, Functions.<List<Double>>throwIllegalArgumentException());
            }
            List<String> labelNamesWithLe = new ArrayList<String>(labelNames);
            labelNamesWithLe.add(LABEL_NAME_BUCKET_BOUND);
            long cumulativeCount = 0;
            for (int i = 0; i < arg.getBuckets().size(); i++) {
                List<String> labelValuesWithLe = new ArrayList<String>(labelValues);
                // The label value of "le" is the upper inclusive bound.
                // For the last bucket, it should be "+Inf".
                String bucketBoundary = doubleToGoString(i < boundaries.size() ? boundaries.get(i) : Double.POSITIVE_INFINITY);
                labelValuesWithLe.add(bucketBoundary);
                cumulativeCount += arg.getBuckets().get(i).getCount();
                samples.add(new MetricFamilySamples.Sample(name + SAMPLE_SUFFIX_BUCKET, labelNamesWithLe, labelValuesWithLe, cumulativeCount));
            }
            samples.add(new MetricFamilySamples.Sample(name + SAMPLE_SUFFIX_COUNT, labelNames, labelValues, arg.getCount()));
            samples.add(new MetricFamilySamples.Sample(name + SAMPLE_SUFFIX_SUM, labelNames, labelValues, arg.getSum()));
            return samples;
        }
    }, new Function<Summary, List<Sample>>() {

        @Override
        public List<Sample> apply(Summary arg) {
            Long count = arg.getCount();
            if (count != null) {
                samples.add(new MetricFamilySamples.Sample(name + SAMPLE_SUFFIX_COUNT, labelNames, labelValues, count));
            }
            Double sum = arg.getSum();
            if (sum != null) {
                samples.add(new MetricFamilySamples.Sample(name + SAMPLE_SUFFIX_SUM, labelNames, labelValues, sum));
            }
            List<ValueAtPercentile> valueAtPercentiles = arg.getSnapshot().getValueAtPercentiles();
            List<String> labelNamesWithQuantile = new ArrayList<String>(labelNames);
            labelNamesWithQuantile.add(LABEL_NAME_QUANTILE);
            for (ValueAtPercentile valueAtPercentile : valueAtPercentiles) {
                List<String> labelValuesWithQuantile = new ArrayList<String>(labelValues);
                labelValuesWithQuantile.add(doubleToGoString(valueAtPercentile.getPercentile() / 100));
                samples.add(new MetricFamilySamples.Sample(name, labelNamesWithQuantile, labelValuesWithQuantile, valueAtPercentile.getValue()));
            }
            return samples;
        }
    }, Functions.<List<Sample>>throwIllegalArgumentException());
}
Also used : ExplicitOptions(io.opencensus.metrics.export.Distribution.BucketOptions.ExplicitOptions) LabelValue(io.opencensus.metrics.LabelValue) Sample(io.prometheus.client.Collector.MetricFamilySamples.Sample) ArrayList(java.util.ArrayList) Collector.doubleToGoString(io.prometheus.client.Collector.doubleToGoString) ValueAtPercentile(io.opencensus.metrics.export.Summary.Snapshot.ValueAtPercentile) BucketOptions(io.opencensus.metrics.export.Distribution.BucketOptions) Distribution(io.opencensus.metrics.export.Distribution) Summary(io.opencensus.metrics.export.Summary) ArrayList(java.util.ArrayList) List(java.util.List) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

ValueAtPercentile (io.opencensus.metrics.export.Summary.Snapshot.ValueAtPercentile)4 MetricDescriptor (io.opencensus.metrics.export.MetricDescriptor)2 Point (io.opencensus.metrics.export.Point)2 Snapshot (io.opencensus.metrics.export.Summary.Snapshot)2 TimeSeries (io.opencensus.metrics.export.TimeSeries)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 LabelValue (io.opencensus.metrics.LabelValue)1 Distribution (io.opencensus.metrics.export.Distribution)1 BucketOptions (io.opencensus.metrics.export.Distribution.BucketOptions)1 ExplicitOptions (io.opencensus.metrics.export.Distribution.BucketOptions.ExplicitOptions)1 Summary (io.opencensus.metrics.export.Summary)1 Sample (io.prometheus.client.Collector.MetricFamilySamples.Sample)1 Collector.doubleToGoString (io.prometheus.client.Collector.doubleToGoString)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Test (org.junit.Test)1