diff --git a/CHANGELOG.md b/CHANGELOG.md index 89017dffdd1..7c342c6914c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Configuration file can now be set via `OTEL_CONFIG_FILE` in `go.opentelemetry.io/contrib/otelconf`. (#8639) - Added support for `service` resource detector in `go.opentelemetry.io/contrib/otelconf`. (#8674) +- Added support for `attribute_count_limit` and `attribute_value_length_limit` in logger provider configuration in `go.opentelemetry.io/contrib/otelconf`. (#8686) ### Changed diff --git a/otelconf/log.go b/otelconf/log.go index 24400a14194..84fa68beca6 100644 --- a/otelconf/log.go +++ b/otelconf/log.go @@ -42,10 +42,23 @@ func loggerProvider(cfg configOptions, res *resource.Resource) (log.LoggerProvid return noop.NewLoggerProvider(), noopShutdown, errors.Join(errs...) } + if cfg.opentelemetryConfig.LoggerProvider.Limits != nil { + opts = logProcessorLimits(opts, *cfg.opentelemetryConfig.LoggerProvider.Limits) + } lp := sdklog.NewLoggerProvider(opts...) return lp, lp.Shutdown, nil } +func logProcessorLimits(opts []sdklog.LoggerProviderOption, limits LogRecordLimits) []sdklog.LoggerProviderOption { + if limits.AttributeCountLimit != nil { + opts = append(opts, sdklog.WithAttributeCountLimit(*limits.AttributeCountLimit)) + } + if limits.AttributeValueLengthLimit != nil { + opts = append(opts, sdklog.WithAttributeValueLengthLimit(*limits.AttributeValueLengthLimit)) + } + return opts +} + func logProcessor(ctx context.Context, processor LogRecordProcessor) (sdklog.Processor, error) { if processor.Batch != nil && processor.Simple != nil { return nil, newErrInvalid("must not specify multiple log processor type") diff --git a/otelconf/log_test.go b/otelconf/log_test.go index e32a2d9825e..bdbcc75ac61 100644 --- a/otelconf/log_test.go +++ b/otelconf/log_test.go @@ -63,11 +63,29 @@ func TestLoggerProvider(t *testing.T) { wantProvider: noop.NewLoggerProvider(), wantErr: newErrInvalid("must not specify multiple log processor type"), }, + { + name: "with-limits", + cfg: configOptions{ + opentelemetryConfig: OpenTelemetryConfiguration{ + LoggerProvider: &LoggerProvider{ + Limits: &LogRecordLimits{ + AttributeCountLimit: ptr(50), + AttributeValueLengthLimit: ptr(100), + }, + }, + }, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - mp, shutdown, err := loggerProvider(tt.cfg, resource.Default()) - require.Equal(t, tt.wantProvider, mp) + lp, shutdown, err := loggerProvider(tt.cfg, resource.Default()) + if tt.wantProvider != nil { + require.Equal(t, tt.wantProvider, lp) + } else { + require.NotNil(t, lp) + require.IsType(t, &sdklog.LoggerProvider{}, lp) + } assert.ErrorIs(t, err, tt.wantErr) require.NoError(t, shutdown(t.Context())) }) @@ -736,6 +754,51 @@ func TestLoggerProviderOptions(t *testing.T) { assert.NotContains(t, buf.String(), "foo") } +func TestLogProcessorLimits(t *testing.T) { + tests := []struct { + name string + limits LogRecordLimits + wantAdditionalOpts int + }{ + { + name: "no-limits", + limits: LogRecordLimits{}, + wantAdditionalOpts: 0, + }, + { + name: "attribute-count-limit-only", + limits: LogRecordLimits{ + AttributeCountLimit: ptr(50), + }, + wantAdditionalOpts: 1, + }, + { + name: "attribute-value-length-limit-only", + limits: LogRecordLimits{ + AttributeValueLengthLimit: ptr(100), + }, + wantAdditionalOpts: 1, + }, + { + name: "both-limits", + limits: LogRecordLimits{ + AttributeCountLimit: ptr(50), + AttributeValueLengthLimit: ptr(100), + }, + wantAdditionalOpts: 2, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + initialOpts := []sdklog.LoggerProviderOption{} + result := logProcessorLimits(initialOpts, tt.limits) + + assert.Len(t, result, tt.wantAdditionalOpts, "unexpected number of options returned") + }) + } +} + func Test_otlpGRPCLogExporter(t *testing.T) { if runtime.GOOS == "windows" || runtime.GOOS == "darwin" { // TODO (#8115): Fix the flakiness on Windows and MacOS.