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);
}
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();
}
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);
}
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());
}
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);
}
Aggregations