[ObsPresentation][APM] Handle malformed span.links data in trace samples#251442
Conversation
|
Pinging @elastic/obs-presentation-team (Team:obs-presentation) |
crespocarlos
left a comment
There was a problem hiding this comment.
Thanks for the quick fix. left some questions
| span_id: event[OTEL_SPAN_LINKS_SPAN_ID], | ||
| }), | ||
| [SPAN_LINKS]: Array.isArray(source?.span?.links) | ||
| ? source.span.links |
There was a problem hiding this comment.
If Elasticsearch returns a valid span.links but not as an array, aren't we discarding it?
There was a problem hiding this comment.
if it's not an array it will crash when we try to do linkedChild[SPAN_LINKS].some(), that was actually the error here, span.links was returning an object not an array
There was a problem hiding this comment.
what if we did something like:
const spanLinksArray = source?.span?.links ?
Array.isArray(source?.span?.links) ? source.span.links : [source.span.links] :
undefined
...
[SPAN_LINKS]: spanLinksArray
? spanLinksArray
...I'm concerned that we're discarding the data because it's malformed, while we could ensure it's in the correct format instead.
There was a problem hiding this comment.
Right, this makes sense, I think in this specific case the issue was that the data returned was just a single span.link, that for some reason was not returned as an array but the single object:
source.span.links = {
span: { id: 'acb417d416b46fb1' },
trace: { id: '87314fa66e73ccf103627be79f2e6612' }
}
Your approach will work, wrapping this into an array
source.span.links =[{
span: { id: 'acb417d416b46fb1' },
trace: { id: '87314fa66e73ccf103627be79f2e6612' }
}]
Good catch!
| }); | ||
|
|
||
| it.each([ | ||
| ['object', { trace: { id: 'test-trace-id' }, span: { id: 'span-1' } }], |
There was a problem hiding this comment.
For this scenario, why is the expected result to be {}?
There was a problem hiding this comment.
this scenario is the case where span.links is malformed, and we don't have OTEL_SPAN_LINKS_TRACE_ID or OTEL_SPAN_LINKS_SPAN_ID fields, so mapOtelToSpanLink will return an empty array, and getSpanLinksCountById reduce on empty array return {}
There was a problem hiding this comment.
but we could enforce it to be an array, right?
There was a problem hiding this comment.
fixed the tests
jennypavlova
left a comment
There was a problem hiding this comment.
The code LGTM 💯 Thanks for the fix!
| ? Array.isArray(source?.span?.links) | ||
| ? source.span.links | ||
| : [source.span.links] | ||
| : []; |
There was a problem hiding this comment.
| : []; | |
| : undefined; |
If we return an empty array here, mapOtelToSpanLink will never be called if span.links is undefined.
💚 Build Succeeded
Metrics [docs]
History
|
|
Starting backport for target branches: 9.3 https://github.com/elastic/kibana/actions/runs/21669973216 |
…les (elastic#251442) ## Summary Fixes an issue where span.links data that is not an array (e.g., null, object, or string) would cause trace sample requests to fail or return empty responses. This can occur with certain OTel data formats. ### Changes: - Replace ?? operator with explicit `Array.isArray()` check to ensure `span.links` is always an array before calling `.filter()` - Add comprehensive unit tests for malformed data scenarios - Covers cases where `span.links` is null, object, string, or other non-array values BEFORE <img width="2880" height="2084" alt="image" src="https://github.com/user-attachments/assets/d853554d-0bb5-4451-bc8e-bea16f5560f8" /> AFTER https://github.com/user-attachments/assets/8f3fb7b8-77e5-484f-bfbe-9ffed89c9f0c (cherry picked from commit fe0a748)
💚 All backports created successfully
Note: Successful backport PRs will be merged automatically after passing CI. Questions ?Please refer to the Backport tool documentation |
…e samples (#251442) (#251639) # Backport This will backport the following commits from `main` to `9.3`: - [[ObsPresentation][APM] Handle malformed span.links data in trace samples (#251442)](#251442) <!--- Backport version: 9.6.6 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sorenlouv/backport) <!--BACKPORT [{"author":{"name":"Miriam","email":"31922082+MiriamAparicio@users.noreply.github.com"},"sourceCommit":{"committedDate":"2026-02-04T11:38:17Z","message":"[ObsPresentation][APM] Handle malformed span.links data in trace samples (#251442)\n\n## Summary\n\nFixes an issue where span.links data that is not an array (e.g., null,\nobject, or string) would cause trace sample requests to fail or return\nempty responses. This can occur with certain OTel data formats.\n\n### Changes:\n- Replace ?? operator with explicit `Array.isArray()` check to ensure\n`span.links` is always an array before calling `.filter()`\n- Add comprehensive unit tests for malformed data scenarios\n- Covers cases where `span.links` is null, object, string, or other\nnon-array values\n\nBEFORE\n\n<img width=\"2880\" height=\"2084\" alt=\"image\"\nsrc=\"https://github.com/user-attachments/assets/d853554d-0bb5-4451-bc8e-bea16f5560f8\"\n/>\n\nAFTER\n\n\nhttps://github.com/user-attachments/assets/8f3fb7b8-77e5-484f-bfbe-9ffed89c9f0c","sha":"fe0a7486e06da6364dd1084eb4e08d7635d8cb5d","branchLabelMapping":{"^v9.4.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","backport:version","v9.3.0","v9.4.0","Team:obs-presentation"],"title":"[ObsPresentation][APM] Handle malformed span.links data in trace samples","number":251442,"url":"https://github.com/elastic/kibana/pull/251442","mergeCommit":{"message":"[ObsPresentation][APM] Handle malformed span.links data in trace samples (#251442)\n\n## Summary\n\nFixes an issue where span.links data that is not an array (e.g., null,\nobject, or string) would cause trace sample requests to fail or return\nempty responses. This can occur with certain OTel data formats.\n\n### Changes:\n- Replace ?? operator with explicit `Array.isArray()` check to ensure\n`span.links` is always an array before calling `.filter()`\n- Add comprehensive unit tests for malformed data scenarios\n- Covers cases where `span.links` is null, object, string, or other\nnon-array values\n\nBEFORE\n\n<img width=\"2880\" height=\"2084\" alt=\"image\"\nsrc=\"https://github.com/user-attachments/assets/d853554d-0bb5-4451-bc8e-bea16f5560f8\"\n/>\n\nAFTER\n\n\nhttps://github.com/user-attachments/assets/8f3fb7b8-77e5-484f-bfbe-9ffed89c9f0c","sha":"fe0a7486e06da6364dd1084eb4e08d7635d8cb5d"}},"sourceBranch":"main","suggestedTargetBranches":["9.3"],"targetPullRequestStates":[{"branch":"9.3","label":"v9.3.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v9.4.0","branchLabelMappingKey":"^v9.4.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/251442","number":251442,"mergeCommit":{"message":"[ObsPresentation][APM] Handle malformed span.links data in trace samples (#251442)\n\n## Summary\n\nFixes an issue where span.links data that is not an array (e.g., null,\nobject, or string) would cause trace sample requests to fail or return\nempty responses. This can occur with certain OTel data formats.\n\n### Changes:\n- Replace ?? operator with explicit `Array.isArray()` check to ensure\n`span.links` is always an array before calling `.filter()`\n- Add comprehensive unit tests for malformed data scenarios\n- Covers cases where `span.links` is null, object, string, or other\nnon-array values\n\nBEFORE\n\n<img width=\"2880\" height=\"2084\" alt=\"image\"\nsrc=\"https://github.com/user-attachments/assets/d853554d-0bb5-4451-bc8e-bea16f5560f8\"\n/>\n\nAFTER\n\n\nhttps://github.com/user-attachments/assets/8f3fb7b8-77e5-484f-bfbe-9ffed89c9f0c","sha":"fe0a7486e06da6364dd1084eb4e08d7635d8cb5d"}}]}] BACKPORT--> Co-authored-by: Miriam <31922082+MiriamAparicio@users.noreply.github.com>
Summary
Fixes an issue where span.links data that is not an array (e.g., null, object, or string) would cause trace sample requests to fail or return empty responses. This can occur with certain OTel data formats.
Changes:
Array.isArray()check to ensurespan.linksis always an array before calling.filter()span.linksis null, object, string, or other non-array valuesBEFORE
AFTER
Screen.Recording.2026-02-03.at.12.16.46.mov