Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 25 additions & 0 deletions .chloggen/service-settings-telemetryfactory.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: breaking
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note to reviewer: I've put this down as a breaking API change because the new setting is required.


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

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: The `service.Settings` type now requires a `telemetry.Factory` to be provided

# One or more tracking issues or pull requests related to the change
issues: [4970]

# (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:

# 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: [api]
3 changes: 2 additions & 1 deletion internal/e2e/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,12 @@ func Test_ComponentStatusReporting_SharedInstance(t *testing.T) {
ExtensionsFactories: map[component.Type]extension.Factory{
component.MustNewType("watcher"): newExtensionFactory(),
},
TelemetryFactory: otelconftelemetry.NewFactory(),
}
set.BuildInfo = component.BuildInfo{Version: "test version", Command: "otelcoltest"}

cfg := service.Config{
Telemetry: otelconftelemetry.Config{
Telemetry: &otelconftelemetry.Config{
Logs: otelconftelemetry.LogsConfig{
Level: zapcore.InfoLevel,
Development: false,
Expand Down
5 changes: 5 additions & 0 deletions otelcol/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"go.opentelemetry.io/collector/confmap/xconfmap"
"go.opentelemetry.io/collector/otelcol/internal/grpclog"
"go.opentelemetry.io/collector/service"
"go.opentelemetry.io/collector/service/telemetry/otelconftelemetry"
)

// State defines Collector's state.
Expand Down Expand Up @@ -216,6 +217,10 @@ func (col *Collector) setupConfigurationComponents(ctx context.Context) error {
},
AsyncErrorChannel: col.asyncErrorChannel,
LoggingOptions: col.set.LoggingOptions,

// TODO: inject the telemetry factory through factories.
// See https://github.com/open-telemetry/opentelemetry-collector/issues/4970
TelemetryFactory: otelconftelemetry.NewFactory(),
}, cfg.Service)
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion otelcol/collector_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"go.opentelemetry.io/collector/featuregate"
"go.opentelemetry.io/collector/service"
"go.opentelemetry.io/collector/service/telemetry/otelconftelemetry"
)

type windowsService struct {
Expand Down Expand Up @@ -214,7 +215,7 @@ func (w windowsEventLogCore) Sync() error {
func withWindowsCore(elog *eventlog.Log, serviceConfig **service.Config) func(zapcore.Core) zapcore.Core {
return func(core zapcore.Core) zapcore.Core {
if serviceConfig != nil && *serviceConfig != nil {
for _, output := range (*serviceConfig).Telemetry.Logs.OutputPaths {
for _, output := range (*serviceConfig).Telemetry.(*otelconftelemetry.Config).Logs.OutputPaths {
if output != "stdout" && output != "stderr" {
// A log file was specified in the configuration, so we should not use the Windows Event Log
return core
Expand Down
24 changes: 22 additions & 2 deletions otelcol/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ func TestConfigValidate(t *testing.T) {
expected: nil,
},
{
name: "custom-service-telemetrySettings-encoding",
name: "valid-telemetry-config",
cfgFn: func() *Config {
cfg := generateConfig()
cfg.Service.Telemetry.Logs.Encoding = "json"
cfg.Service.Telemetry = fakeTelemetryConfig{}
return cfg
},
expected: nil,
Expand Down Expand Up @@ -89,6 +89,15 @@ func TestConfigValidate(t *testing.T) {
},
expected: errMissingReceivers,
},
{
name: "invalid-telemetry-config",
cfgFn: func() *Config {
cfg := generateConfig()
cfg.Service.Telemetry = fakeTelemetryConfig{Invalid: true}
return cfg
},
expected: errors.New("service::telemetry: invalid config"),
},
{
name: "invalid-extension-reference",
cfgFn: func() *Config {
Expand Down Expand Up @@ -330,3 +339,14 @@ func generateConfig() *Config {
func newPtr[T int | string](str T) *T {
return &str
}

type fakeTelemetryConfig struct {
Invalid bool `mapstructure:"invalid"`
}

func (cfg fakeTelemetryConfig) Validate() error {
if cfg.Invalid {
return errors.New("invalid config")
}
return nil
}
2 changes: 1 addition & 1 deletion otelcol/unmarshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func unmarshal(v *confmap.Conf, factories Factories) (*configSettings, error) {
// TODO: inject the telemetry factory through factories, once available.
// See https://github.com/open-telemetry/opentelemetry-collector/issues/4970
telFactory := otelconftelemetry.NewFactory()
defaultTelConfig := *telFactory.CreateDefaultConfig().(*otelconftelemetry.Config)
defaultTelConfig := telFactory.CreateDefaultConfig().(*otelconftelemetry.Config)

// Unmarshal top level sections and validate.
cfg := &configSettings{
Expand Down
25 changes: 7 additions & 18 deletions otelcol/unmarshaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestUnmarshalEmptyAllSections(t *testing.T) {
OutputPaths: zapProdCfg.OutputPaths,
ErrorOutputPaths: zapProdCfg.ErrorOutputPaths,
InitialFields: zapProdCfg.InitialFields,
}, cfg.Service.Telemetry.Logs)
}, cfg.Service.Telemetry.(*otelconftelemetry.Config).Logs)
}

func TestUnmarshalUnknownTopLevel(t *testing.T) {
Expand Down Expand Up @@ -149,26 +149,13 @@ func TestServiceUnmarshalError(t *testing.T) {
expectError string
}{
{
name: "invalid-logs-level",
name: "invalid-telemetry-unknown-key",
conf: confmap.NewFromStringMap(map[string]any{
"telemetry": map[string]any{
"logs": map[string]any{
"level": "UNKNOWN",
},
},
}),
expectError: "decoding failed due to the following error(s):\n\n'telemetry.logs' decoding failed due to the following error(s):\n\n'level' unrecognized level: \"UNKNOWN\"",
},
{
name: "invalid-metrics-level",
conf: confmap.NewFromStringMap(map[string]any{
"telemetry": map[string]any{
"metrics": map[string]any{
"level": "unknown",
},
"unknown": "key",
},
}),
expectError: "decoding failed due to the following error(s):\n\n'telemetry.metrics' decoding failed due to the following error(s):\n\n'level' unknown metrics level \"unknown\"",
expectError: "decoding failed due to the following error(s):\n\n'telemetry' has invalid keys: unknown",
},
{
name: "invalid-service-extensions-section",
Expand Down Expand Up @@ -201,7 +188,9 @@ func TestServiceUnmarshalError(t *testing.T) {

for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
err := tt.conf.Unmarshal(&service.Config{})
err := tt.conf.Unmarshal(&service.Config{
Telemetry: fakeTelemetryConfig{},
})
require.ErrorContains(t, err, tt.expectError)
})
}
Expand Down
4 changes: 2 additions & 2 deletions service/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
package service // import "go.opentelemetry.io/collector/service"

import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/service/extensions"
"go.opentelemetry.io/collector/service/pipelines"
"go.opentelemetry.io/collector/service/telemetry/otelconftelemetry"
)

// Config defines the configurable components of the Service.
type Config struct {
// Telemetry is the configuration for collector's own telemetry.
Telemetry otelconftelemetry.Config `mapstructure:"telemetry"`
Telemetry component.Config `mapstructure:"telemetry"`

// Extensions are the ordered list of extensions configured for the service.
Extensions extensions.Config `mapstructure:"extensions,omitempty"`
Expand Down
92 changes: 18 additions & 74 deletions service/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,16 @@ package service
import (
"errors"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
config "go.opentelemetry.io/contrib/otelconf/v0.3.0"
"go.uber.org/zap/zapcore"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configtelemetry"
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/confmap/xconfmap"
"go.opentelemetry.io/collector/pipeline"
"go.opentelemetry.io/collector/service/extensions"
"go.opentelemetry.io/collector/service/pipelines"
"go.opentelemetry.io/collector/service/telemetry/otelconftelemetry"
)

func TestConfigValidate(t *testing.T) {
Expand All @@ -35,10 +30,10 @@ func TestConfigValidate(t *testing.T) {
expected: nil,
},
{
name: "custom-service-telemetrySettings-encoding",
name: "valid-telemetry-config",
cfgFn: func() *Config {
cfg := generateConfig()
cfg.Telemetry.Logs.Encoding = "json"
cfg.Telemetry = fakeTelemetryConfig{Invalid: false}
return cfg
},
expected: nil,
Expand All @@ -54,14 +49,13 @@ func TestConfigValidate(t *testing.T) {
expected: errors.New(`references processor "nop" multiple times`),
},
{
name: "invalid-telemetry-metric-config",
name: "invalid-telemetry-config",
cfgFn: func() *Config {
cfg := generateConfig()
cfg.Telemetry.Metrics.Level = configtelemetry.LevelBasic
cfg.Telemetry.Metrics.Readers = nil
cfg.Telemetry = fakeTelemetryConfig{Invalid: true}
return cfg
},
expected: errors.New("collector telemetry metrics reader should exist when metric level is not none"),
expected: errors.New("telemetry: invalid config"),
},
}

Expand All @@ -79,80 +73,19 @@ func TestConfigValidate(t *testing.T) {
}

func TestConfmapMarshalConfig(t *testing.T) {
telFactory := otelconftelemetry.NewFactory()
defaultTelConfig := *telFactory.CreateDefaultConfig().(*otelconftelemetry.Config)
conf := confmap.New()

require.NoError(t, conf.Marshal(Config{
Telemetry: defaultTelConfig,
Telemetry: fakeTelemetryConfig{},
}))
assert.Equal(t, map[string]any{
"pipelines": map[string]any(nil),
"telemetry": map[string]any{
"logs": map[string]any{
"encoding": "console",
"level": "info",
"error_output_paths": []any{"stderr"},
"output_paths": []any{"stderr"},
"sampling": map[string]any{
"enabled": true,
"initial": 10,
"thereafter": 100,
"tick": 10 * time.Second,
},
},
"metrics": map[string]any{
"level": "Normal",
"readers": []any{
map[string]any{
"pull": map[string]any{
"exporter": map[string]any{
"prometheus": map[string]any{
"host": "localhost",
"port": 8888,
"with_resource_constant_labels": map[string]any{
"included": []any{},
},
"without_scope_info": true,
"without_type_suffix": true,
"without_units": true,
},
},
},
},
},
},
},
"telemetry": map[string]any{"invalid": false},
}, conf.ToStringMap())
}

func generateConfig() *Config {
return &Config{
Telemetry: otelconftelemetry.Config{
Logs: otelconftelemetry.LogsConfig{
Level: zapcore.DebugLevel,
Development: true,
Encoding: "console",
DisableCaller: true,
DisableStacktrace: true,
OutputPaths: []string{"stderr", "./output-logs"},
ErrorOutputPaths: []string{"stderr", "./error-output-logs"},
InitialFields: map[string]any{"fieldKey": "filed-value"},
},
Metrics: otelconftelemetry.MetricsConfig{
Level: configtelemetry.LevelNormal,
MeterProvider: config.MeterProvider{
Readers: []config.MetricReader{
{
Pull: &config.PullMetricReader{Exporter: config.PullMetricExporter{Prometheus: &config.Prometheus{
Host: newPtr("localhost"),
Port: newPtr(8080),
}}},
},
},
},
},
},
Extensions: extensions.Config{component.MustNewID("nop")},
Pipelines: pipelines.Config{
pipeline.NewID(pipeline.SignalTraces): {
Expand All @@ -163,3 +96,14 @@ func generateConfig() *Config {
},
}
}

type fakeTelemetryConfig struct {
Invalid bool `mapstructure:"invalid"`
}

func (cfg fakeTelemetryConfig) Validate() error {
if cfg.Invalid {
return errors.New("invalid config")
}
return nil
}
2 changes: 2 additions & 0 deletions service/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ require (
go.opentelemetry.io/contrib/propagators/b3 v1.36.0
go.opentelemetry.io/otel v1.38.0
go.opentelemetry.io/otel/log v0.14.0
go.opentelemetry.io/otel/log/logtest v0.14.0
go.opentelemetry.io/otel/metric v1.38.0
go.opentelemetry.io/otel/sdk v1.38.0
go.opentelemetry.io/otel/sdk/metric v1.38.0
Expand All @@ -77,6 +78,7 @@ require (
github.com/gobwas/glob v0.2.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/snappy v1.0.0 // indirect
github.com/google/go-cmp v0.7.0 // indirect
github.com/google/go-tpm v0.9.5 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.27.1 // indirect
github.com/hashicorp/go-version v1.7.0 // indirect
Expand Down
Loading