Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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/fix-13014.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: bug_fix

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

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Allocate less memory per component when OTLP exporting of logs is disabled

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

# (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: [user]
2 changes: 1 addition & 1 deletion internal/telemetry/componentattribute/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func TestCore(t *testing.T) {
createLogger: func() (*zap.Logger, logRecorder) {
core, observed := createZapCore()
recorder := logtest.NewRecorder()
core = componentattribute.NewOTelTeeCoreWithAttributes(core, recorder, "testinstr", zap.DebugLevel, attribute.NewSet())
core = componentattribute.NewOTelTeeCoreWithAttributes(core, recorder, "testinstr", zap.DebugLevel, attribute.NewSet(), func(c zapcore.Core) zapcore.Core { return c })
return zap.New(core), logRecorder{zapLogs: observed, otelLogs: recorder}
},
check: func(t *testing.T, rec logRecorder) {
Expand Down
37 changes: 5 additions & 32 deletions internal/telemetry/componentattribute/logger_zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ type otelTeeCoreWithAttributes struct {
lp log.LoggerProvider
scopeName string
level zapcore.Level
wrapper func(zapcore.Core) zapcore.Core
}

var _ coreWithAttributes = (*otelTeeCoreWithAttributes)(nil)
Expand All @@ -84,7 +85,7 @@ var _ coreWithAttributes = (*otelTeeCoreWithAttributes)(nil)
// logs, component attributes are injected as instrumentation scope attributes.
//
// This is used when service::telemetry::logs::processors is configured.
func NewOTelTeeCoreWithAttributes(consoleCore zapcore.Core, lp log.LoggerProvider, scopeName string, level zapcore.Level, attrs attribute.Set) zapcore.Core {
func NewOTelTeeCoreWithAttributes(consoleCore zapcore.Core, lp log.LoggerProvider, scopeName string, level zapcore.Level, attrs attribute.Set, wrapper func(zapcore.Core) zapcore.Core) zapcore.Core {
// TODO: Use `otelzap.WithAttributes` and remove `LoggerProviderWithAttributes`
// once we've upgraded to otelzap v0.11.0.
lpwa := LoggerProviderWithAttributes(lp, attrs)
Expand All @@ -97,45 +98,17 @@ func NewOTelTeeCoreWithAttributes(consoleCore zapcore.Core, lp log.LoggerProvide
}

return &otelTeeCoreWithAttributes{
Core: zapcore.NewTee(consoleCore, otelCore),
Core: zapcore.NewTee(consoleCore, wrapper(otelCore)),
consoleCore: consoleCore,
lp: lp,
scopeName: scopeName,
level: level,
wrapper: wrapper,
}
}

func (ocwa *otelTeeCoreWithAttributes) withAttributeSet(attrs attribute.Set) zapcore.Core {
return NewOTelTeeCoreWithAttributes(
tryWithAttributeSet(ocwa.consoleCore, attrs),
ocwa.lp, ocwa.scopeName, ocwa.level,
attrs,
)
}

type wrapperCoreWithAttributes struct {
zapcore.Core
from zapcore.Core
wrapper func(zapcore.Core) zapcore.Core
}

var _ coreWithAttributes = (*wrapperCoreWithAttributes)(nil)

// NewWrapperCoreWithAttributes applies a wrapper function to a core, similar to [zap.WrapCore]. The resulting wrapped core
// allows setting component attributes on the inner core and reapplying the wrapper function when
// needed.
//
// This is used when adding [zapcore.NewSamplerWithOptions] to our logger stack.
func NewWrapperCoreWithAttributes(from zapcore.Core, wrapper func(zapcore.Core) zapcore.Core) zapcore.Core {
return &wrapperCoreWithAttributes{
Core: wrapper(from),
from: from,
wrapper: wrapper,
}
}

func (wcwa *wrapperCoreWithAttributes) withAttributeSet(attrs attribute.Set) zapcore.Core {
return NewWrapperCoreWithAttributes(tryWithAttributeSet(wcwa.from, attrs), wcwa.wrapper)
return NewOTelTeeCoreWithAttributes(tryWithAttributeSet(ocwa.consoleCore, attrs), ocwa.lp, ocwa.scopeName, ocwa.level, attrs, ocwa.wrapper)
}

// ZapLoggerWithAttributes creates a Zap Logger with a new set of injected component attributes.
Expand Down
22 changes: 14 additions & 8 deletions service/telemetry/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
// We do NOT add them to the logger using With, because that would apply to all logs, even ones exported through the core that wraps
// the LoggerProvider, meaning that the attributes would be exported twice.
logger = logger.WithOptions(zap.WrapCore(func(c zapcore.Core) zapcore.Core {
fields := []zap.Field{}
var fields []zap.Field
for k, v := range cfg.Resource {
if v != nil {
f := zap.Stringp(k, v)
Expand All @@ -56,28 +56,34 @@
}))

var lp log.LoggerProvider

logger = logger.WithOptions(zap.WrapCore(func(core zapcore.Core) zapcore.Core {
if cfg.Logs.Sampling != nil && cfg.Logs.Sampling.Enabled {
core = newSampledCore(core, cfg.Logs.Sampling)
}

core = componentattribute.NewConsoleCoreWithAttributes(core, attribute.NewSet())

if len(cfg.Logs.Processors) > 0 && set.SDK != nil {
lp = set.SDK.LoggerProvider()
wrapper := func(c zapcore.Core) zapcore.Core {
return c
}
if cfg.Logs.Sampling != nil && cfg.Logs.Sampling.Enabled {
wrapper = func(c zapcore.Core) zapcore.Core {
return newSampledCore(c, cfg.Logs.Sampling)
}

Check warning on line 74 in service/telemetry/logger.go

View check run for this annotation

Codecov / codecov/patch

service/telemetry/logger.go#L72-L74

Added lines #L72 - L74 were not covered by tests
}

core = componentattribute.NewOTelTeeCoreWithAttributes(
core,
lp,
"go.opentelemetry.io/collector/service/telemetry",
cfg.Logs.Level,
attribute.NewSet(),
wrapper,
)
}

if cfg.Logs.Sampling != nil && cfg.Logs.Sampling.Enabled {
core = componentattribute.NewWrapperCoreWithAttributes(core, func(c zapcore.Core) zapcore.Core {
return newSampledCore(c, cfg.Logs.Sampling)
})
}

return core
}))

Expand Down
2 changes: 1 addition & 1 deletion service/telemetry/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func TestNewLogger(t *testing.T) {
InitialFields: map[string]any(nil),
},
},
wantCoreType: "*componentattribute.wrapperCoreWithAttributes",
wantCoreType: "*componentattribute.consoleCoreWithAttributes",
},
}
for _, tt := range tests {
Expand Down
Loading