Skip to content

[BUG] Decide Elasticsearch bulk export success from HTTP status and the errors flag - #4297

Open
thc1006 wants to merge 12 commits into
open-telemetry:mainfrom
thc1006:bugfix/elasticsearch-response-handling
Open

thc1006 wants to merge 12 commits into
open-telemetry:mainfrom
thc1006:bugfix/elasticsearch-response-handling

Conversation

@thc1006

@thc1006 thc1006 commented Jul 24, 2026 •

Copy link
Copy Markdown
Member

Fixes #4295.

Both export paths decided bulk success by searching the response body for "failed" : 0, without reading the HTTP status. A batch with a rejected item could therefore read as success, a compact successful response as failure, and an HTTP 500 as success when its body contained that exact marker. Both export paths now pass the HTTP status, response body and number of submitted records to the same parser.

This exporter sends unfiltered index operations. A successful response has HTTP status 200, a Boolean errors field set to false and one acknowledgement per submitted operation. Each item must contain one index result with a string _index, integer status 200 or 201, and no non-null error. An alias may resolve to a different concrete index name. The response and item status predicates are distinct.

The parser is private to the exporter. Its detail header is excluded from the CMake install and has package-private Bazel visibility. #4327 previously excluded the HTTP client's internal detail headers from the CMake install as well. This adds the repository's first Bazel implementation_deps. If you would rather list the header in the exporter's hdrs, or move the parser into src/, I can change it. The bulk request URI also drops ?pretty, since the parser does not depend on pretty-printed whitespace.

At 6406682 a standalone test of the actual helper checked 30,000 combinations of HTTP status, item status, errors and submitted count. It found zero mismatches with exceptions enabled and zero in a second 30,000-case run with exceptions disabled. The CMake Install Tests run on this head stopped during the build when its runner shut down (exit 143). The separate CI workflow passed.

thc1006 added a commit to thc1006/opentelemetry-cpp that referenced this pull request Jul 24, 2026
@codecov

codecov Bot commented Jul 24, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.14286% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.67%. Comparing base (c7ad5c9) to head (6406682).

Files with missing lines Patch % Lines
.../exporters/elasticsearch/detail/es_bulk_response.h 96.83% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #4297      +/-   ##
==========================================
+ Coverage   86.63%   86.67%   +0.05%     
==========================================
  Files         530      531       +1     
  Lines       21183    21248      +65     
==========================================
+ Hits        18349    18415      +66     
+ Misses       2834     2833       -1     
Files with missing lines Coverage Δ
...orters/elasticsearch/src/es_log_record_exporter.cc 50.75% <100.00%> (+3.02%) ⬆️
.../exporters/elasticsearch/detail/es_bulk_response.h 96.83% <96.83%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

thc1006 added a commit to thc1006/opentelemetry-cpp that referenced this pull request Jul 25, 2026
Review of open-telemetry#4297 found the synchronous path reported a non-2xx response
as a success: ResponseHandler::OnResponse only logged the status and
unconditionally set response_received_, waitForResponse returned true,
and Export then checked the body alone, so HTTP 500 with a body of
{"errors":false} became kSuccess. My description claiming the sync path
already checked the status was wrong: it observed the status but never
let it affect the ExportResult.

IsBulkResponseSuccessful now takes the status code and treats a non-2xx
as a failure before looking at the body. The top-level "errors" flag is
item-level and cannot override a transport or application error. Both
paths call it: the async handler drops its duplicated status check, and
the sync handler stores the status so Export can pass it in.

Tests assert the invariant directly, including HTTP 500 with
{"errors":false} on the sync-shaped path, plus the errors:true generic
reason and the missing item-error branches. A full handler-level mock
across HttpClient/Session is out of scope: the exporter has no such mock
today (its network tests are DISABLED_), and the status invariant is
what the bug was, so it is covered at the validator instead.

Fixes open-telemetry#4295

Signed-off-by: thc1006 <[email protected]>
@thc1006 thc1006 changed the title [BUG] Parse the Elasticsearch bulk response instead of matching a substring [BUG] Decide Elasticsearch bulk export success from HTTP status and the errors flag Jul 25, 2026
thc1006 added a commit to thc1006/opentelemetry-cpp that referenced this pull request Jul 25, 2026
thc1006 added a commit to thc1006/opentelemetry-cpp that referenced this pull request Jul 25, 2026
Review of open-telemetry#4297 found the synchronous path reported a non-2xx response
as a success: ResponseHandler::OnResponse only logged the status and
unconditionally set response_received_, waitForResponse returned true,
and Export then checked the body alone, so HTTP 500 with a body of
{"errors":false} became kSuccess. My description claiming the sync path
already checked the status was wrong: it observed the status but never
let it affect the ExportResult.

IsBulkResponseSuccessful now takes the status code and treats a non-2xx
as a failure before looking at the body. The top-level "errors" flag is
item-level and cannot override a transport or application error. Both
paths call it: the async handler drops its duplicated status check, and
the sync handler stores the status so Export can pass it in.

Tests assert the invariant directly, including HTTP 500 with
{"errors":false} on the sync-shaped path, plus the errors:true generic
reason and the missing item-error branches. A full handler-level mock
across HttpClient/Session is out of scope: the exporter has no such mock
today (its network tests are DISABLED_), and the status invariant is
what the bug was, so it is covered at the validator instead.

Fixes open-telemetry#4295

Signed-off-by: thc1006 <[email protected]>
@thc1006
thc1006 force-pushed the bugfix/elasticsearch-response-handling branch from d751066 to 229ee19 Compare July 25, 2026 09:28
@thc1006
thc1006 marked this pull request as ready for review July 25, 2026 19:12
@thc1006
thc1006 requested a review from a team as a code owner July 25, 2026 19:12
Copilot AI review requested due to automatic review settings July 25, 2026 19:12

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@thc1006

thc1006 commented Aug 11, 2026 •

Copy link
Copy Markdown
Member Author

Eighteen days and no human has looked at this yet. The one review on it is Copilot reporting that it could not run because the requester was out of quota, and CI has been green throughout. So here is the short entry point that CONTRIBUTING.md asks for, because the description above is long and I suspect that is part of why this is easier to skip than to start.

What it does. Both export paths decided a bulk write had succeeded with body.find("\"failed\" : 0"). That substring is per shard information about one item rather than the batch outcome, and it bakes in pretty printed whitespace, so it was wrong in both directions: a batch with a rejected item read as success, and a compact successful response read as failure. One helper now decides it from the HTTP status, the top level errors flag, and one acknowledgement per record the request submitted.

Where to look, if you would rather spot check than read it all.

  • exporters/elasticsearch/src/es_log_record_exporter.cc, +50/-40, is the entire production change. Everything else is the new helper, its tests, and build files.
  • detail/es_bulk_response.h is the file worth reading. It is the whole decision in one function.
  • The behaviour a user would notice: a response that does not acknowledge every record is now a failure rather than a success. That is the point of the change, and the description says why an exporter claiming success without an acknowledgement is the worse direction to be wrong in.
  • Previously, an HTTP 500 could be reported as a successful synchronous export if the response body contained the exact marker "failed" : 0. The new helper rejects any bulk response whose HTTP status is not 200.

Two choices I flagged as open, which I am now closing rather than leaving on you. I raised them because both are firsts for this repository, not because I think they are wrong, and re-reading the description I can see that asking for two decisions on top of a review is a good reason to defer the whole thing.

  1. The helper is a detail/ header, excluded from the installed package. It is a header rather than an anonymous namespace function in the .cc so the tests can reach it, which an earlier revision could not. The install output is verified against a clean prefix.
  2. On the Bazel side it has its own package private target reached through implementation_deps, so a Bazel consumer cannot include a header the CMake package deliberately does not install. Checked both ways: a consumer depending only on :es_log_record_exporter fails to find the header, and the same consumer with :es_bulk_response builds.

Both stay as they are unless you would rather they did not. Say the word on either and I will change it, but nothing is waiting on an answer.

Edited 16 Aug. It is a draft now, and that is my doing rather than anything about the code. Reading it alongside #4331 and #4337 turned up two gaps I had not pinned: no asynchronous case carried a non-200 status through to the parser, and no successful case carried more than one record, so either could have been hard-coded without a test noticing. Both are measured now, with the mutation that used to pass and no longer does. The original text said 18 August; that clock is paused while it sits in draft, and I have folded the follow-up comment in here rather than leave two to read.

Edited 3 October. I have taken out a line that said I would bring this to the C++ SIG meeting if it was still unlooked-at. I am not going to, and leaving it there read as a threat. @dbarker asked on #4448 for a map of the related open issues and their priority, and I sent one. @om7057 reviewed the sibling change on #4502 and found a real error in a comment of mine. Curl fixes kept landing through September. None of that is the neglect that clause in CONTRIBUTING.md is written for. The length of this diff is the likelier reason and that part is mine, so I have put the smallest of the three forward on #4331 instead of pressing this one.

@thc1006
thc1006 marked this pull request as draft August 13, 2026 17:45
@thc1006
thc1006 force-pushed the bugfix/elasticsearch-response-handling branch 4 times, most recently from 2be3585 to 04152bf Compare August 15, 2026 08:48
thc1006 added a commit to thc1006/opentelemetry-cpp that referenced this pull request Aug 15, 2026
…the log assertions

Three things the cases were getting away with.

The deferred helper published the handler into a slot beside the promise and signalled
with the promise. EXPECT_EQ is not fatal, so when the five second handoff wait timed out
the helper carried on and read that slot while the exporter's thread could still be
writing it. A wait that times out has not observed the promise becoming ready, so those
two accesses have nothing ordering them and the failure path was itself undefined. The
handler now travels in the promise and is taken from the future, and each precondition
reports and returns instead of leaving the rest of the helper to run on state it just
said was wrong.

The cases that read the exporter's error lines now sit on their own fixture. Below error
level OTEL_INTERNAL_LOG_ERROR expands to nothing rather than being filtered, so with
-DOTEL_INTERNAL_LOG_LEVEL=0 the ones expecting the winner to report failed on correct
code and the ones expecting silence from the loser passed without testing anything.
Measured: with the fixture, twelve pass and those six skip; move one back and it fails
expecting one line and finding none.

The accepted bulk body now names an index. open-telemetry#4297 requires a string _index in every index
acknowledgement, so without it every success case here would be read as a failure once
these two meet, which is the opposite of what this file's comment promised.

The two deferred cases are renamed for what they hold. The session publishes the handler
before SendRequest() returns, so they cover a callback delivered after the handoff while
the export is still running, not a waiter proven to be inside cv_.wait().

Signed-off-by: thc1006 <[email protected]>
@thc1006
thc1006 marked this pull request as ready for review August 16, 2026 01:59
@thc1006
thc1006 force-pushed the bugfix/elasticsearch-response-handling branch from 4a4b935 to 1f80e02 Compare August 24, 2026 15:29
thc1006 added a commit to thc1006/opentelemetry-cpp that referenced this pull request Aug 25, 2026
…the log assertions

Three things the cases were getting away with.

The deferred helper published the handler into a slot beside the promise and signalled
with the promise. EXPECT_EQ is not fatal, so when the five second handoff wait timed out
the helper carried on and read that slot while the exporter's thread could still be
writing it. A wait that times out has not observed the promise becoming ready, so those
two accesses have nothing ordering them and the failure path was itself undefined. The
handler now travels in the promise and is taken from the future, and each precondition
reports and returns instead of leaving the rest of the helper to run on state it just
said was wrong.

The cases that read the exporter's error lines now sit on their own fixture. Below error
level OTEL_INTERNAL_LOG_ERROR expands to nothing rather than being filtered, so with
-DOTEL_INTERNAL_LOG_LEVEL=0 the ones expecting the winner to report failed on correct
code and the ones expecting silence from the loser passed without testing anything.
Measured: with the fixture, twelve pass and those six skip; move one back and it fails
expecting one line and finding none.

The accepted bulk body now names an index. open-telemetry#4297 requires a string _index in every index
acknowledgement, so without it every success case here would be read as a failure once
these two meet, which is the opposite of what this file's comment promised.

The two deferred cases are renamed for what they hold. The session publishes the handler
before SendRequest() returns, so they cover a callback delivered after the handoff while
the export is still running, not a waiter proven to be inside cv_.wait().

Signed-off-by: thc1006 <[email protected]>
@thc1006
thc1006 force-pushed the bugfix/elasticsearch-response-handling branch from 04a1b0c to f23c186 Compare August 26, 2026 21:50
Both export paths decided whether a bulk write had succeeded by searching
the response body for the literal `"failed" : 0`. That field is per-shard
information for a single item rather than the outcome of the batch, which
is the top-level `errors` flag, and the literal baked in pretty-printing
whitespace besides. It was wrong in both directions: a batch holding a
rejected item read as success, and a compact successful response read as
failure.

One helper, `IsBulkResponseSuccessful(status_code, body, expected_items,
reason)`, now answers for both paths. A non-2xx status fails first. Then
the body has to be a JSON object carrying a boolean `errors` flag and an
`items` array with one operation result per operation the request
submitted, and `errors` has to be false. A malformed body is a failure
rather than a silent success, and the reason the helper reports is the
one written to the log.

Squashed from fourteen commits so the rebase onto main resolves once
rather than at every step. No content change: the tree is identical to
the one the fourteen produced.

Signed-off-by: thc1006 <[email protected]>
…ccepts

Main grew its own FakeResponse, FakeRequest, FakeSession and FakeHttpClient
in open-telemetry#4071 and open-telemetry#4501, in an unnamed namespace, and this branch already had
four of the same names in another unnamed namespace in the same file.
Reopening an unnamed namespace names the same namespace, so the rebase
merged both without a conflict into four redefinitions that do not compile.

Merged rather than renamed. Main's set is used once and answers with one
fixed response; this branch's runs a script the case supplies, which is the
general case. So the session and the client take a script and default to
answering the way main's did, and the existing call site is untouched.

The default body had to change too, which is not cosmetic. It read
`{"errors": false, "failed" : 0}`, and this pull request stops reading
`failed` and starts requiring one `items` result per submitted record, so
that body is now rejected: with it,
ExportingARecordWithInvalidUtf8DoesNotAbort returns kFailure and fails.
The default is now a body a server could send for a one record batch, and
the comment that described the old substring search is rewritten.

Verified in both configurations, since the guards cut different code:
26 tests, 22 passed and 4 skipped without async export, 20 passed and 6
skipped with it, no failures in either.

Signed-off-by: thc1006 <[email protected]>
@thc1006
thc1006 force-pushed the bugfix/elasticsearch-response-handling branch from f23c186 to d08ae97 Compare September 8, 2026 06:01
thc1006 added a commit to thc1006/opentelemetry-cpp that referenced this pull request Sep 8, 2026
…the log assertions

Three things the cases were getting away with.

The deferred helper published the handler into a slot beside the promise and signalled
with the promise. EXPECT_EQ is not fatal, so when the five second handoff wait timed out
the helper carried on and read that slot while the exporter's thread could still be
writing it. A wait that times out has not observed the promise becoming ready, so those
two accesses have nothing ordering them and the failure path was itself undefined. The
handler now travels in the promise and is taken from the future, and each precondition
reports and returns instead of leaving the rest of the helper to run on state it just
said was wrong.

The cases that read the exporter's error lines now sit on their own fixture. Below error
level OTEL_INTERNAL_LOG_ERROR expands to nothing rather than being filtered, so with
-DOTEL_INTERNAL_LOG_LEVEL=0 the ones expecting the winner to report failed on correct
code and the ones expecting silence from the loser passed without testing anything.
Measured: with the fixture, twelve pass and those six skip; move one back and it fails
expecting one line and finding none.

The accepted bulk body now names an index. open-telemetry#4297 requires a string _index in every index
acknowledgement, so without it every success case here would be read as a failure once
these two meet, which is the opposite of what this file's comment promised.

The two deferred cases are renamed for what they hold. The session publishes the handler
before SendRequest() returns, so they cover a callback delivered after the handoff while
the export is still running, not a waiter proven to be inside cv_.wait().

Signed-off-by: thc1006 <[email protected]>
Signed-off-by: thc1006 <[email protected]>

# Conflicts:
#	CHANGELOG.md
Signed-off-by: thc1006 <[email protected]>

# Conflicts:
#	CHANGELOG.md
Signed-off-by: thc1006 <[email protected]>

# Conflicts:
#	CHANGELOG.md
Signed-off-by: thc1006 <[email protected]>

# Conflicts:
#	CHANGELOG.md
The three es_bulk_response.h contracts restated themselves, and a test section
banner carried its own rules. IsBulkResponseSuccessful keeps 16 lines, over the
10 that is main's longest doxygen in this component, because it documents four
parameters and a four-part return condition.

es_log_record_exporter_test passes 22 with 4 skipped in the synchronous build,
and clang-format 18.1.8 is clean.

Signed-off-by: thc1006 <[email protected]>
The 1.30.0 release rewrote the Unreleased section, which is the whole
conflict. Both sides kept, ours after main's.

Signed-off-by: thc1006 <[email protected]>
The 1.30.0 release rewrote that section, and resolving the conflict in
place left this entry inside the released block, where it reads as
shipped. Rebuilt as main's file with this branch's entry inserted under
Unreleased, so the only difference from main is the entry itself.

Signed-off-by: thc1006 <[email protected]>
The only conflict is the Unreleased section again. Rebuilt as main's
changelog with this branch's entries inserted under Unreleased, so the
only difference from main is those entries.

Signed-off-by: thc1006 <[email protected]>
The Unreleased section is the only conflict. Rebuilt as main's changelog
with this branch's entries under Unreleased.

Signed-off-by: thc1006 <[email protected]>

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.

[BUG] Elasticsearch exporter decides bulk success by substring instead of the errors field

2 participants