Search in sources :

Example 1 with Bucket

use of com.amazon.dataprepper.model.metric.Bucket in project data-prepper by opensearch-project.

the class OTelMetricsProtoHelper method createBuckets.

/**
 * Create the buckets, see <a href="https://github.com/open-telemetry/opentelemetry-proto/blob/main/opentelemetry/proto/metrics/v1/metrics.proto">
 *     the OTel metrics proto spec</a>
 * <p>
 * The boundaries for bucket at index i are:
 * <p>
 * <pre>{@code
 * (-infinity, explicit_bounds[i]) for i == 0
 * (explicit_bounds[i-1], +infinity) for i == size(explicit_bounds)
 * (explicit_bounds[i-1], explicit_bounds[i]) for 0 < i < size(explicit_bounds)
 * }</pre>
 *
 * <br>
 * <br>
 * <b>NOTE:</b> here we map infinity as +/- FLOAT.MAX_VALUE since JSON rfc4627 only supports finite numbers and
 * OpenSearch maps double values to floats as per default.
 *
 * @param bucketCountsList   a list with the bucket counts
 * @param explicitBoundsList a list with the bounds
 * @return buckets list
 */
public static List<Bucket> createBuckets(List<Long> bucketCountsList, List<Double> explicitBoundsList) {
    List<Bucket> buckets = new ArrayList<>();
    if (bucketCountsList.isEmpty()) {
        return buckets;
    }
    if (bucketCountsList.size() - 1 != explicitBoundsList.size()) {
        LOG.error("bucket count list not equals to bounds list {} {}", bucketCountsList.size(), explicitBoundsList.size());
        throw new IllegalArgumentException("OpenTelemetry protocol mandates that the number of elements in bucket_counts array must be by one greater than\n" + "  // the number of elements in explicit_bounds array.");
    } else {
        for (int i = 0; i < bucketCountsList.size(); i++) {
            if (i == 0) {
                // "-Infinity"
                double min = -Float.MAX_VALUE;
                double max = explicitBoundsList.get(i);
                Long bucketCount = bucketCountsList.get(i);
                buckets.add(new DefaultBucket(min, max, bucketCount));
            } else if (i == bucketCountsList.size() - 1) {
                double min = explicitBoundsList.get(i - 1);
                // "Infinity"
                double max = Float.MAX_VALUE;
                Long bucketCount = bucketCountsList.get(i);
                buckets.add(new DefaultBucket(min, max, bucketCount));
            } else {
                double min = explicitBoundsList.get(i - 1);
                double max = explicitBoundsList.get(i);
                Long bucketCount = bucketCountsList.get(i);
                buckets.add(new DefaultBucket(min, max, bucketCount));
            }
        }
    }
    return buckets;
}
Also used : DefaultBucket(com.amazon.dataprepper.model.metric.DefaultBucket) Bucket(com.amazon.dataprepper.model.metric.Bucket) DefaultBucket(com.amazon.dataprepper.model.metric.DefaultBucket) ArrayList(java.util.ArrayList) NumberDataPoint(io.opentelemetry.proto.metrics.v1.NumberDataPoint) SummaryDataPoint(io.opentelemetry.proto.metrics.v1.SummaryDataPoint)

Example 2 with Bucket

use of com.amazon.dataprepper.model.metric.Bucket in project data-prepper by opensearch-project.

the class MetricsPluginHistogramTest method assertHistogramProcessing.

private void assertHistogramProcessing(Map<Object, Object> map, List<DefaultBucket> expectedBuckets) {
    assertThat(map).contains(entry("kind", Metric.KIND.HISTOGRAM.toString()));
    assertThat(map).contains(entry("unit", "seconds"));
    assertThat(map).contains(entry("description", "description"));
    assertThat(map).contains(entry("name", "name"));
    assertThat(map).contains(entry("bucketCounts", 4));
    assertThat(map).contains(entry("sum", (1d / 3d)));
    assertThat(map).contains(entry("count", 4));
    assertThat(map).contains(entry("serviceName", "service"));
    assertThat(map).contains(entry("aggregationTemporality", "AGGREGATION_TEMPORALITY_UNSPECIFIED"));
    assertThat(map).containsKey("buckets");
    List<Map> listOfMaps = (List<Map>) map.get("buckets");
    assertThat(listOfMaps).hasSize(expectedBuckets.size());
    for (int i = 0; i < expectedBuckets.size(); i++) {
        Bucket expectedBucket = expectedBuckets.get(i);
        Map<Object, Object> actualBucket = listOfMaps.get(i);
        assertThat(actualBucket).contains(entry("min", expectedBucket.getMin())).contains(entry("max", expectedBucket.getMax())).contains(entry("count", expectedBucket.getCount().intValue()));
    }
}
Also used : DefaultBucket(com.amazon.dataprepper.model.metric.DefaultBucket) Bucket(com.amazon.dataprepper.model.metric.Bucket) List(java.util.List) Map(java.util.Map) HistogramDataPoint(io.opentelemetry.proto.metrics.v1.HistogramDataPoint)

Example 3 with Bucket

use of com.amazon.dataprepper.model.metric.Bucket in project data-prepper by opensearch-project.

the class OTelMetricsProtoHelperTest method testCreateBuckets.

@Test
public void testCreateBuckets() {
    List<Long> bucketsCountList = Arrays.asList(1L, 2L, 3L, 4L);
    List<Double> explicitBOundsList = Arrays.asList(5D, 10D, 25D);
    List<Bucket> buckets = OTelMetricsProtoHelper.createBuckets(bucketsCountList, explicitBOundsList);
    assertThat(buckets.size(), equalTo(4));
    Bucket b1 = buckets.get(0);
    assertThat(b1.getCount(), equalTo(1L));
    assertThat(b1.getMin(), equalTo((double) -Float.MAX_VALUE));
    assertThat(b1.getMax(), equalTo(5D));
    Bucket b2 = buckets.get(1);
    assertThat(b2.getCount(), equalTo(2L));
    assertThat(b2.getMin(), equalTo(5D));
    assertThat(b2.getMax(), equalTo(10D));
    Bucket b3 = buckets.get(2);
    assertThat(b3.getCount(), equalTo(3L));
    assertThat(b3.getMin(), equalTo(10D));
    assertThat(b3.getMax(), equalTo(25D));
    Bucket b4 = buckets.get(3);
    assertThat(b4.getCount(), equalTo(4L));
    assertThat(b4.getMin(), equalTo(25D));
    assertThat(b4.getMax(), equalTo((double) Float.MAX_VALUE));
}
Also used : Bucket(com.amazon.dataprepper.model.metric.Bucket) Test(org.junit.jupiter.api.Test)

Aggregations

Bucket (com.amazon.dataprepper.model.metric.Bucket)3 DefaultBucket (com.amazon.dataprepper.model.metric.DefaultBucket)2 HistogramDataPoint (io.opentelemetry.proto.metrics.v1.HistogramDataPoint)1 NumberDataPoint (io.opentelemetry.proto.metrics.v1.NumberDataPoint)1 SummaryDataPoint (io.opentelemetry.proto.metrics.v1.SummaryDataPoint)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 Test (org.junit.jupiter.api.Test)1