Search in sources :

Example 16 with LabelValue

use of io.opencensus.metrics.LabelValue in project instrumentation-java by census-instrumentation.

the class DoubleGaugeImplTest method withConstantLabels.

@Test
public void withConstantLabels() {
    List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("key1", "desc"), LabelKey.create("key2", "desc"));
    List<LabelValue> labelValues = Arrays.asList(LabelValue.create("value1"), LabelValue.create("value2"));
    LabelKey constantKey = LabelKey.create("constant_key", "desc");
    LabelValue constantValue = LabelValue.create("constant_value");
    Map<LabelKey, LabelValue> constantLabels = Collections.<LabelKey, LabelValue>singletonMap(constantKey, constantValue);
    DoubleGaugeImpl doubleGauge = new DoubleGaugeImpl(METRIC_NAME, METRIC_DESCRIPTION, METRIC_UNIT, labelKeys, constantLabels);
    DoublePoint doublePoint = doubleGauge.getOrCreateTimeSeries(labelValues);
    doublePoint.add(1);
    doublePoint.add(2);
    DoublePoint defaultPoint = doubleGauge.getDefaultTimeSeries();
    defaultPoint.set(100);
    List<LabelKey> allKeys = new ArrayList<>(labelKeys);
    allKeys.add(constantKey);
    MetricDescriptor expectedDescriptor = MetricDescriptor.create(METRIC_NAME, METRIC_DESCRIPTION, METRIC_UNIT, Type.GAUGE_DOUBLE, allKeys);
    List<LabelValue> allValues = new ArrayList<>(labelValues);
    allValues.add(constantValue);
    List<TimeSeries> expectedTimeSeriesList = new ArrayList<TimeSeries>();
    TimeSeries defaultTimeSeries = TimeSeries.createWithOnePoint(Arrays.asList(UNSET_VALUE, UNSET_VALUE, constantValue), Point.create(Value.doubleValue(100), TEST_TIME), null);
    expectedTimeSeriesList.add(TimeSeries.createWithOnePoint(allValues, Point.create(Value.doubleValue(3), TEST_TIME), null));
    expectedTimeSeriesList.add(defaultTimeSeries);
    Metric metric = doubleGauge.getMetric(testClock);
    assertThat(metric).isNotNull();
    assertThat(metric.getMetricDescriptor()).isEqualTo(expectedDescriptor);
    assertThat(metric.getTimeSeriesList().size()).isEqualTo(2);
    assertThat(metric.getTimeSeriesList()).containsExactlyElementsIn(expectedTimeSeriesList);
    doubleGauge.removeTimeSeries(labelValues);
    Metric metric2 = doubleGauge.getMetric(testClock);
    assertThat(metric2).isNotNull();
    assertThat(metric2.getTimeSeriesList()).containsExactly(defaultTimeSeries);
}
Also used : MetricDescriptor(io.opencensus.metrics.export.MetricDescriptor) TimeSeries(io.opencensus.metrics.export.TimeSeries) LabelValue(io.opencensus.metrics.LabelValue) DoublePoint(io.opencensus.metrics.DoubleGauge.DoublePoint) ArrayList(java.util.ArrayList) LabelKey(io.opencensus.metrics.LabelKey) Metric(io.opencensus.metrics.export.Metric) Test(org.junit.Test)

Example 17 with LabelValue

use of io.opencensus.metrics.LabelValue in project instrumentation-java by census-instrumentation.

the class DoubleGaugeImplTest method testEquals.

@Test
public void testEquals() {
    List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("key1", "desc"), LabelKey.create("key2", "desc"));
    List<LabelValue> labelValues = Arrays.asList(LabelValue.create("value1"), LabelValue.create("value2"));
    DoubleGaugeImpl doubleGauge = new DoubleGaugeImpl(METRIC_NAME, METRIC_DESCRIPTION, METRIC_UNIT, labelKeys, EMPTY_CONSTANT_LABELS);
    DoublePoint defaultPoint1 = doubleGauge.getDefaultTimeSeries();
    DoublePoint defaultPoint2 = doubleGauge.getDefaultTimeSeries();
    DoublePoint doublePoint1 = doubleGauge.getOrCreateTimeSeries(labelValues);
    DoublePoint doublePoint2 = doubleGauge.getOrCreateTimeSeries(labelValues);
    new EqualsTester().addEqualityGroup(defaultPoint1, defaultPoint2).addEqualityGroup(doublePoint1, doublePoint2).testEquals();
    doubleGauge.clear();
    DoublePoint newDefaultPointAfterClear = doubleGauge.getDefaultTimeSeries();
    DoublePoint newDoublePointAfterClear = doubleGauge.getOrCreateTimeSeries(labelValues);
    doubleGauge.removeTimeSeries(labelValues);
    DoublePoint newDoublePointAfterRemove = doubleGauge.getOrCreateTimeSeries(labelValues);
    new EqualsTester().addEqualityGroup(defaultPoint1, defaultPoint2).addEqualityGroup(doublePoint1, doublePoint2).addEqualityGroup(newDefaultPointAfterClear).addEqualityGroup(newDoublePointAfterClear).addEqualityGroup(newDoublePointAfterRemove).testEquals();
}
Also used : LabelValue(io.opencensus.metrics.LabelValue) EqualsTester(com.google.common.testing.EqualsTester) DoublePoint(io.opencensus.metrics.DoubleGauge.DoublePoint) LabelKey(io.opencensus.metrics.LabelKey) Test(org.junit.Test)

Example 18 with LabelValue

use of io.opencensus.metrics.LabelValue in project instrumentation-java by census-instrumentation.

the class LongCumulativeImplTest method withConstantLabels.

@Test
public void withConstantLabels() {
    List<LabelKey> labelKeys = Arrays.asList(LabelKey.create("key1", "desc"), LabelKey.create("key2", "desc"));
    List<LabelValue> labelValues = Arrays.asList(LabelValue.create("value1"), LabelValue.create("value2"));
    LabelKey constantKey = LabelKey.create("constant_key", "desc");
    LabelValue constantValue = LabelValue.create("constant_value");
    Map<LabelKey, LabelValue> constantLabels = Collections.<LabelKey, LabelValue>singletonMap(constantKey, constantValue);
    LongCumulativeImpl longCumulative = new LongCumulativeImpl(METRIC_NAME, METRIC_DESCRIPTION, METRIC_UNIT, labelKeys, constantLabels, START_TIME);
    LongPoint longPoint = longCumulative.getOrCreateTimeSeries(labelValues);
    longPoint.add(1);
    longPoint.add(2);
    LongPoint defaultPoint = longCumulative.getDefaultTimeSeries();
    defaultPoint.add(100);
    List<LabelKey> allKeys = new ArrayList<>(labelKeys);
    allKeys.add(constantKey);
    MetricDescriptor expectedDescriptor = MetricDescriptor.create(METRIC_NAME, METRIC_DESCRIPTION, METRIC_UNIT, Type.CUMULATIVE_INT64, allKeys);
    testClock.advanceTime(ONE_MINUTE);
    Timestamp endTime = testClock.now();
    List<LabelValue> allValues = new ArrayList<>(labelValues);
    allValues.add(constantValue);
    List<TimeSeries> expectedTimeSeriesList = new ArrayList<TimeSeries>();
    TimeSeries defaultTimeSeries = TimeSeries.createWithOnePoint(Arrays.asList(UNSET_VALUE, UNSET_VALUE, constantValue), Point.create(Value.longValue(100), endTime), START_TIME);
    expectedTimeSeriesList.add(TimeSeries.createWithOnePoint(allValues, Point.create(Value.longValue(3), endTime), START_TIME));
    expectedTimeSeriesList.add(defaultTimeSeries);
    Metric metric = longCumulative.getMetric(testClock);
    assertThat(metric).isNotNull();
    assertThat(metric.getMetricDescriptor()).isEqualTo(expectedDescriptor);
    assertThat(metric.getTimeSeriesList().size()).isEqualTo(2);
    assertThat(metric.getTimeSeriesList()).containsExactlyElementsIn(expectedTimeSeriesList);
    longCumulative.removeTimeSeries(labelValues);
    Metric metric2 = longCumulative.getMetric(testClock);
    assertThat(metric2).isNotNull();
    assertThat(metric2.getTimeSeriesList()).containsExactly(defaultTimeSeries);
}
Also used : TimeSeries(io.opencensus.metrics.export.TimeSeries) LabelValue(io.opencensus.metrics.LabelValue) ArrayList(java.util.ArrayList) LongPoint(io.opencensus.metrics.LongCumulative.LongPoint) Timestamp(io.opencensus.common.Timestamp) MetricDescriptor(io.opencensus.metrics.export.MetricDescriptor) LabelKey(io.opencensus.metrics.LabelKey) Metric(io.opencensus.metrics.export.Metric) Test(org.junit.Test)

Example 19 with LabelValue

use of io.opencensus.metrics.LabelValue 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)

Example 20 with LabelValue

use of io.opencensus.metrics.LabelValue in project instrumentation-java by census-instrumentation.

the class StackdriverExportUtilsTest method convertSummaryMetricWithNullSum.

@Test
public void convertSummaryMetricWithNullSum() {
    io.opencensus.metrics.export.MetricDescriptor expectedMetricDescriptor1 = io.opencensus.metrics.export.MetricDescriptor.create(METRIC_NAME + SUMMARY_SUFFIX_COUNT, METRIC_DESCRIPTION, METRIC_UNIT_2, Type.CUMULATIVE_INT64, LABEL_KEY);
    List<LabelKey> labelKeys = new ArrayList<>(LABEL_KEY);
    labelKeys.add(PERCENTILE_LABEL_KEY);
    io.opencensus.metrics.export.MetricDescriptor expectedMetricDescriptor2 = io.opencensus.metrics.export.MetricDescriptor.create(METRIC_NAME + SNAPSHOT_SUFFIX_PERCENTILE, METRIC_DESCRIPTION, METRIC_UNIT, Type.GAUGE_DOUBLE, labelKeys);
    List<io.opencensus.metrics.export.TimeSeries> expectedTimeSeries1 = Collections.singletonList(io.opencensus.metrics.export.TimeSeries.createWithOnePoint(LABEL_VALUE, Point.create(Value.longValue(22), TIMESTAMP), null));
    LabelValue existingLabelValues = LABEL_VALUE.get(0);
    List<io.opencensus.metrics.export.TimeSeries> expectedTimeSeries2 = Collections.singletonList(io.opencensus.metrics.export.TimeSeries.createWithOnePoint(Arrays.asList(existingLabelValues, LabelValue.create("50.0")), Point.create(Value.doubleValue(6), TIMESTAMP), null));
    List<io.opencensus.metrics.export.Metric> metrics = StackdriverExportUtils.convertSummaryMetric(SUMMARY_METRIC_NULL_SUM);
    assertThat(metrics).isNotEmpty();
    assertThat(metrics.size()).isEqualTo(2);
    assertThat(metrics.get(0).getMetricDescriptor()).isEqualTo(expectedMetricDescriptor1);
    assertThat(metrics.get(0).getTimeSeriesList()).isNotEmpty();
    assertThat(metrics.get(0).getTimeSeriesList().size()).isEqualTo(1);
    assertThat(metrics.get(0).getTimeSeriesList()).containsExactlyElementsIn(expectedTimeSeries1);
    assertThat(metrics.get(1).getTimeSeriesList()).isNotEmpty();
    assertThat(metrics.get(1).getMetricDescriptor()).isEqualTo(expectedMetricDescriptor2);
    assertThat(metrics.get(1).getTimeSeriesList().size()).isEqualTo(1);
    assertThat(metrics.get(1).getTimeSeriesList()).containsExactlyElementsIn(expectedTimeSeries2);
}
Also used : TimeSeries(com.google.monitoring.v3.TimeSeries) LabelValue(io.opencensus.metrics.LabelValue) ArrayList(java.util.ArrayList) LabelKey(io.opencensus.metrics.LabelKey) Metric(com.google.api.Metric) Test(org.junit.Test)

Aggregations

LabelValue (io.opencensus.metrics.LabelValue)22 LabelKey (io.opencensus.metrics.LabelKey)17 Test (org.junit.Test)16 ArrayList (java.util.ArrayList)14 MetricDescriptor (io.opencensus.metrics.export.MetricDescriptor)10 TimeSeries (io.opencensus.metrics.export.TimeSeries)10 Metric (io.opencensus.metrics.export.Metric)9 EqualsTester (com.google.common.testing.EqualsTester)4 Timestamp (io.opencensus.common.Timestamp)4 Metric (com.google.api.Metric)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 LongPoint (io.opencensus.metrics.LongGauge.LongPoint)3 TimeSeries (com.google.monitoring.v3.TimeSeries)2 DoublePoint (io.opencensus.metrics.DoubleCumulative.DoublePoint)2 DoublePoint (io.opencensus.metrics.DoubleGauge.DoublePoint)2 LongPoint (io.opencensus.metrics.LongCumulative.LongPoint)2 Type (io.opencensus.metrics.export.MetricDescriptor.Type)2 Value (io.opencensus.metrics.export.Value)2 List (java.util.List)2 Map (java.util.Map)2