Skip to content

Add immutable noop Observation.Context#7133

Merged
jonatan-ivanov merged 1 commit intomicrometer-metrics:1.15.xfrom
jonatan-ivanov:empty-context
Jan 29, 2026
Merged

Add immutable noop Observation.Context#7133
jonatan-ivanov merged 1 commit intomicrometer-metrics:1.15.xfrom
jonatan-ivanov:empty-context

Conversation

@jonatan-ivanov
Copy link
Copy Markdown
Member

@jonatan-ivanov jonatan-ivanov commented Jan 28, 2026

Before this change the Observation.Context instance used for noop observations were mutable.
This can be a source of memory leaks if the user gets a noop Observation and writes the context directly:

Observation observation = Observation.start("test", ObservationRegistry.NOOP);
assertThat(observation.getContext()).isSameAs(Observation.Context.EMPTY);
observation.getContext().put("key", "value"); // this should not do anything
assertThat(observation.getContext().<String>get("key")).isEqualTo("value"); // this should fail

This change introduces an immutable and noop Observation.Context implementation.

See #7105 (comment)

Comment thread micrometer-observation/src/main/java/io/micrometer/observation/Observation.java Outdated
Copy link
Copy Markdown
Member

@shakuzen shakuzen left a comment

Choose a reason for hiding this comment

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

Thanks for this. We should do this in the maintenance branches, right?

Comment thread micrometer-observation/src/main/java/io/micrometer/observation/EmptyContext.java Outdated
Comment thread micrometer-observation/src/main/java/io/micrometer/observation/EmptyContext.java Outdated
@jonatan-ivanov
Copy link
Copy Markdown
Member Author

Thanks for this. We should do this in the maintenance branches, right?

When I started I was not sure but looking at how it ended up, I think so, I moved to 1.15.x.

@jonatan-ivanov jonatan-ivanov changed the base branch from main to 1.15.x January 28, 2026 20:48
@jonatan-ivanov jonatan-ivanov changed the title Add immutable empty Observation.Context Add immutable noop Observation.Context Jan 28, 2026
Before this change, the Observation.Context instance used for noop
observations were mutable. This can be a source of memory leaks if
the user gets a noop Observation and writes the context directly.
This change introduces an immutable noop implementation.

See micrometer-metrics#7105 (comment)
@jonatan-ivanov jonatan-ivanov added bug A general bug and removed enhancement A general enhancement labels Jan 28, 2026
@jonatan-ivanov jonatan-ivanov modified the milestones: 1.17.0-M2, 1.15.9 Jan 28, 2026
@jonatan-ivanov
Copy link
Copy Markdown
Member Author

Forward merge will be a bit more adventurous, let me know if it looks ok and I will merge it.

@jonatan-ivanov jonatan-ivanov merged commit fd34e78 into micrometer-metrics:1.15.x Jan 29, 2026
9 checks passed
@jonatan-ivanov jonatan-ivanov deleted the empty-context branch January 29, 2026 18:49
inshining pushed a commit to inshining/micrometer that referenced this pull request Jan 30, 2026
Before this change, the Observation.Context instances used for noop
observations were mutable. This can be a source of memory leaks if
the user gets a noop Observation and writes the context directly.
This change introduces an immutable noop implementation.

See micrometer-metrics#7105 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants