Skip to content

Use json omitzero for struct-typed fields in cgroup structs#288

Merged
orestisfl merged 1 commit intoelastic:mainfrom
orestisfl:fix/json-omitzero-struct-fields
Feb 26, 2026
Merged

Use json omitzero for struct-typed fields in cgroup structs#288
orestisfl merged 1 commit intoelastic:mainfrom
orestisfl:fix/json-omitzero-struct-fields

Conversation

@orestisfl
Copy link
Contributor

What does this PR do?

encoding/json's omitempty is silently ignored on struct fields — zero-valued structs are always serialized regardless of the tag. Go 1.24's omitzero fixes this by checking IsZero() or the zero value.

Why is it important?

No downstream production code is affected (consumers use struct tags via go-structform, not json tags), but this corrects a latent bug if these types are ever json.Marshal'd directly.

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • I have added tests that prove my fix is effective or that my feature works

Related issues

encoding/json's omitempty is silently ignored on struct fields —
zero-valued structs are always serialized regardless of the tag.
Go 1.24's omitzero fixes this by checking IsZero() or the zero value.

No downstream production code is affected (consumers use struct tags
via go-structform, not json tags), but this corrects a latent bug
if these types are ever json.Marshal'd directly.
@orestisfl orestisfl requested review from AndersonQ and rdner February 25, 2026 08:17
@orestisfl orestisfl self-assigned this Feb 25, 2026
@orestisfl orestisfl requested a review from a team as a code owner February 25, 2026 08:17
@orestisfl orestisfl added bug Something isn't working Team:Elastic-Agent-Data-Plane Label for the Agent Data Plane team labels Feb 25, 2026
@rdner
Copy link
Member

rdner commented Feb 25, 2026

@orestisfl have you tried to use a local replace in go.mod and run Metricbeat tests with this new version?

@orestisfl
Copy link
Contributor Author

@rdner testded it locally
I also pulled the changes here: elastic/beats#49098, they are bundled with #279 so some zswap-related changes were needed but otherwise passes cleanly.

@orestisfl orestisfl merged commit 4c778fb into elastic:main Feb 26, 2026
5 checks passed
@orestisfl orestisfl deleted the fix/json-omitzero-struct-fields branch February 26, 2026 08:21
orestisfl added a commit to orestisfl/elastic-agent-system-metrics that referenced this pull request Feb 26, 2026
The CFS, UsOpt, and opt.Uint fields added on this branch used
omitempty, which is silently ignored for structs by encoding/json.
Switch to omitzero (Go 1.24) consistent with the fix in elastic#288.

Refactor omitzero_test.go to table-driven style with exact key
equality via assert.Equal instead of Contains/NotContains.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working Team:Elastic-Agent-Data-Plane Label for the Agent Data Plane team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants