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

[processor/transform] Allow covert_summary methods to use custom metric suffix #35141

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .chloggen/33850-transform-summary-custom-metric-suffix.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: transformprocessor

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Allow covert_summary methods to use custom metric suffix

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [33850]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
8 changes: 4 additions & 4 deletions processor/transformprocessor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,13 +302,13 @@ Examples:

### convert_summary_count_val_to_sum

`convert_summary_count_val_to_sum(aggregation_temporality, is_monotonic)`
`convert_summary_count_val_to_sum(aggregation_temporality, is_monotonic, Optional[suffix])`

The `convert_summary_count_val_to_sum` function creates a new Sum metric from a Summary's count value.

`aggregation_temporality` is a string (`"cumulative"` or `"delta"`) representing the desired aggregation temporality of the new metric. `is_monotonic` is a boolean representing the monotonicity of the new metric.

The name for the new metric will be `<summary metric name>_count`. The fields that are copied are: `timestamp`, `starttimestamp`, `attibutes`, and `description`. The new metric that is created will be passed to all functions in the metrics statements list. Function conditions will apply.
The name for the new metric will default to `<summary metric name>_count`. The metric suffix can be configured by providing the optional suffix argument. The fields that are copied are: `timestamp`, `starttimestamp`, `attibutes`, and `description`. The new metric that is created will be passed to all functions in the metrics statements list. Function conditions will apply.

**NOTE:** This function may cause a metric to break semantics for [Sum metrics](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/data-model.md#sums). Use at your own risk.

Expand All @@ -321,13 +321,13 @@ Examples:

### convert_summary_sum_val_to_sum

`convert_summary_sum_val_to_sum(aggregation_temporality, is_monotonic)`
`convert_summary_sum_val_to_sum(aggregation_temporality, is_monotonic, Optional[suffix])`

The `convert_summary_sum_val_to_sum` function creates a new Sum metric from a Summary's sum value.

`aggregation_temporality` is a string (`"cumulative"` or `"delta"`) representing the desired aggregation temporality of the new metric. `is_monotonic` is a boolean representing the monotonicity of the new metric.

The name for the new metric will be `<summary metric name>_sum`. The fields that are copied are: `timestamp`, `starttimestamp`, `attibutes`, and `description`. The new metric that is created will be passed to all functions in the metrics statements list. Function conditions will apply.
The name for the new metric will default to `<summary metric name>_sum`. The metric suffix can be configured by providing the optional suffix argument. The fields that are copied are: `timestamp`, `starttimestamp`, `attibutes`, and `description`. The new metric that is created will be passed to all functions in the metrics statements list. Function conditions will apply.

**NOTE:** This function may cause a metric to break semantics for [Sum metrics](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/data-model.md#sums). Use at your own risk.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
type convertSummaryCountValToSumArguments struct {
StringAggTemp string
Monotonic bool
Suffix ottl.Optional[string]
}

func newConvertSummaryCountValToSumFactory() ottl.Factory[ottldatapoint.TransformContext] {
Expand All @@ -29,10 +30,10 @@ func createConvertSummaryCountValToSumFunction(_ ottl.FunctionContext, oArgs ott
return nil, fmt.Errorf("convertSummaryCountValToSumFactory args must be of type *convertSummaryCountValToSumArguments")
}

return convertSummaryCountValToSum(args.StringAggTemp, args.Monotonic)
return convertSummaryCountValToSum(args.StringAggTemp, args.Monotonic, args.Suffix)
}

func convertSummaryCountValToSum(stringAggTemp string, monotonic bool) (ottl.ExprFunc[ottldatapoint.TransformContext], error) {
func convertSummaryCountValToSum(stringAggTemp string, monotonic bool, optSuffix ottl.Optional[string]) (ottl.ExprFunc[ottldatapoint.TransformContext], error) {
var aggTemp pmetric.AggregationTemporality
switch stringAggTemp {
case "delta":
Expand All @@ -42,6 +43,10 @@ func convertSummaryCountValToSum(stringAggTemp string, monotonic bool) (ottl.Exp
default:
return nil, fmt.Errorf("unknown aggregation temporality: %s", stringAggTemp)
}
suffix := "_count"
if !optSuffix.IsEmpty() {
suffix = optSuffix.Get()
}
return func(_ context.Context, tCtx ottldatapoint.TransformContext) (any, error) {
metric := tCtx.GetMetric()
if metric.Type() != pmetric.MetricTypeSummary {
Expand All @@ -50,7 +55,7 @@ func convertSummaryCountValToSum(stringAggTemp string, monotonic bool) (ottl.Exp

sumMetric := tCtx.GetMetrics().AppendEmpty()
sumMetric.SetDescription(metric.Description())
sumMetric.SetName(metric.Name() + "_count")
sumMetric.SetName(metric.Name() + suffix)
sumMetric.SetUnit(metric.Unit())
sumMetric.SetEmptySum().SetAggregationTemporality(aggTemp)
sumMetric.Sum().SetIsMonotonic(monotonic)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottldatapoint"
)

Expand Down Expand Up @@ -78,6 +79,28 @@ func Test_ConvertSummaryCountValToSum(t *testing.T) {
attrs.CopyTo(dp.Attributes())
},
},
{
name: "convert_summary_count_val_to_sum (custom_metric_suffix)",
input: getTestSummaryMetric(),
suffix: ottl.NewTestingOptional[string](".count_value"),
temporality: "cumulative",
monotonicity: false,
want: func(metrics pmetric.MetricSlice) {
summaryMetric := getTestSummaryMetric()
summaryMetric.CopyTo(metrics.AppendEmpty())
sumMetric := metrics.AppendEmpty()
sumMetric.SetEmptySum()
sumMetric.Sum().SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
sumMetric.Sum().SetIsMonotonic(false)

sumMetric.SetName("summary_metric.count_value")
dp := sumMetric.Sum().DataPoints().AppendEmpty()
dp.SetIntValue(100)

attrs := getTestAttributes()
attrs.CopyTo(dp.Attributes())
},
},
{
name: "convert_summary_count_val_to_sum (no op)",
input: getTestGaugeMetric(),
Expand All @@ -94,7 +117,7 @@ func Test_ConvertSummaryCountValToSum(t *testing.T) {
actualMetrics := pmetric.NewMetricSlice()
tt.input.CopyTo(actualMetrics.AppendEmpty())

evaluate, err := convertSummaryCountValToSum(tt.temporality, tt.monotonicity)
evaluate, err := convertSummaryCountValToSum(tt.temporality, tt.monotonicity, tt.suffix)
assert.NoError(t, err)

_, err = evaluate(nil, ottldatapoint.NewTransformContext(pmetric.NewNumberDataPoint(), tt.input, actualMetrics, pcommon.NewInstrumentationScope(), pcommon.NewResource(), pmetric.NewScopeMetrics(), pmetric.NewResourceMetrics()))
Expand All @@ -111,16 +134,28 @@ func Test_ConvertSummaryCountValToSum_validation(t *testing.T) {
tests := []struct {
name string
stringAggTemp string
suffix ottl.Optional[string]
errorMsg string
}{
{
name: "invalid aggregation temporality",
stringAggTemp: "not a real aggregation temporality",
errorMsg: "unknown aggregation temporality",
},
{
name: "valid",
suffix: ottl.NewTestingOptional[string](".count"),
stringAggTemp: "delta",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := convertSummaryCountValToSum(tt.stringAggTemp, true)
assert.Error(t, err, "unknown aggregation temporality: not a real aggregation temporality")
_, err := convertSummaryCountValToSum(tt.stringAggTemp, true, tt.suffix)
if tt.errorMsg != "" {
assert.ErrorContains(t, err, tt.errorMsg)
} else {
assert.NoError(t, err)
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
type convertSummarySumValToSumArguments struct {
StringAggTemp string
Monotonic bool
Suffix ottl.Optional[string]
}

func newConvertSummarySumValToSumFactory() ottl.Factory[ottldatapoint.TransformContext] {
Expand All @@ -29,10 +30,10 @@ func createConvertSummarySumValToSumFunction(_ ottl.FunctionContext, oArgs ottl.
return nil, fmt.Errorf("convertSummarySumValToSumFactory args must be of type *convertSummarySumValToSumArguments")
}

return convertSummarySumValToSum(args.StringAggTemp, args.Monotonic)
return convertSummarySumValToSum(args.StringAggTemp, args.Monotonic, args.Suffix)
}

func convertSummarySumValToSum(stringAggTemp string, monotonic bool) (ottl.ExprFunc[ottldatapoint.TransformContext], error) {
func convertSummarySumValToSum(stringAggTemp string, monotonic bool, optSuffix ottl.Optional[string]) (ottl.ExprFunc[ottldatapoint.TransformContext], error) {
var aggTemp pmetric.AggregationTemporality
switch stringAggTemp {
case "delta":
Expand All @@ -42,6 +43,10 @@ func convertSummarySumValToSum(stringAggTemp string, monotonic bool) (ottl.ExprF
default:
return nil, fmt.Errorf("unknown aggregation temporality: %s", stringAggTemp)
}
suffix := "_sum"
if !optSuffix.IsEmpty() {
suffix = optSuffix.Get()
}
return func(_ context.Context, tCtx ottldatapoint.TransformContext) (any, error) {
metric := tCtx.GetMetric()
if metric.Type() != pmetric.MetricTypeSummary {
Expand All @@ -50,7 +55,7 @@ func convertSummarySumValToSum(stringAggTemp string, monotonic bool) (ottl.ExprF

sumMetric := tCtx.GetMetrics().AppendEmpty()
sumMetric.SetDescription(metric.Description())
sumMetric.SetName(metric.Name() + "_sum")
sumMetric.SetName(metric.Name() + suffix)
sumMetric.SetUnit(metric.Unit())
sumMetric.SetEmptySum().SetAggregationTemporality(aggTemp)
sumMetric.Sum().SetIsMonotonic(monotonic)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottldatapoint"
)

Expand All @@ -18,6 +19,7 @@ type summaryTestCase struct {
input pmetric.Metric
temporality string
monotonicity bool
suffix ottl.Optional[string]
want func(pmetric.MetricSlice)
}

Expand Down Expand Up @@ -86,6 +88,28 @@ func Test_ConvertSummarySumValToSum(t *testing.T) {
attrs.CopyTo(dp.Attributes())
},
},
{
name: "convert_summary_sum_val_to_sum (custom_metric_suffix)",
input: getTestSummaryMetric(),
suffix: ottl.NewTestingOptional[string](".sum_value"),
temporality: "cumulative",
monotonicity: false,
want: func(metrics pmetric.MetricSlice) {
summaryMetric := getTestSummaryMetric()
summaryMetric.CopyTo(metrics.AppendEmpty())
sumMetric := metrics.AppendEmpty()
sumMetric.SetEmptySum()
sumMetric.Sum().SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
sumMetric.Sum().SetIsMonotonic(false)

sumMetric.SetName("summary_metric.sum_value")
dp := sumMetric.Sum().DataPoints().AppendEmpty()
dp.SetDoubleValue(12.34)

attrs := getTestAttributes()
attrs.CopyTo(dp.Attributes())
},
},
{
name: "convert_summary_sum_val_to_sum (no op)",
input: getTestGaugeMetric(),
Expand All @@ -102,7 +126,7 @@ func Test_ConvertSummarySumValToSum(t *testing.T) {
actualMetrics := pmetric.NewMetricSlice()
tt.input.CopyTo(actualMetrics.AppendEmpty())

evaluate, err := convertSummarySumValToSum(tt.temporality, tt.monotonicity)
evaluate, err := convertSummarySumValToSum(tt.temporality, tt.monotonicity, tt.suffix)
assert.NoError(t, err)

_, err = evaluate(nil, ottldatapoint.NewTransformContext(pmetric.NewNumberDataPoint(), tt.input, actualMetrics, pcommon.NewInstrumentationScope(), pcommon.NewResource(), pmetric.NewScopeMetrics(), pmetric.NewResourceMetrics()))
Expand All @@ -119,16 +143,28 @@ func Test_ConvertSummarySumValToSum_validation(t *testing.T) {
tests := []struct {
name string
stringAggTemp string
suffix ottl.Optional[string]
errorMsg string
}{
{
name: "invalid aggregation temporality",
stringAggTemp: "not a real aggregation temporality",
errorMsg: "unknown aggregation temporality",
},
{
name: "valid",
suffix: ottl.NewTestingOptional[string](".sum.us"),
stringAggTemp: "delta",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := convertSummarySumValToSum(tt.stringAggTemp, true)
assert.Error(t, err, "unknown aggregation temporality: not a real aggregation temporality")
_, err := convertSummarySumValToSum(tt.stringAggTemp, true, tt.suffix)
if tt.errorMsg != "" {
assert.ErrorContains(t, err, tt.errorMsg)
} else {
assert.NoError(t, err)
}
})
}
}