Skip to content

Commit a90086d

Browse files
author
Alex Boten
committed
WIP [processor/groupbyattrs] move from OpenCensus to OpenTelemetry
Fixes open-telemetry#30763 Signed-off-by: Alex Boten <[email protected]>
1 parent 0e28e15 commit a90086d

File tree

7 files changed

+185
-132
lines changed

7 files changed

+185
-132
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: groupbyattributesprocessor
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: move metrics from OpenCensus to OpenTelemetry
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [30763]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: []

processor/groupbyattrsprocessor/factory.go

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ package groupbyattrsprocessor // import "github.com/open-telemetry/opentelemetry
55

66
import (
77
"context"
8-
"sync"
98

10-
"go.opencensus.io/stats/view"
119
"go.opentelemetry.io/collector/component"
1210
"go.opentelemetry.io/collector/consumer"
1311
"go.opentelemetry.io/collector/processor"
@@ -21,15 +19,8 @@ var (
2119
consumerCapabilities = consumer.Capabilities{MutatesData: true}
2220
)
2321

24-
var once sync.Once
25-
2622
// NewFactory returns a new factory for the Filter processor.
2723
func NewFactory() processor.Factory {
28-
once.Do(func() {
29-
// TODO: as with other -contrib factories registering metrics, this is causing the error being ignored
30-
_ = view.Register(metricViews()...)
31-
})
32-
3324
return processor.NewFactory(
3425
metadata.Type,
3526
createDefaultConfig,
@@ -45,23 +36,27 @@ func createDefaultConfig() component.Config {
4536
}
4637
}
4738

48-
func createGroupByAttrsProcessor(logger *zap.Logger, attributes []string) *groupByAttrsProcessor {
39+
func createGroupByAttrsProcessor(set processor.CreateSettings, attributes []string) (*groupByAttrsProcessor, error) {
4940
var nonEmptyAttributes []string
5041
presentAttributes := make(map[string]struct{})
5142

5243
for _, str := range attributes {
5344
if str != "" {
5445
_, isPresent := presentAttributes[str]
5546
if isPresent {
56-
logger.Warn("A grouping key is already present", zap.String("key", str))
47+
set.Logger.Warn("A grouping key is already present", zap.String("key", str))
5748
} else {
5849
nonEmptyAttributes = append(nonEmptyAttributes, str)
5950
presentAttributes[str] = struct{}{}
6051
}
6152
}
6253
}
6354

64-
return &groupByAttrsProcessor{logger: logger, groupByKeys: nonEmptyAttributes}
55+
it, err := newProcessorTelemetry(set.TelemetrySettings)
56+
if err != nil {
57+
return nil, err
58+
}
59+
return &groupByAttrsProcessor{logger: set.Logger, groupByKeys: nonEmptyAttributes, internalTelemetry: it}, nil
6560
}
6661

6762
// createTracesProcessor creates a trace processor based on this config.
@@ -72,7 +67,10 @@ func createTracesProcessor(
7267
nextConsumer consumer.Traces) (processor.Traces, error) {
7368

7469
oCfg := cfg.(*Config)
75-
gap := createGroupByAttrsProcessor(set.Logger, oCfg.GroupByKeys)
70+
gap, err := createGroupByAttrsProcessor(set, oCfg.GroupByKeys)
71+
if err != nil {
72+
return nil, err
73+
}
7674

7775
return processorhelper.NewTracesProcessor(
7876
ctx,
@@ -91,7 +89,10 @@ func createLogsProcessor(
9189
nextConsumer consumer.Logs) (processor.Logs, error) {
9290

9391
oCfg := cfg.(*Config)
94-
gap := createGroupByAttrsProcessor(set.Logger, oCfg.GroupByKeys)
92+
gap, err := createGroupByAttrsProcessor(set, oCfg.GroupByKeys)
93+
if err != nil {
94+
return nil, err
95+
}
9596

9697
return processorhelper.NewLogsProcessor(
9798
ctx,
@@ -110,7 +111,10 @@ func createMetricsProcessor(
110111
nextConsumer consumer.Metrics) (processor.Metrics, error) {
111112

112113
oCfg := cfg.(*Config)
113-
gap := createGroupByAttrsProcessor(set.Logger, oCfg.GroupByKeys)
114+
gap, err := createGroupByAttrsProcessor(set, oCfg.GroupByKeys)
115+
if err != nil {
116+
return nil, err
117+
}
114118

115119
return processorhelper.NewMetricsProcessor(
116120
ctx,

processor/groupbyattrsprocessor/factory_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import (
88
"testing"
99

1010
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
1112
"go.opentelemetry.io/collector/consumer/consumertest"
1213
"go.opentelemetry.io/collector/processor/processortest"
13-
"go.uber.org/zap"
1414
)
1515

1616
func TestDefaultConfiguration(t *testing.T) {
@@ -41,12 +41,14 @@ func TestCreateTestProcessor(t *testing.T) {
4141

4242
func TestNoKeys(t *testing.T) {
4343
// This is allowed since can be used for compacting data
44-
gap := createGroupByAttrsProcessor(zap.NewNop(), []string{})
44+
gap, err := createGroupByAttrsProcessor(processortest.NewNopCreateSettings(), []string{})
45+
require.NoError(t, err)
4546
assert.NotNil(t, gap)
4647
}
4748

4849
func TestDuplicateKeys(t *testing.T) {
49-
gbap := createGroupByAttrsProcessor(zap.NewNop(), []string{"foo", "foo", ""})
50+
gbap, err := createGroupByAttrsProcessor(processortest.NewNopCreateSettings(), []string{"foo", "foo", ""})
51+
require.NoError(t, err)
5052
assert.NotNil(t, gbap)
5153
assert.EqualValues(t, []string{"foo"}, gbap.groupByKeys)
5254
}

processor/groupbyattrsprocessor/metrics.go

Lines changed: 110 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -4,87 +4,123 @@
44
package groupbyattrsprocessor // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/groupbyattrsprocessor"
55

66
import (
7-
"go.opencensus.io/stats"
8-
"go.opencensus.io/stats/view"
7+
"go.opentelemetry.io/collector/component"
98
"go.opentelemetry.io/collector/processor/processorhelper"
9+
"go.opentelemetry.io/otel/metric"
1010

1111
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/groupbyattrsprocessor/internal/metadata"
1212
)
1313

14-
var (
15-
mNumGroupedSpans = stats.Int64("num_grouped_spans", "Number of spans that had attributes grouped", stats.UnitDimensionless)
16-
mNumNonGroupedSpans = stats.Int64("num_non_grouped_spans", "Number of spans that did not have attributes grouped", stats.UnitDimensionless)
17-
mDistSpanGroups = stats.Int64("span_groups", "Distribution of groups extracted for spans", stats.UnitDimensionless)
14+
type internalTelemetry struct {
15+
mNumGroupedSpans metric.Int64Counter
16+
mNumNonGroupedSpans metric.Int64Counter
17+
mDistSpanGroups metric.Int64Histogram
1818

19-
mNumGroupedLogs = stats.Int64("num_grouped_logs", "Number of logs that had attributes grouped", stats.UnitDimensionless)
20-
mNumNonGroupedLogs = stats.Int64("num_non_grouped_logs", "Number of logs that did not have attributes grouped", stats.UnitDimensionless)
21-
mDistLogGroups = stats.Int64("log_groups", "Distribution of groups extracted for logs", stats.UnitDimensionless)
19+
mNumGroupedLogs metric.Int64Counter
20+
mNumNonGroupedLogs metric.Int64Counter
21+
mDistLogGroups metric.Int64Histogram
2222

23-
mNumGroupedMetrics = stats.Int64("num_grouped_metrics", "Number of metrics that had attributes grouped", stats.UnitDimensionless)
24-
mNumNonGroupedMetrics = stats.Int64("num_non_grouped_metrics", "Number of metrics that did not have attributes grouped", stats.UnitDimensionless)
25-
mDistMetricGroups = stats.Int64("metric_groups", "Distribution of groups extracted for metrics", stats.UnitDimensionless)
26-
)
23+
mNumGroupedMetrics metric.Int64Counter
24+
mNumNonGroupedMetrics metric.Int64Counter
25+
mDistMetricGroups metric.Int64Histogram
26+
}
27+
28+
func newProcessorTelemetry(set component.TelemetrySettings) (*internalTelemetry, error) {
29+
it := internalTelemetry{}
30+
31+
counter, err := metadata.Meter(set).Int64Counter(
32+
processorhelper.BuildCustomMetricName(metadata.Type, "num_grouped_spans"),
33+
metric.WithDescription("Number of spans that had attributes grouped"),
34+
metric.WithUnit("1"),
35+
)
36+
if err != nil {
37+
return nil, err
38+
}
39+
it.mNumGroupedSpans = counter
40+
41+
counter, err = metadata.Meter(set).Int64Counter(
42+
processorhelper.BuildCustomMetricName(metadata.Type, "num_non_grouped_spans"),
43+
metric.WithDescription("Number of spans that did not have attributes grouped"),
44+
metric.WithUnit("1"),
45+
)
46+
if err != nil {
47+
return nil, err
48+
}
49+
50+
it.mNumNonGroupedSpans = counter
51+
52+
histo, err := metadata.Meter(set).Int64Histogram(
53+
processorhelper.BuildCustomMetricName(metadata.Type, "span_groups"),
54+
metric.WithDescription("Distribution of groups extracted for spans"),
55+
metric.WithUnit("1"),
56+
)
57+
if err != nil {
58+
return nil, err
59+
}
60+
61+
it.mDistSpanGroups = histo
62+
63+
counter, err = metadata.Meter(set).Int64Counter(
64+
processorhelper.BuildCustomMetricName(metadata.Type, "num_grouped_logs"),
65+
metric.WithDescription("Number of logs that had attributes grouped"),
66+
metric.WithUnit("1"),
67+
)
68+
if err != nil {
69+
return nil, err
70+
}
71+
it.mNumGroupedLogs = counter
72+
73+
counter, err = metadata.Meter(set).Int64Counter(
74+
processorhelper.BuildCustomMetricName(metadata.Type, "num_non_grouped_logs"),
75+
metric.WithDescription("Number of logs that did not have attributes grouped"),
76+
metric.WithUnit("1"),
77+
)
78+
if err != nil {
79+
return nil, err
80+
}
81+
82+
it.mNumNonGroupedLogs = counter
83+
84+
histo, err = metadata.Meter(set).Int64Histogram(
85+
processorhelper.BuildCustomMetricName(metadata.Type, "log_groups"),
86+
metric.WithDescription("Distribution of groups extracted for logs"),
87+
metric.WithUnit("1"),
88+
)
89+
if err != nil {
90+
return nil, err
91+
}
2792

28-
// metricViews return the metrics views according to given telemetry level.
29-
func metricViews() []*view.View {
30-
distributionGroups := view.Distribution(1, 2, 5, 10, 20, 50, 100, 500, 2000)
31-
32-
return []*view.View{
33-
{
34-
Name: processorhelper.BuildCustomMetricName(string(metadata.Type), mNumGroupedSpans.Name()),
35-
Measure: mNumGroupedSpans,
36-
Description: mNumGroupedSpans.Description(),
37-
Aggregation: view.Sum(),
38-
},
39-
{
40-
Name: processorhelper.BuildCustomMetricName(string(metadata.Type), mNumNonGroupedSpans.Name()),
41-
Measure: mNumNonGroupedSpans,
42-
Description: mNumNonGroupedSpans.Description(),
43-
Aggregation: view.Sum(),
44-
},
45-
{
46-
Name: processorhelper.BuildCustomMetricName(string(metadata.Type), mDistSpanGroups.Name()),
47-
Measure: mDistSpanGroups,
48-
Description: mDistSpanGroups.Description(),
49-
Aggregation: distributionGroups,
50-
},
51-
52-
{
53-
Name: processorhelper.BuildCustomMetricName(string(metadata.Type), mNumGroupedLogs.Name()),
54-
Measure: mNumGroupedLogs,
55-
Description: mNumGroupedLogs.Description(),
56-
Aggregation: view.Sum(),
57-
},
58-
{
59-
Name: processorhelper.BuildCustomMetricName(string(metadata.Type), mNumNonGroupedLogs.Name()),
60-
Measure: mNumNonGroupedLogs,
61-
Description: mNumNonGroupedLogs.Description(),
62-
Aggregation: view.Sum(),
63-
},
64-
{
65-
Name: processorhelper.BuildCustomMetricName(string(metadata.Type), mDistLogGroups.Name()),
66-
Measure: mDistLogGroups,
67-
Description: mDistLogGroups.Description(),
68-
Aggregation: distributionGroups,
69-
},
70-
71-
{
72-
Name: processorhelper.BuildCustomMetricName(string(metadata.Type), mNumGroupedMetrics.Name()),
73-
Measure: mNumGroupedMetrics,
74-
Description: mNumGroupedMetrics.Description(),
75-
Aggregation: view.Sum(),
76-
},
77-
{
78-
Name: processorhelper.BuildCustomMetricName(string(metadata.Type), mNumNonGroupedMetrics.Name()),
79-
Measure: mNumNonGroupedMetrics,
80-
Description: mNumNonGroupedMetrics.Description(),
81-
Aggregation: view.Sum(),
82-
},
83-
{
84-
Name: processorhelper.BuildCustomMetricName(string(metadata.Type), mDistMetricGroups.Name()),
85-
Measure: mDistMetricGroups,
86-
Description: mDistMetricGroups.Description(),
87-
Aggregation: distributionGroups,
88-
},
93+
it.mDistLogGroups = histo
94+
95+
counter, err = metadata.Meter(set).Int64Counter(
96+
processorhelper.BuildCustomMetricName(metadata.Type, "num_grouped_metrics"),
97+
metric.WithDescription("Number of metrics that had attributes grouped"),
98+
metric.WithUnit("1"),
99+
)
100+
if err != nil {
101+
return nil, err
102+
}
103+
it.mNumGroupedMetrics = counter
104+
105+
counter, err = metadata.Meter(set).Int64Counter(
106+
processorhelper.BuildCustomMetricName(metadata.Type, "num_non_grouped_metrics"),
107+
metric.WithDescription("Number of metrics that did not have attributes grouped"),
108+
metric.WithUnit("1"),
109+
)
110+
if err != nil {
111+
return nil, err
89112
}
113+
it.mNumNonGroupedMetrics = counter
114+
115+
histo, err = metadata.Meter(set).Int64Histogram(
116+
processorhelper.BuildCustomMetricName(metadata.Type, "metric_groups"),
117+
metric.WithDescription("Distribution of groups extracted for metrics"),
118+
metric.WithUnit("1"),
119+
)
120+
if err != nil {
121+
return nil, err
122+
}
123+
it.mDistMetricGroups = histo
124+
125+
return &it, nil
90126
}

processor/groupbyattrsprocessor/metrics_test.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,3 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
package groupbyattrsprocessor
5-
6-
import (
7-
"testing"
8-
9-
"github.com/stretchr/testify/assert"
10-
)
11-
12-
func TestProcessorMetrics(t *testing.T) {
13-
expectedViewNames := []string{
14-
"processor/groupbyattrs/num_grouped_spans",
15-
"processor/groupbyattrs/num_non_grouped_spans",
16-
"processor/groupbyattrs/span_groups",
17-
"processor/groupbyattrs/num_grouped_logs",
18-
"processor/groupbyattrs/num_non_grouped_logs",
19-
"processor/groupbyattrs/log_groups",
20-
}
21-
22-
views := metricViews()
23-
for i, viewName := range expectedViewNames {
24-
assert.Equal(t, viewName, views[i].Name)
25-
}
26-
}

0 commit comments

Comments
 (0)