Skip to content
Open
Show file tree
Hide file tree
Changes from 8 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/main.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: "enhancement"

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

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "Add support to disable resource attribute"

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

# (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: []
8 changes: 8 additions & 0 deletions service/telemetry/internal/migration/v0.3.0.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ type LogsConfigV030 struct {
// Processors allow configuration of log record processors to emit logs to
// any number of supported backends.
Processors []config.LogRecordProcessor `mapstructure:"processors,omitempty"`

// ResourceAsZapFields indicates whether resource attributes should be added as zap fields.
// If true, resource attributes will be added to logs exported through the LoggerProvider
// and also to logs written to stdout/stderr.
// If false, resource attributes neither be added to logs exported through the LoggerProvider nor
// to the logs written to stdout/stderr.
// (default = true)
ResourceAsZapFields bool `mapstructure:"resource_as_zap_field"`
Copy link
Member

Choose a reason for hiding this comment

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

Not sure where implementation specific things should go. @open-telemetry/collector-approvers

Copy link
Author

Choose a reason for hiding this comment

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

Could you kindly elaborate on this, wasn't able to get it completely.

Copy link
Author

Choose a reason for hiding this comment

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

BTW, if you were saying for this comment - #13869 (comment) ?

}

// LogsSamplingConfig sets a sampling strategy for the logger. Sampling caps the
Expand Down
11 changes: 6 additions & 5 deletions service/telemetry/otelconftelemetry/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@ func createDefaultConfig() component.Config {
Initial: 10,
Thereafter: 100,
},
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
DisableCaller: false,
DisableStacktrace: false,
InitialFields: map[string]any(nil),
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
DisableCaller: false,
DisableStacktrace: false,
InitialFields: map[string]any(nil),
ResourceAsZapFields: true,
},
Metrics: MetricsConfig{
Level: configtelemetry.LevelNormal,
Expand Down
2 changes: 1 addition & 1 deletion service/telemetry/otelconftelemetry/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func createLogger(
// 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.
if res != nil && len(res.Attributes()) > 0 {
if cfg.Logs.ResourceAsZapFields && res != nil && len(res.Attributes()) > 0 {
logger = logger.WithOptions(zap.WrapCore(func(c zapcore.Core) zapcore.Core {
var fields []zap.Field
for _, attr := range res.Attributes() {
Expand Down
90 changes: 83 additions & 7 deletions service/telemetry/otelconftelemetry/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,22 @@ func TestCreateLogger(t *testing.T) {
},
},
},
{
name: "log config with resource_as_zap_field disabled",
cfg: Config{
Logs: LogsConfig{
Level: zapcore.InfoLevel,
Development: false,
Encoding: "console",
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
DisableCaller: false,
DisableStacktrace: false,
InitialFields: map[string]any(nil),
ResourceAsZapFields: false,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -152,6 +168,7 @@ func TestCreateLoggerWithResource(t *testing.T) {
buildInfo component.BuildInfo
resourceConfig map[string]*string
wantFields map[string]string
cfg *Config
}{
{
name: "auto-populated fields only",
Expand All @@ -165,6 +182,14 @@ func TestCreateLoggerWithResource(t *testing.T) {
string(semconv.ServiceVersionKey): "1.0.0",
string(semconv.ServiceInstanceIDKey): "",
},
cfg: &Config{
Logs: LogsConfig{
Level: zapcore.InfoLevel,
Encoding: "json",
ResourceAsZapFields: true,
},
Resource: map[string]*string{},
},
},
{
name: "override service.name",
Expand All @@ -180,6 +205,14 @@ func TestCreateLoggerWithResource(t *testing.T) {
string(semconv.ServiceVersionKey): "1.0.0",
string(semconv.ServiceInstanceIDKey): "",
},
cfg: &Config{
Logs: LogsConfig{
Level: zapcore.InfoLevel,
Encoding: "json",
ResourceAsZapFields: true,
},
Resource: map[string]*string{},
},
},
{
name: "override service.version",
Expand All @@ -195,6 +228,14 @@ func TestCreateLoggerWithResource(t *testing.T) {
string(semconv.ServiceVersionKey): "2.0.0",
string(semconv.ServiceInstanceIDKey): "",
},
cfg: &Config{
Logs: LogsConfig{
Level: zapcore.InfoLevel,
Encoding: "json",
ResourceAsZapFields: true,
},
Resource: map[string]*string{},
},
},
{
name: "custom field with auto-populated",
Expand All @@ -211,6 +252,14 @@ func TestCreateLoggerWithResource(t *testing.T) {
string(semconv.ServiceInstanceIDKey): "", // Just check presence
"custom.field": "custom-value",
},
cfg: &Config{
Logs: LogsConfig{
Level: zapcore.InfoLevel,
Encoding: "json",
ResourceAsZapFields: true,
},
Resource: map[string]*string{},
},
},
{
name: "resource with no attributes",
Expand All @@ -220,6 +269,31 @@ func TestCreateLoggerWithResource(t *testing.T) {
// A random UUID is injected for service.instance.id by default
string(semconv.ServiceInstanceIDKey): "", // Just check presence
},
cfg: &Config{
Logs: LogsConfig{
Level: zapcore.InfoLevel,
Encoding: "json",
ResourceAsZapFields: true,
},
Resource: map[string]*string{},
},
},
{
name: "validate `ResourceAsZapFields=false` shouldn't add resource fields",
buildInfo: component.BuildInfo{
Command: "mycommand",
Version: "1.0.0",
},
resourceConfig: map[string]*string{},
wantFields: map[string]string{},
cfg: &Config{
Logs: LogsConfig{
Level: zapcore.InfoLevel,
Encoding: "json",
ResourceAsZapFields: false,
},
Resource: map[string]*string{},
},
},
}

Expand All @@ -233,12 +307,13 @@ func TestCreateLoggerWithResource(t *testing.T) {
zap.WrapCore(func(zapcore.Core) zapcore.Core { return core }),
},
}
cfg := &Config{
Logs: LogsConfig{
Level: zapcore.InfoLevel,
Encoding: "json",
},
Resource: tt.resourceConfig,

cfg := tt.cfg
if cfg == nil {
cfg = createDefaultConfig().(*Config)
}
if tt.resourceConfig != nil {
cfg.Resource = tt.resourceConfig
}

logger, loggerProvider, err := createLogger(t.Context(), set, cfg)
Expand All @@ -251,7 +326,8 @@ func TestCreateLoggerWithResource(t *testing.T) {
require.Len(t, observedLogs.All(), 1)

entry := observedLogs.All()[0]
if tt.wantFields == nil {
// treat empty map as "no expected fields"
if len(tt.wantFields) == 0 {
assert.Empty(t, entry.Context)
return
}
Expand Down
Loading