Skip to content

fix(metrics_sdk)!: add the attribute_keys filter for view - #2426

Open
xuan-cao-swi wants to merge 4 commits into
open-telemetry:mainfrom
xuan-cao-swi:issues-2359
Open

xuan-cao-swi wants to merge 4 commits into
open-telemetry:mainfrom
xuan-cao-swi:issues-2359

Conversation

@xuan-cao-swi

Copy link
Copy Markdown
Contributor

Description

Resolves #2359

  1. add attributes_key option for views
  2. attributes_key can have three format:
Array: attribute_keys = ['env', 'host'] # this will be treated as allow list -> ['env', 'host']
Hash:  attributes_key = {'a' => true, 'b' => 'c' } # this will be treated as allow list -> ['a', 'b']
Hash:  attributes_key = { included: ['env'], excluded: ['b'] } # this will be treated as two list: allowlist and denylist 

view will filter out the attributes that is for reporting if the attributes key is in denylist or not in allowlist (e.g. view_attributes = view.filter_attributes(attributes))

@mwear mwear left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I left a few comments in-line. Since this changes what attribute_keys does for anyone passing a Hash today (values were merged in, now the keys filter the stream), can you mark the title as breaking, e.g. fix(metrics_sdk)!:, so it's called out in the changelog?

included = normalize_attribute_keys(attribute_keys[:included])
excluded = normalize_attribute_keys(attribute_keys[:excluded])

OpenTelemetry.logger.warn("attribute_keys #{overlap.inspect} are both included and excluded. They will be treated as excluded.") if included && excluded && included.intersect?(excluded)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

overlap isn't defined, so a NameError will be raised if this code path executes.

The README and the add_view docs also say this raises an ArgumentError, but the intent here looks like warn and treat the key as excluded. The spec allows either, so I have a slight preference for raising an ArgumentError, since that's what's already documented. Either way, can you make the code and docs agree and add a test for the overlap case?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Update to use overlap and added the test case for overlap.
I still log the warning because the only concern is that don't want to break user's application, although I am open to raise the error anyway since the next comment/change also raise the error

end

return [nil, nil] if attribute_keys.empty?
return [normalize_attribute_keys(attribute_keys.keys), nil] unless (attribute_keys.keys - INCLUDE_EXCLUDE_KEYS).empty?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If a Hash mixes the two forms, e.g. { included: ['a'], 'b' => nil }, this falls through to the legacy allow-list path and the allow-list becomes ['included', 'b'], so a gets dropped. Similarly, { 'included' => nil }, meant as a legacy allow-list for an attribute named included, becomes no filter at all because of the symbol conversion above. I think raising an ArgumentError when included/excluded are mixed with other keys would be clearer than guessing. What do you think?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, I think simplification make sense. Raising error here but I am open to just log the warning to avoid break user's application (e.g. log the error and don't ignore anything)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

p.s. either log or raise the error for both case

@xuan-cao-swi xuan-cao-swi changed the title fix(metrics_sdk): add the attribute_keys filter for view fix(metrics_sdk)!: add the attribute_keys filter for view Oct 20, 2026

This branch has not been deployed

No deployments
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.

Fix View attribute_keys to behave as an allowlist (with exclude list)

2 participants