Skip to content

Commit

Permalink
[chore] remove unnecessary error formatting when not needed (#35589)
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu authored Oct 3, 2024
1 parent 5d9fcbb commit ec129c9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottlmetric"
)

const sumCountName = "extract_count_metric"

type extractCountMetricArguments struct {
Monotonic bool
}

func newExtractCountMetricFactory() ottl.Factory[ottlmetric.TransformContext] {
return ottl.NewFactory("extract_count_metric", &extractCountMetricArguments{}, createExtractCountMetricFunction)
return ottl.NewFactory(sumCountName, &extractCountMetricArguments{}, createExtractCountMetricFunction)
}

func createExtractCountMetricFunction(_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[ottlmetric.TransformContext], error) {
Expand All @@ -34,11 +36,10 @@ func createExtractCountMetricFunction(_ ottl.FunctionContext, oArgs ottl.Argumen
func extractCountMetric(monotonic bool) (ottl.ExprFunc[ottlmetric.TransformContext], error) {
return func(_ context.Context, tCtx ottlmetric.TransformContext) (any, error) {
metric := tCtx.GetMetric()
invalidMetricTypeError := fmt.Errorf("extract_count_metric requires an input metric of type Histogram, ExponentialHistogram or Summary, got %s", metric.Type())

aggTemp := getAggregationTemporality(metric)
if aggTemp == pmetric.AggregationTemporalityUnspecified {
return nil, invalidMetricTypeError
return nil, invalidMetricTypeError(sumCountName, metric)
}

countMetric := pmetric.NewMetric()
Expand Down Expand Up @@ -66,7 +67,7 @@ func extractCountMetric(monotonic bool) (ottl.ExprFunc[ottlmetric.TransformConte
addCountDataPoint(dataPoints.At(i), countMetric.Sum().DataPoints())
}
default:
return nil, invalidMetricTypeError
return nil, invalidMetricTypeError(sumCountName, metric)
}

if countMetric.Sum().DataPoints().Len() > 0 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottlmetric"
)

const sumFuncName = "extract_sum_metric"

type extractSumMetricArguments struct {
Monotonic bool
}

func newExtractSumMetricFactory() ottl.Factory[ottlmetric.TransformContext] {
return ottl.NewFactory("extract_sum_metric", &extractSumMetricArguments{}, createExtractSumMetricFunction)
return ottl.NewFactory(sumFuncName, &extractSumMetricArguments{}, createExtractSumMetricFunction)
}

func createExtractSumMetricFunction(_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[ottlmetric.TransformContext], error) {
Expand All @@ -45,11 +47,9 @@ type SumCountDataPoint interface {
func extractSumMetric(monotonic bool) (ottl.ExprFunc[ottlmetric.TransformContext], error) {
return func(_ context.Context, tCtx ottlmetric.TransformContext) (any, error) {
metric := tCtx.GetMetric()
invalidMetricTypeError := fmt.Errorf("extract_sum_metric requires an input metric of type Histogram, ExponentialHistogram or Summary, got %s", metric.Type())

aggTemp := getAggregationTemporality(metric)
if aggTemp == pmetric.AggregationTemporalityUnspecified {
return nil, invalidMetricTypeError
return nil, invalidMetricTypeError(sumFuncName, metric)
}

sumMetric := pmetric.NewMetric()
Expand Down Expand Up @@ -83,7 +83,7 @@ func extractSumMetric(monotonic bool) (ottl.ExprFunc[ottlmetric.TransformContext
addSumDataPoint(dataPoints.At(i), sumMetric.Sum().DataPoints())
}
default:
return nil, invalidMetricTypeError
return nil, invalidMetricTypeError(sumFuncName, metric)
}

if sumMetric.Sum().DataPoints().Len() > 0 {
Expand Down Expand Up @@ -116,3 +116,7 @@ func getAggregationTemporality(metric pmetric.Metric) pmetric.AggregationTempora
return pmetric.AggregationTemporalityUnspecified
}
}

func invalidMetricTypeError(name string, metric pmetric.Metric) error {
return fmt.Errorf("%s requires an input metric of type Histogram, ExponentialHistogram or Summary, got %s", name, metric.Type())
}

0 comments on commit ec129c9

Please sign in to comment.