Skip to content

Commit

Permalink
Factor our common aggregation temporality logic
Browse files Browse the repository at this point in the history
  • Loading branch information
swiatekm committed Aug 7, 2023
1 parent 2b3c476 commit 45c39e9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,11 @@ func createExtractCountMetricFunction(_ ottl.FunctionContext, oArgs ottl.Argumen

func extractCountMetric(monotonic bool) (ottl.ExprFunc[ottlmetric.TransformContext], error) {
return func(_ context.Context, tCtx ottlmetric.TransformContext) (interface{}, error) {
var aggTemp pmetric.AggregationTemporality
metric := tCtx.GetMetric()
invalidMetricTypeError := fmt.Errorf("extract_count_metric requires an input metric of type Histogram, ExponentialHistogram or Summary, got %s", metric.Type())

switch metric.Type() {
case pmetric.MetricTypeHistogram:
aggTemp = metric.Histogram().AggregationTemporality()
case pmetric.MetricTypeExponentialHistogram:
aggTemp = metric.ExponentialHistogram().AggregationTemporality()
case pmetric.MetricTypeSummary:
// Summaries don't have an aggregation temporality, but they *should* be cumulative based on the Openmetrics spec.
// This should become an optional argument once those are available in OTTL.
aggTemp = pmetric.AggregationTemporalityCumulative
default:
aggTemp := getAggregationTemporality(metric)
if aggTemp == pmetric.AggregationTemporalityUnspecified {
return nil, invalidMetricTypeError
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,11 @@ type SumCountDataPoint interface {

func extractSumMetric(monotonic bool) (ottl.ExprFunc[ottlmetric.TransformContext], error) {
return func(_ context.Context, tCtx ottlmetric.TransformContext) (interface{}, error) {
var aggTemp pmetric.AggregationTemporality
metric := tCtx.GetMetric()
invalidMetricTypeError := fmt.Errorf("extract_sum_metric requires an input metric of type Histogram, ExponentialHistogram or Summary, got %s", metric.Type())

switch metric.Type() {
case pmetric.MetricTypeHistogram:
aggTemp = metric.Histogram().AggregationTemporality()
case pmetric.MetricTypeExponentialHistogram:
aggTemp = metric.ExponentialHistogram().AggregationTemporality()
case pmetric.MetricTypeSummary:
// Summaries don't have an aggregation temporality, but they *should* be cumulative based on the Openmetrics spec.
// This should become an optional argument once those are available in OTTL.
aggTemp = pmetric.AggregationTemporalityCumulative
default:
aggTemp := getAggregationTemporality(metric)
if aggTemp == pmetric.AggregationTemporalityUnspecified {
return nil, invalidMetricTypeError
}

Expand Down Expand Up @@ -110,3 +101,18 @@ func addSumDataPoint(dataPoint SumCountDataPoint, destination pmetric.NumberData
newDp.SetStartTimestamp(dataPoint.StartTimestamp())
newDp.SetTimestamp(dataPoint.Timestamp())
}

func getAggregationTemporality(metric pmetric.Metric) pmetric.AggregationTemporality {
switch metric.Type() {
case pmetric.MetricTypeHistogram:
return metric.Histogram().AggregationTemporality()
case pmetric.MetricTypeExponentialHistogram:
return metric.ExponentialHistogram().AggregationTemporality()
case pmetric.MetricTypeSummary:
// Summaries don't have an aggregation temporality, but they *should* be cumulative based on the Openmetrics spec.
// This should become an optional argument once those are available in OTTL.
return pmetric.AggregationTemporalityCumulative
default:
return pmetric.AggregationTemporalityUnspecified
}
}

0 comments on commit 45c39e9

Please sign in to comment.