Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable exporting Histogram aggregation to OTLP metric #1209

Merged
merged 6 commits into from
Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased]

### Added

- OTLP Metric exporter supports Histogram aggregation. (#1209)

## [0.12.0] - 2020-09-24

### Added
Expand Down
70 changes: 69 additions & 1 deletion exporters/otlp/internal/transform/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,13 @@ func Record(r export.Record) (*metricpb.Metric, error) {
}
return minMaxSumCount(r, mmsc)

case aggregation.HistogramKind:
h, ok := agg.(aggregation.Histogram)
if !ok {
return nil, fmt.Errorf("%w: %T", ErrIncompatibleAgg, agg)
}
return histogram(r, h)

case aggregation.SumKind:
s, ok := agg.(aggregation.Sum)
if !ok {
Expand Down Expand Up @@ -327,7 +334,7 @@ func scalar(record export.Record, num metric.Number, start, end time.Time) (*met
}

// minMaxSumCountValue returns the values of the MinMaxSumCount Aggregator
// as discret values.
// as discrete values.
func minMaxSumCountValues(a aggregation.MinMaxSumCount) (min, max, sum metric.Number, count int64, err error) {
if min, err = a.Min(); err != nil {
return
Expand Down Expand Up @@ -383,6 +390,67 @@ func minMaxSumCount(record export.Record, a aggregation.MinMaxSumCount) (*metric
}, nil
}

func histogramValues(a aggregation.Histogram) (boundaries []float64, counts []float64, err error) {
var buckets aggregation.Buckets
if buckets, err = a.Histogram(); err != nil {
return
}
boundaries, counts = buckets.Boundaries, buckets.Counts
if len(counts) != len(boundaries)+1 {
err = ErrTransforming
return
}
return
}

// histogram transforms a Histogram Aggregator into an OTLP Metric.
func histogram(record export.Record, a aggregation.Histogram) (*metricpb.Metric, error) {
desc := record.Descriptor()
labels := record.Labels()
boundaries, counts, err := histogramValues(a)
if err != nil {
return nil, err
}

count, err := a.Count()
if err != nil {
return nil, err
}

sum, err := a.Sum()
if err != nil {
return nil, err
}

buckets := make([]*metricpb.HistogramDataPoint_Bucket, len(counts))
for i := 0; i < len(counts); i++ {
buckets[i] = &metricpb.HistogramDataPoint_Bucket{
Count: uint64(counts[i]),
}
}

numKind := desc.NumberKind()
return &metricpb.Metric{
MetricDescriptor: &metricpb.MetricDescriptor{
Name: desc.Name(),
Description: desc.Description(),
Unit: string(desc.Unit()),
Type: metricpb.MetricDescriptor_HISTOGRAM,
},
HistogramDataPoints: []*metricpb.HistogramDataPoint{
{
Labels: stringKeyValues(labels.Iter()),
StartTimeUnixNano: toNanos(record.StartTime()),
TimeUnixNano: toNanos(record.EndTime()),
Count: uint64(count),
Sum: sum.CoerceToFloat64(numKind),
Buckets: buckets,
ExplicitBounds: boundaries,
},
},
}, nil
}

// stringKeyValues transforms a label iterator into an OTLP StringKeyValues.
func stringKeyValues(iter label.Iterator) []*commonpb.StringKeyValue {
l := iter.Len()
Expand Down
44 changes: 17 additions & 27 deletions exporters/otlp/otlp_metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
metricsdk "go.opentelemetry.io/otel/sdk/export/metric"
"go.opentelemetry.io/otel/sdk/export/metric/aggregation"
"go.opentelemetry.io/otel/sdk/export/metric/metrictest"
"go.opentelemetry.io/otel/sdk/metric/aggregator/minmaxsumcount"
"go.opentelemetry.io/otel/sdk/metric/aggregator/histogram"
"go.opentelemetry.io/otel/sdk/metric/aggregator/sum"
"go.opentelemetry.io/otel/sdk/resource"

Expand Down Expand Up @@ -107,6 +107,8 @@ var (
testInstA = resource.New(label.String("instance", "tester-a"))
testInstB = resource.New(label.String("instance", "tester-b"))

testHistogramBoundaries = []float64{2.0, 4.0, 8.0}

md = &metricpb.MetricDescriptor{
Name: "int64-count",
Type: metricpb.MetricDescriptor_INT64,
Expand Down Expand Up @@ -229,9 +231,9 @@ func TestValuerecorderMetricGroupingExport(t *testing.T) {
{
MetricDescriptor: &metricpb.MetricDescriptor{
Name: "valuerecorder",
Type: metricpb.MetricDescriptor_SUMMARY,
Type: metricpb.MetricDescriptor_HISTOGRAM,
},
SummaryDataPoints: []*metricpb.SummaryDataPoint{
HistogramDataPoints: []*metricpb.HistogramDataPoint{
{
Labels: []*commonpb.StringKeyValue{
{
Expand All @@ -243,20 +245,14 @@ func TestValuerecorderMetricGroupingExport(t *testing.T) {
Value: "test.com",
},
},
Count: 2,
Sum: 11,
PercentileValues: []*metricpb.SummaryDataPoint_ValueAtPercentile{
{
Percentile: 0.0,
Value: 1.0,
},
{
Percentile: 100.0,
Value: 10.0,
},
},
StartTimeUnixNano: startTime(),
TimeUnixNano: pointTime(),
Count: 2,
Sum: 11,
ExplicitBounds: testHistogramBoundaries,
Buckets: []*metricpb.HistogramDataPoint_Bucket{
{Count: 1}, {Count: 0}, {Count: 0}, {Count: 1},
},
},
{
Labels: []*commonpb.StringKeyValue{
Expand All @@ -269,17 +265,11 @@ func TestValuerecorderMetricGroupingExport(t *testing.T) {
Value: "test.com",
},
},
Count: 2,
Sum: 11,
PercentileValues: []*metricpb.SummaryDataPoint_ValueAtPercentile{
{
Percentile: 0.0,
Value: 1.0,
},
{
Percentile: 100.0,
Value: 10.0,
},
Count: 2,
Sum: 11,
ExplicitBounds: testHistogramBoundaries,
Buckets: []*metricpb.HistogramDataPoint_Bucket{
{Count: 1}, {Count: 0}, {Count: 0}, {Count: 1},
},
StartTimeUnixNano: startTime(),
TimeUnixNano: pointTime(),
Expand Down Expand Up @@ -690,7 +680,7 @@ func runMetricExportTest(t *testing.T, exp *Exporter, rs []record, expected []me
case metric.CounterKind:
agg, ckpt = metrictest.Unslice2(sum.New(2))
default:
agg, ckpt = metrictest.Unslice2(minmaxsumcount.New(2, &desc))
agg, ckpt = metrictest.Unslice2(histogram.New(2, &desc, testHistogramBoundaries))
}

ctx := context.Background()
Expand Down
3 changes: 2 additions & 1 deletion sdk/export/metric/aggregation/aggregation.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type (
Sum() (metric.Number, error)
}

// Sum returns the number of values that were aggregated.
// Count returns the number of values that were aggregated.
Count interface {
Aggregation
Count() (int64, error)
Expand Down Expand Up @@ -95,6 +95,7 @@ type (
// Histogram returns the count of events in pre-determined buckets.
Histogram interface {
Aggregation
Count() (int64, error)
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
Sum() (metric.Number, error)
Histogram() (Buckets, error)
}
Expand Down