Skip to content

Commit 9e9a3da

Browse files
use a factory option to set telemetry attributes
1 parent 5abb4fb commit 9e9a3da

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+362
-310
lines changed

.chloggen/factory_option.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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. otlpreceiver)
7+
component: xconnector, xexporter, xreceiver, xprocessor
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Offer a factory option for components to register modifications to the telemetry attributes
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [13832]
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+
# Optional: The change log or logs in which this entry should be included.
21+
# e.g. '[user]' or '[user, api]'
22+
# Include 'user' if the change is relevant to end users.
23+
# Include 'api' if there is a change to a library API.
24+
# Default: '[user]'
25+
change_logs: [api]

.github/workflows/utils/cspell.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,6 @@
446446
"tailsampling",
447447
"tchannel",
448448
"telemetrygen",
449-
"telemetryimpl",
450449
"testcomponents",
451450
"testconverter",
452451
"testdata",

cmd/builder/internal/builder/main_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ var replaceModules = []string{
9292
"/internal/fanoutconsumer",
9393
"/internal/sharedcomponent",
9494
"/internal/telemetry",
95-
"/internal/telemetryimpl",
9695
"/otelcol",
9796
"/pdata",
9897
"/pdata/testdata",

cmd/mdatagen/go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,5 +201,3 @@ replace go.opentelemetry.io/collector/extension/extensioncapabilities => ../../e
201201
replace go.opentelemetry.io/collector/pdata/xpdata => ../../pdata/xpdata
202202

203203
replace go.opentelemetry.io/collector/exporter/exporterhelper => ../../exporter/exporterhelper
204-
205-
replace go.opentelemetry.io/collector/internal/telemetryimpl => ../../internal/telemetryimpl

cmd/otelcorecol/builder-config.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ replaces:
9292
- go.opentelemetry.io/collector/internal/memorylimiter => ../../internal/memorylimiter
9393
- go.opentelemetry.io/collector/internal/fanoutconsumer => ../../internal/fanoutconsumer
9494
- go.opentelemetry.io/collector/internal/telemetry => ../../internal/telemetry
95-
- go.opentelemetry.io/collector/internal/telemetryimpl => ../../internal/telemetryimpl
9695
- go.opentelemetry.io/collector/internal/sharedcomponent => ../../internal/sharedcomponent
9796
- go.opentelemetry.io/collector/otelcol => ../../otelcol
9897
- go.opentelemetry.io/collector/pdata => ../../pdata

cmd/otelcorecol/go.mod

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ require (
120120
go.opentelemetry.io/collector/internal/memorylimiter v0.135.0 // indirect
121121
go.opentelemetry.io/collector/internal/sharedcomponent v0.135.0 // indirect
122122
go.opentelemetry.io/collector/internal/telemetry v0.135.0 // indirect
123-
go.opentelemetry.io/collector/internal/telemetryimpl v0.135.0 // indirect
124123
go.opentelemetry.io/collector/pdata v1.41.0 // indirect
125124
go.opentelemetry.io/collector/pdata/pprofile v0.135.0 // indirect
126125
go.opentelemetry.io/collector/pdata/testdata v0.135.0 // indirect
@@ -288,8 +287,6 @@ replace go.opentelemetry.io/collector/internal/fanoutconsumer => ../../internal/
288287

289288
replace go.opentelemetry.io/collector/internal/telemetry => ../../internal/telemetry
290289

291-
replace go.opentelemetry.io/collector/internal/telemetryimpl => ../../internal/telemetryimpl
292-
293290
replace go.opentelemetry.io/collector/internal/sharedcomponent => ../../internal/sharedcomponent
294291

295292
replace go.opentelemetry.io/collector/otelcol => ../../otelcol

connector/xconnector/connector.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ package xconnector // import "go.opentelemetry.io/collector/connector/xconnector
66
import (
77
"context"
88

9+
"go.opentelemetry.io/otel/attribute"
10+
911
"go.opentelemetry.io/collector/component"
1012
"go.opentelemetry.io/collector/connector"
1113
"go.opentelemetry.io/collector/connector/internal"
@@ -213,6 +215,13 @@ func WithProfilesToLogs(createProfilesToLogs CreateProfilesToLogsFunc, sl compon
213215
})
214216
}
215217

218+
// WithTelemetryAttributes overrides the default component attributes setter to change the attributes to set on the component telemetry.
219+
func WithTelemetryAttributes(attributesFunc func(set attribute.Set) attribute.Set) FactoryOption {
220+
return factoryOptionFunc(func(o *factoryOpts) {
221+
o.attributesFunc = attributesFunc
222+
})
223+
}
224+
216225
// factory implements the Factory interface.
217226
type factory struct {
218227
connector.Factory
@@ -234,6 +243,8 @@ type factory struct {
234243
profilesToTracesStabilityLevel component.StabilityLevel
235244
profilesToMetricsStabilityLevel component.StabilityLevel
236245
profilesToLogsStabilityLevel component.StabilityLevel
246+
247+
attributesFunc func(set attribute.Set) attribute.Set
237248
}
238249

239250
func (f *factory) TracesToProfilesStability() component.StabilityLevel {
@@ -313,6 +324,13 @@ func (f *factory) CreateProfilesToLogs(ctx context.Context, set connector.Settin
313324
return f.createProfilesToLogsFunc(ctx, set, cfg, next)
314325
}
315326

327+
func (f *factory) TelemetryAttributes(attributes attribute.Set) attribute.Set {
328+
if f.attributesFunc != nil {
329+
return f.attributesFunc(attributes)
330+
}
331+
return attributes
332+
}
333+
316334
// NewFactory returns a Factory.
317335
func NewFactory(cfgType component.Type, createDefaultConfig component.CreateDefaultConfigFunc, options ...FactoryOption) Factory {
318336
opts := factoryOpts{factory: &factory{}}

connector/xconnector/connector_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/stretchr/testify/assert"
1111
"github.com/stretchr/testify/require"
12+
"go.opentelemetry.io/otel/attribute"
1213

1314
"go.opentelemetry.io/collector/component"
1415
"go.opentelemetry.io/collector/connector"
@@ -146,3 +147,27 @@ func createProfilesToMetrics(context.Context, connector.Settings, component.Conf
146147
func createProfilesToLogs(context.Context, connector.Settings, component.Config, consumer.Logs) (Profiles, error) {
147148
return nopInstance, nil
148149
}
150+
151+
func TestWithAttributes(t *testing.T) {
152+
testType := component.MustNewType("test")
153+
defaultCfg := struct{}{}
154+
factory := NewFactory(
155+
testType,
156+
func() component.Config { return &defaultCfg },
157+
WithTelemetryAttributes(func(set attribute.Set) attribute.Set {
158+
return attribute.NewSet(append(set.ToSlice(), attribute.String("foobar", "bar"))...)
159+
}),
160+
)
161+
type factoryWithAttributes interface {
162+
TelemetryAttributes(attributes attribute.Set) attribute.Set
163+
}
164+
result := factory.(factoryWithAttributes).TelemetryAttributes(attribute.NewSet(attribute.String("foo", "bar")))
165+
assert.Equal(t, attribute.NewSet(attribute.String("foobar", "bar"), attribute.String("foo", "bar")), result)
166+
// test without attributes set
167+
factory = NewFactory(
168+
testType,
169+
func() component.Config { return &defaultCfg },
170+
)
171+
result = factory.(factoryWithAttributes).TelemetryAttributes(attribute.NewSet())
172+
assert.Empty(t, result.ToSlice())
173+
}

connector/xconnector/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
go.opentelemetry.io/collector/pdata/testdata v0.135.0
1515
go.opentelemetry.io/collector/pipeline v1.41.0
1616
go.opentelemetry.io/collector/pipeline/xpipeline v0.135.0
17+
go.opentelemetry.io/otel v1.38.0
1718
)
1819

1920
require (
@@ -27,7 +28,6 @@ require (
2728
go.opentelemetry.io/collector/featuregate v1.41.0 // indirect
2829
go.opentelemetry.io/collector/internal/telemetry v0.135.0 // indirect
2930
go.opentelemetry.io/collector/pdata v1.41.0 // indirect
30-
go.opentelemetry.io/otel v1.38.0 // indirect
3131
go.opentelemetry.io/otel/metric v1.38.0 // indirect
3232
go.opentelemetry.io/otel/trace v1.38.0 // indirect
3333
go.uber.org/multierr v1.11.0 // indirect

exporter/xexporter/exporter.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ package xexporter // import "go.opentelemetry.io/collector/exporter/xexporter"
66
import (
77
"context"
88

9+
"go.opentelemetry.io/otel/attribute"
10+
911
"go.opentelemetry.io/collector/component"
1012
"go.opentelemetry.io/collector/consumer/xconsumer"
1113
"go.opentelemetry.io/collector/exporter"
@@ -85,6 +87,7 @@ type factory struct {
8587
exporter.Factory
8688
createProfilesFunc CreateProfilesFunc
8789
profilesStabilityLevel component.StabilityLevel
90+
attributesFunc func(attributes attribute.Set) attribute.Set
8891
}
8992

9093
func (f *factory) ProfilesStability() component.StabilityLevel {
@@ -102,6 +105,20 @@ func (f *factory) CreateProfiles(ctx context.Context, set exporter.Settings, cfg
102105
return f.createProfilesFunc(ctx, set, cfg)
103106
}
104107

108+
// WithTelemetryAttributes overrides the default component attributes setter to change the attributes to set on the component telemetry.
109+
func WithTelemetryAttributes(attributesFunc func(set attribute.Set) attribute.Set) FactoryOption {
110+
return factoryOptionFunc(func(o *factoryOpts) {
111+
o.attributesFunc = attributesFunc
112+
})
113+
}
114+
115+
func (f *factory) TelemetryAttributes(attributes attribute.Set) attribute.Set {
116+
if f.attributesFunc != nil {
117+
return f.attributesFunc(attributes)
118+
}
119+
return attributes
120+
}
121+
105122
// NewFactory returns a Factory.
106123
func NewFactory(cfgType component.Type, createDefaultConfig component.CreateDefaultConfigFunc, options ...FactoryOption) Factory {
107124
opts := factoryOpts{factory: &factory{}}

0 commit comments

Comments
 (0)