Search in sources :

Example 1 with Bucket

use of io.opencensus.metrics.export.Distribution.Bucket in project instrumentation-java by census-instrumentation.

the class StackdriverExportUtils method setBucketCountsAndExemplars.

// Convert OpenCensus Buckets to a list of bucket counts and a list of proto Exemplars, then set
// them to the builder.
private static void setBucketCountsAndExemplars(List<Bucket> buckets, Distribution.Builder builder) {
    // The first bucket (underflow bucket) should always be 0 count because the Metrics first bucket
    // is [0, first_bound) but StackDriver distribution consists of an underflow bucket (number 0).
    builder.addBucketCounts(0L);
    for (Bucket bucket : buckets) {
        builder.addBucketCounts(bucket.getCount());
        @javax.annotation.Nullable io.opencensus.metrics.data.Exemplar exemplar = bucket.getExemplar();
        if (exemplar != null) {
            builder.addExemplars(toProtoExemplar(exemplar));
        }
    }
}
Also used : Bucket(io.opencensus.metrics.export.Distribution.Bucket)

Example 2 with Bucket

use of io.opencensus.metrics.export.Distribution.Bucket in project instrumentation-java by census-instrumentation.

the class DistributionTest method createDistribution_ZeroCountAndPositiveMean.

@Test
public void createDistribution_ZeroCountAndPositiveMean() {
    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 should be 0 if count is 0.");
    Distribution.create(0, 6.6, 0, bucketOptions, buckets);
}
Also used : Bucket(io.opencensus.metrics.export.Distribution.Bucket) BucketOptions(io.opencensus.metrics.export.Distribution.BucketOptions) Test(org.junit.Test)

Example 3 with Bucket

use of io.opencensus.metrics.export.Distribution.Bucket in project instrumentation-java by census-instrumentation.

the class DistributionTest method createAndGet_Bucket.

@Test
public void createAndGet_Bucket() {
    Bucket bucket = Bucket.create(98);
    assertThat(bucket.getCount()).isEqualTo(98);
    assertThat(bucket.getExemplar()).isNull();
}
Also used : Bucket(io.opencensus.metrics.export.Distribution.Bucket) Test(org.junit.Test)

Example 4 with Bucket

use of io.opencensus.metrics.export.Distribution.Bucket in project instrumentation-java by census-instrumentation.

the class DistributionTest method createDistribution_NegativeCount.

@Test
public void createDistribution_NegativeCount() {
    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), Bucket.create(2), Bucket.create(4));
    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("count should be non-negative.");
    Distribution.create(-10, 6.6, 678.54, bucketOptions, buckets);
}
Also used : Bucket(io.opencensus.metrics.export.Distribution.Bucket) BucketOptions(io.opencensus.metrics.export.Distribution.BucketOptions) Test(org.junit.Test)

Example 5 with Bucket

use of io.opencensus.metrics.export.Distribution.Bucket in project instrumentation-java by census-instrumentation.

the class DistributionTest method createAndGet_Distribution.

@Test
public void createAndGet_Distribution() {
    Exemplar exemplar = Exemplar.create(15.0, TIMESTAMP, ATTACHMENTS);
    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), Bucket.create(2), Bucket.create(4, exemplar));
    Distribution distribution = Distribution.create(10, 6.6, 678.54, bucketOptions, buckets);
    assertThat(distribution.getCount()).isEqualTo(10);
    assertThat(distribution.getSum()).isWithin(TOLERANCE).of(6.6);
    assertThat(distribution.getSumOfSquaredDeviations()).isWithin(TOLERANCE).of(678.54);
    final List<Double> actual = new ArrayList<Double>();
    distribution.getBucketOptions().match(new Function<ExplicitOptions, Object>() {

        @Override
        public Object apply(ExplicitOptions arg) {
            actual.addAll(arg.getBucketBoundaries());
            return null;
        }
    }, Functions.throwAssertionError());
    assertThat(actual).containsExactlyElementsIn(bucketBounds).inOrder();
    assertThat(distribution.getBuckets()).containsExactlyElementsIn(buckets).inOrder();
}
Also used : ExplicitOptions(io.opencensus.metrics.export.Distribution.BucketOptions.ExplicitOptions) Exemplar(io.opencensus.metrics.data.Exemplar) Bucket(io.opencensus.metrics.export.Distribution.Bucket) BucketOptions(io.opencensus.metrics.export.Distribution.BucketOptions) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

Bucket (io.opencensus.metrics.export.Distribution.Bucket)9 Test (org.junit.Test)8 BucketOptions (io.opencensus.metrics.export.Distribution.BucketOptions)6 Exemplar (io.opencensus.metrics.data.Exemplar)2 ExplicitOptions (io.opencensus.metrics.export.Distribution.BucketOptions.ExplicitOptions)1 ArrayList (java.util.ArrayList)1