Skip to content

Install a no-op monitor state loader in heartbeat receiver when no Elasticsearch client is available - #54846

Merged
swiatekm merged 3 commits into
elastic:mainfrom
swiatekm:swiatekm/hbreceiver-state-loader
Nov 26, 2026
Merged

swiatekm merged 3 commits into
elastic:mainfrom
swiatekm:swiatekm/hbreceiver-state-loader

Conversation

@swiatekm

@swiatekm swiatekm commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

When the Heartbeat OTel receiver gets its monitors over the control protocol, Heartbeat gives them a "deferred" state loader. It blocks every state load until it is replaced, or until a 15s timeout. In the receiver, the only thing that replaces it is the Elasticsearch state loader. If no Elasticsearch client is configured, or one can't be built, nothing opens it. Every monitor then waits the full 15s before its first event goes out.

If there is no Elasticsearch state loader to install, install a no-op one instead. It reports "no prior state" right away, so monitors start immediately.

  • Add Heartbeat.WithoutStateLoader() in heartbeat/beater/heartbeat.go. It atomically replaces the deferred loader with monitorstate.NilStateLoader.
  • In x-pack/heartbeat/hbreceiver/elasticsearch_auth.go, beatsAuthStartHook now calls it on every path that doesn't install an Elasticsearch loader: an empty monitor_state.elasticsearch reference, and a failure to build the ES client (still best-effort, so startup isn't blocked).
  • beatsAuthStartHook takes a small stateLoaderInstaller interface instead of *beater.Heartbeat, so it can be tested with a fake. heartbeatCreator.stateLoaders() returns a true nil interface when no Heartbeat was created, and the hook handles that case.

Proposed commit message

Install a no-op monitor state loader in heartbeat receiver when no Elasticsearch client is available

Heartbeat uses a deferred state loader when monitors arrive over the control protocol. It blocks every state load until it is replaced, or until a 15s timeout. In the OTel receiver, the start hook is the only thing that replaces it, and it did so only when an Elasticsearch client was built. Without one, the first run of each monitor waited out the timeout.

Add Heartbeat.WithoutStateLoader and call it from the receiver start hook on every path that doesn't install an Elasticsearch loader, so monitors start without prior state right away.

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • [ ] I have made corresponding changes to the documentation
  • [ ] I have made corresponding change to the default configuration files
  • I have added tests that prove my fix is effective or that my feature works.
  • [ ] I have added an entry in ./changelog/fragments using the changelog tool.

How to test this PR locally

go test -race -run 'TestHeartbeatWithoutStateLoader' ./heartbeat/beater/...
go test -race ./x-pack/heartbeat/hbreceiver/...

TestReceiverWithControlProtocol now also requires the first monitor event within 10s, well below the 15s deferred timeout.

Related issues

Use cases

  • Receiver configured without monitor_state.elasticsearch: monitors publish their first event immediately.
  • Receiver whose Elasticsearch client can't be built: monitors publish immediately without prior state, and the receiver still starts.

Heartbeat defers every monitor state load when its monitors come over the
control protocol, so that state loads wait for the Elasticsearch client the
output unit used to bring. A host that knows no such client is coming had no
way to say so, and monitors had to wait out the 15s deferral.

Add WithoutStateLoader next to WithElasticsearchStateLoader, so a host can
install the no-op loader and let monitors start with no prior state right away.

Assisted-By: Claude Opus 5 <noreply@anthropic.com>
…sn't coming

A Heartbeat receiver that takes its monitors over the control protocol gets a
state loader that is deferred for 15s. In a Heartbeat process that wait ends
when the output unit arrives and the Elasticsearch client is built from it. The
receiver registers no output reloader on purpose, so the only thing that ends
the wait is the start hook installing the Elasticsearch loader from
monitor_state.elasticsearch. When that setting is absent, as with a
non-Elasticsearch exporter, or the client cannot be built, every monitor state
load blocked until the deferral timed out and the first events were held back
by 15s.

Install the no-op loader on both of those paths, so monitors start with no
prior state instead of waiting. The hook now takes the installer as an
interface, which keeps the nil Heartbeat case explicit in the factory and lets
the tests assert which loader each path installs.

Assisted-By: Claude Opus 5 <noreply@anthropic.com>
TestReceiverWithControlProtocol only waited for events with a 90s budget, so
the 15s monitor state deferral it used to hit was invisible in the test apart
from its runtime. Assert the first event arrives within 10s, which is loose
enough for slow CI and well below the deferral, so that wait coming back fails
the test.

Assisted-By: Claude Opus 5 <noreply@anthropic.com>
@swiatekm swiatekm added bug Team:Elastic-Agent-Data-Plane Label for the Agent Data Plane team backport-9.6 Automated backport to the 9.6 branch labels Nov 20, 2026
@botelastic botelastic Bot added needs_team Indicates that the issue/PR needs a Team:* label and removed needs_team Indicates that the issue/PR needs a Team:* label labels Nov 20, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🤖 GitHub comments

Just comment with:

  • run docs-build : Re-trigger the docs validation. (use unformatted text in the comment!)
  • /test : Run the Buildkite pipeline.

@swiatekm
swiatekm marked this pull request as ready for review November 23, 2026 12:58
@swiatekm
swiatekm requested a review from a team as a code owner November 23, 2026 12:58
@infra-vault-gh-plugin-prod

Copy link
Copy Markdown

Pinging @elastic/elastic-agent-data-plane (Team:Elastic-Agent-Data-Plane)

@mergify

mergify Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

This pull request is merged.

@swiatekm
swiatekm merged commit 5bcc68e into elastic:main Nov 26, 2026
127 of 133 checks passed
@swiatekm
swiatekm deleted the swiatekm/hbreceiver-state-loader branch November 26, 2026 19:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-9.6 Automated backport to the 9.6 branch bug skip-changelog 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