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/cumulativetodelta] Add metric type filter #34407

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
0b6b871
[receiver/cumulativetodelta] add filter for metric type
bacherfl Aug 2, 2024
9a4764b
add unit tests
bacherfl Aug 5, 2024
ed6f12f
add docs, changelog entry and additional tests
bacherfl Aug 5, 2024
3921fa4
fix linting
bacherfl Aug 5, 2024
55985d8
fix linting
bacherfl Aug 5, 2024
eb9980b
Merge branch 'main' into feat/ctdprocessor-type-filter
bacherfl Aug 5, 2024
aed59b8
Merge branch 'main' into feat/ctdprocessor-type-filter
bacherfl Aug 22, 2024
688a2a0
Merge branch 'main' into feat/ctdprocessor-type-filter
bacherfl Sep 16, 2024
6b8a148
Merge branch 'main' into feat/ctdprocessor-type-filter
bacherfl Sep 17, 2024
2828ccf
Merge branch 'main' into feat/ctdprocessor-type-filter
bacherfl Sep 23, 2024
4b9be70
Merge branch 'main' into feat/ctdprocessor-type-filter
bacherfl Oct 9, 2024
2f74c14
Merge branch 'main' into feat/ctdprocessor-type-filter
bacherfl Oct 10, 2024
94e4f4b
Merge branch 'main' into feat/ctdprocessor-type-filter
bacherfl Oct 29, 2024
5df602a
Merge branch 'main' into feat/ctdprocessor-type-filter
bacherfl Nov 4, 2024
172d902
incorporate feedback from PR review
bacherfl Nov 4, 2024
0554bbd
fix linting
bacherfl Nov 4, 2024
6ff0493
fix unit test
bacherfl Nov 4, 2024
19d520f
Merge branch 'main' into feat/ctdprocessor-type-filter
bacherfl Nov 13, 2024
e032b9b
Merge branch 'main' into feat/ctdprocessor-type-filter
bacherfl Nov 14, 2024
4eb9f40
Merge branch 'main' into feat/ctdprocessor-type-filter
bacherfl Nov 19, 2024
79b8e73
Merge branch 'main' into feat/ctdprocessor-type-filter
bacherfl Nov 22, 2024
8ef94fb
Merge branch 'main' into feat/ctdprocessor-type-filter
bacherfl Nov 26, 2024
661268f
Merge branch 'main' into feat/ctdprocessor-type-filter
bacherfl Dec 6, 2024
6e5be14
Merge branch 'main' into feat/ctdprocessor-type-filter
bacherfl Dec 9, 2024
efc63fc
Merge branch 'main' into feat/ctdprocessor-type-filter
bacherfl Dec 10, 2024
96333e2
Merge branch 'main' into feat/ctdprocessor-type-filter
bacherfl Dec 17, 2024
74f09ae
return error if metric type filter contains an unsupported type
bacherfl Dec 17, 2024
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
4 changes: 2 additions & 2 deletions processor/cumulativetodeltaprocessor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ Configuration is specified through a list of metrics. The processor uses metric

The following settings can be optionally configured:

- `include`: List of metrics names, patterns or metric types to convert to delta.
- `exclude`: List of metrics names, patterns or metric types to not convert to delta. **If a metric name matches both include and exclude, exclude takes precedence.**
- `include`: List of metrics names (case-insensitive), patterns or metric types to convert to delta. Valid values are: `sum`, `histogram`.
- `exclude`: List of metrics names (case-insensitive), patterns or metric types to not convert to delta. **If a metric name matches both include and exclude, exclude takes precedence.** Valid values are: `sum`, `histogram`.
- `max_staleness`: The total time a state entry will live past the time it was last seen. Set to 0 to retain state indefinitely. Default: 0
- `initial_value`: Handling of the first observed point for a given metric identity.
When the collector (re)starts, there's no record of how much of a given cumulative counter has already been converted to delta values.
Expand Down
24 changes: 12 additions & 12 deletions processor/cumulativetodeltaprocessor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package cumulativetodeltaprocessor // import "github.com/open-telemetry/opentele

import (
"fmt"
"slices"
"golang.org/x/exp/maps"
"strings"
"time"

Expand All @@ -16,11 +16,13 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/cumulativetodeltaprocessor/internal/tracking"
)

var validMetricTypes = []string{
strings.ToLower(pmetric.MetricTypeSum.String()),
strings.ToLower(pmetric.MetricTypeHistogram.String()),
var validMetricTypes = map[string]bool{
strings.ToLower(pmetric.MetricTypeSum.String()): true,
strings.ToLower(pmetric.MetricTypeHistogram.String()): true,
}

var validMetricTypeList = maps.Keys(validMetricTypes)

// Config defines the configuration for the processor.
type Config struct {
// MaxStaleness is the total time a state entry will live past the time it was last seen. Set to 0 to retain state indefinitely.
Expand Down Expand Up @@ -63,23 +65,21 @@ func (config *Config) Validate() error {
return fmt.Errorf("metrics must be supplied if match_type is set")
}

for i, metricType := range config.Exclude.MetricTypes {
config.Exclude.MetricTypes[i] = strings.ToLower(metricType)
if !slices.Contains(validMetricTypes, config.Exclude.MetricTypes[i]) {
for _, metricType := range config.Exclude.MetricTypes {
if valid := validMetricTypes[strings.ToLower(metricType)]; !valid {
return fmt.Errorf(
"found invalid metric type in exclude.metric_types: %s. Valid values are [%s]",
metricType,
strings.Join(validMetricTypes, ","),
strings.Join(validMetricTypeList, ","),
)
}
}
for i, metricType := range config.Include.MetricTypes {
config.Include.MetricTypes[i] = strings.ToLower(metricType)
if !slices.Contains(validMetricTypes, config.Include.MetricTypes[i]) {
for _, metricType := range config.Include.MetricTypes {
if valid := validMetricTypes[strings.ToLower(metricType)]; !valid {
return fmt.Errorf(
"found invalid metric type in include.metric_types: %s. Valid values are [%s]",
metricType,
strings.Join(validMetricTypes, ","),
strings.Join(validMetricTypeList, ","),
)
}
}
Expand Down
1 change: 1 addition & 0 deletions processor/cumulativetodeltaprocessor/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
go.opentelemetry.io/collector/processor/processortest v0.112.0
go.uber.org/goleak v1.3.0
go.uber.org/zap v1.27.0
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842
)

require (
Expand Down
2 changes: 2 additions & 0 deletions processor/cumulativetodeltaprocessor/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 22 additions & 10 deletions processor/cumulativetodeltaprocessor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ package cumulativetodeltaprocessor // import "github.com/open-telemetry/opentele

import (
"context"
"math"
"slices"
"strings"

"go.opentelemetry.io/collector/pdata/pmetric"
"go.uber.org/zap"
"math"
"strings"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterset"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/cumulativetodeltaprocessor/internal/tracking"
Expand All @@ -19,8 +17,8 @@ import (
type cumulativeToDeltaProcessor struct {
includeFS filterset.FilterSet
excludeFS filterset.FilterSet
includeMetricTypes []string
excludeMetricTypes []string
includeMetricTypes map[pmetric.MetricType]bool
excludeMetricTypes map[pmetric.MetricType]bool
logger *zap.Logger
deltaCalculator *tracking.MetricTracker
cancelFunc context.CancelFunc
Expand All @@ -32,8 +30,8 @@ func newCumulativeToDeltaProcessor(config *Config, logger *zap.Logger) *cumulati
logger: logger,
deltaCalculator: tracking.NewMetricTracker(ctx, logger, config.MaxStaleness, config.InitialValue),
cancelFunc: cancel,
includeMetricTypes: config.Include.MetricTypes,
excludeMetricTypes: config.Exclude.MetricTypes,
includeMetricTypes: getMetricTypeFilter(config.Include.MetricTypes),
excludeMetricTypes: getMetricTypeFilter(config.Exclude.MetricTypes),
}
if len(config.Include.Metrics) > 0 {
p.includeFS, _ = filterset.CreateFilterSet(config.Include.Metrics, &config.Include.Config)
Expand All @@ -44,6 +42,20 @@ func newCumulativeToDeltaProcessor(config *Config, logger *zap.Logger) *cumulati
return p
}

func getMetricTypeFilter(types []string) map[pmetric.MetricType]bool {
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved
res := map[pmetric.MetricType]bool{}
for _, t := range types {
switch strings.ToLower(t) {
case strings.ToLower(pmetric.MetricTypeSum.String()):
res[pmetric.MetricTypeSum] = true
case strings.ToLower(pmetric.MetricTypeHistogram.String()):
res[pmetric.MetricTypeHistogram] = true
default:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there are metric types we don't support we should error.

}
}
return res
}

// processMetrics implements the ProcessMetricsFunc type.
func (ctdp *cumulativeToDeltaProcessor) processMetrics(_ context.Context, md pmetric.Metrics) (pmetric.Metrics, error) {
md.ResourceMetrics().RemoveIf(func(rm pmetric.ResourceMetrics) bool {
Expand Down Expand Up @@ -119,9 +131,9 @@ func (ctdp *cumulativeToDeltaProcessor) shutdown(context.Context) error {

func (ctdp *cumulativeToDeltaProcessor) shouldConvertMetric(metric pmetric.Metric) bool {
return (ctdp.includeFS == nil || ctdp.includeFS.Matches(metric.Name())) &&
(len(ctdp.includeMetricTypes) == 0 || slices.Contains(ctdp.includeMetricTypes, strings.ToLower(metric.Type().String()))) &&
(len(ctdp.includeMetricTypes) == 0 || ctdp.includeMetricTypes[metric.Type()]) &&
(ctdp.excludeFS == nil || !ctdp.excludeFS.Matches(metric.Name())) &&
(len(ctdp.excludeMetricTypes) == 0 || !slices.Contains(ctdp.excludeMetricTypes, strings.ToLower(metric.Type().String())))
(len(ctdp.excludeMetricTypes) == 0 || !ctdp.excludeMetricTypes[metric.Type()])
}

func (ctdp *cumulativeToDeltaProcessor) convertNumberDataPoints(dps pmetric.NumberDataPointSlice, baseIdentity tracking.MetricIdentity) {
Expand Down
Loading