Skip to content

Conversation

@yaten2302
Copy link

Description

This PR adds an option to disable the resource attribute in logs.

Link to tracking issue

Fixes #13869

Testing

Added unit tests in - TestCreateLogger().

@yaten2302 yaten2302 requested a review from a team as a code owner October 20, 2025 18:18
@yaten2302 yaten2302 requested a review from songy23 October 20, 2025 18:18
// 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) ?

@yaten2302 yaten2302 marked this pull request as draft October 21, 2025 06:39
@yaten2302
Copy link
Author

There're some failing tests, working on the fix. By the time, I've converted this to draft 👍

@yaten2302
Copy link
Author

@jade-guiton-dd , wanted to ask here, that currently, TestCreateLoggerWithResource test is failing in logger_test.go.
If I explicitly mention like this:

{
			name: "auto-populated fields only",
			buildInfo: component.BuildInfo{
				Command: "mycommand",
				Version: "1.0.0",
			},
			resourceConfig: map[string]*string{},
			wantFields: map[string]string{
				string(semconv.ServiceNameKey):       "mycommand",
				string(semconv.ServiceVersionKey):    "1.0.0",
				string(semconv.ServiceInstanceIDKey): "",
			},
			cfg: &Config{
				Logs: LogsConfig{
					Level:               zapcore.InfoLevel,
					Encoding:            "json",
					ResourceAsZapFields: true,
				},
				Resource: map[string]*string{},
			},
		},

Then, it'll work fine, but, it's not working if I don't add this field - ResourceAsZapFields. How, can I make it's value as true by default?
I tried using the defaults lib as well - "github.com/mcuadros/go-defaults". But, still the default value isn't being set as true. I'm trying to figure this out, if you've any suggestions/hints for this, kindly share :)

@jade-guiton-dd
Copy link
Contributor

The default configuration for the telemetry factory is in service/telemetry/otelconftelemetry/factory.go, you'll want to modify the createDefaultConfig function to set the new field.

@yaten2302
Copy link
Author

@jade-guiton-dd , I've added the required field in the createDefaultConfig() func in factory.go. But, if we check in the TestCreateLoggerWithResource() func, we're passing our own config (currently that code is commented for testing purpose). So, if we pass our own config, then the default one will be overwritten, right? Because, if I test this func by passing - createDefaultConfig to createLogger func, then the 2 tests are passing and others which are failing are because it's meant to be (because of wantFields).

In short, the as far as I've observed here is that, this - if cfg.Logs.ResourceAsZapFields && res != nil && len(res.Attributes()) > 0 { (which I added) is working fine, but, I'm in a dilemma that if we pass a custom config, then what should be the behaviour? Should it merge the default and the custom config or the default config shouldn't be taken into account (while passing the default one)? Since, the current behaviour in my latest commit is this only. The default config is getting ignored if we pass the custom one, and if we pass the default config (in which we've ResourceAsZapFields as true by default), it's working perfectly fine.

@jade-guiton-dd
Copy link
Contributor

jade-guiton-dd commented Oct 22, 2025

I didn't fully understand your comment, but let me try to explain.

The way defaults are normally handled in the Collector is as follows: 1. the createDefaultConfig function is called, which returns a config struct with default values, 2. the user's YAML config is parsed and merged into this struct, overwriting fields explicitly set by the user and leaving others as-is, 3. the resulting struct is passed to the component.

In tests however, we need to perform this logic manually. If you pass in a config struct to a component, and a field is not specified, that field will default to its zero value, as is normal in Go; the value of that field in createDefaultConfig doesn't matter.

This means that the existing test cases in TestCreateLogger already have ResourceAsZapFields set to false. In that regard, I think it would make more sense for the new test case you added to set ResourceAsZapFields: true instead, to ensure that it doesn't cause issues for logger creation.

As for TestCreateLoggerWithResource: since that test is specifically checking the behavior that ResourceAsZapFields is toggling, it makes sense to hardcode ResourceAsZapFields: true in the config that it uses.

@yaten2302
Copy link
Author

Got this, thanks for explaining @jade-guiton-dd :)
I've pushed a commit for this, and the test TestCreateLoggerWithResource is passing now

@codecov
Copy link

codecov bot commented Oct 24, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.27%. Comparing base (507664f) to head (f320dcd).

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #14050   +/-   ##
=======================================
  Coverage   92.27%   92.27%           
=======================================
  Files         657      657           
  Lines       41093    41094    +1     
=======================================
+ Hits        37917    37918    +1     
  Misses       2174     2174           
  Partials     1002     1002           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@yaten2302 yaten2302 marked this pull request as ready for review October 25, 2025 14:20
@yaten2302
Copy link
Author

I think there're some tests failing because of my latest commit, checking those 👀

@jade-guiton-dd
Copy link
Contributor

The contrib-tests are failing for everyone, it's not related to your PR

@yaten2302
Copy link
Author

Ok, got this 👍
Should I update the changelog file and update the component name to this - pkg/service?
@jade-guiton-dd

@jade-guiton-dd
Copy link
Contributor

Yes, I think that's the best option.

Comment on lines +264 to +280
var cfg *Config
if tt.cfg == nil {
cfg = &Config{
Logs: LogsConfig{
Level: zapcore.InfoLevel,
Encoding: "json",
DisableResourceAttributes: false,
},
Resource: tt.resourceConfig,
}
} else {
cfg = tt.cfg
cfg.Logs.DisableResourceAttributes = true
}

if tt.resourceConfig != nil {
cfg.Resource = tt.resourceConfig
Copy link
Contributor

@jade-guiton-dd jade-guiton-dd Oct 29, 2025

Choose a reason for hiding this comment

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

I would suggest something like:

Suggested change
var cfg *Config
if tt.cfg == nil {
cfg = &Config{
Logs: LogsConfig{
Level: zapcore.InfoLevel,
Encoding: "json",
DisableResourceAttributes: false,
},
Resource: tt.resourceConfig,
}
} else {
cfg = tt.cfg
cfg.Logs.DisableResourceAttributes = true
}
if tt.resourceConfig != nil {
cfg.Resource = tt.resourceConfig
cfg := &Config{
Logs: LogsConfig{
Level: zapcore.InfoLevel,
Encoding: "json",
},
Resource: tt.resourceConfig,
}
if tt.setConfig != nil {
tt.setConfig(cfg)

and then in the test case have:

setConfig: func(cfg *Config) {
	cfg.Logs.DisableResourceAttributes = true
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unnecessary 132B in every log line

4 participants