use of io.opencensus.metrics.export.Distribution.BucketOptions in project instrumentation-java by census-instrumentation.
the class DistributionTest method createDistribution_NullBucket.
@Test
public void createDistribution_NullBucket() {
List<Double> bucketBounds = Arrays.asList(1.0, 2.0, 5.0);
BucketOptions bucketOptions = BucketOptions.explicitOptions(bucketBounds);
List<Bucket> buckets = Arrays.asList(Bucket.create(3), Bucket.create(1), null, Bucket.create(4));
thrown.expect(NullPointerException.class);
thrown.expectMessage("bucket");
Distribution.create(10, 6.6, 678.54, bucketOptions, buckets);
}
use of io.opencensus.metrics.export.Distribution.BucketOptions in project instrumentation-java by census-instrumentation.
the class DistributionTest method createDistribution_ZeroCountAndSumOfSquaredDeviations.
@Test
public void createDistribution_ZeroCountAndSumOfSquaredDeviations() {
List<Double> bucketBounds = Arrays.asList(1.0, 2.0, 5.0);
BucketOptions bucketOptions = BucketOptions.explicitOptions(bucketBounds);
List<Bucket> buckets = Arrays.asList(Bucket.create(0), Bucket.create(0), Bucket.create(0), Bucket.create(0));
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("sum of squared deviations should be 0 if count is 0.");
Distribution.create(0, 0, 678.54, bucketOptions, buckets);
}
use of io.opencensus.metrics.export.Distribution.BucketOptions in project instrumentation-java by census-instrumentation.
the class DistributionTest method createDistribution_NegativeSumOfSquaredDeviations.
@Test
public void createDistribution_NegativeSumOfSquaredDeviations() {
List<Double> bucketBounds = Arrays.asList(1.0, 2.0, 5.0);
BucketOptions bucketOptions = BucketOptions.explicitOptions(bucketBounds);
List<Bucket> buckets = Arrays.asList(Bucket.create(0), Bucket.create(0), Bucket.create(0), Bucket.create(0));
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("sum of squared deviations should be non-negative.");
Distribution.create(0, 6.6, -678.54, bucketOptions, buckets);
}
use of io.opencensus.metrics.export.Distribution.BucketOptions in project instrumentation-java by census-instrumentation.
the class DistributionTest method createAndGet_ExplicitBucketsEmptyBounds.
@Test
public void createAndGet_ExplicitBucketsEmptyBounds() {
List<Double> bucketBounds = new ArrayList<Double>();
BucketOptions bucketOptions = BucketOptions.explicitOptions(bucketBounds);
final List<Double> actual = new ArrayList<Double>();
bucketOptions.match(new Function<ExplicitOptions, Object>() {
@Override
public Object apply(ExplicitOptions arg) {
actual.addAll(arg.getBucketBoundaries());
return null;
}
}, Functions.throwAssertionError());
assertThat(actual).isEmpty();
}
use of io.opencensus.metrics.export.Distribution.BucketOptions 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());
}
Aggregations