Skip to content

[ObsPresentation][APM] Handle malformed span.links data in trace samples#251442

Merged
MiriamAparicio merged 3 commits intoelastic:mainfrom
MiriamAparicio:fix-span-links-malformed
Feb 4, 2026
Merged

[ObsPresentation][APM] Handle malformed span.links data in trace samples#251442
MiriamAparicio merged 3 commits intoelastic:mainfrom
MiriamAparicio:fix-span-links-malformed

Conversation

@MiriamAparicio
Copy link
Copy Markdown
Contributor

@MiriamAparicio MiriamAparicio commented Feb 3, 2026

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

image

AFTER

Screen.Recording.2026-02-03.at.12.16.46.mov

@MiriamAparicio MiriamAparicio added the release_note:skip Skip the PR/issue when compiling release notes label Feb 3, 2026
@MiriamAparicio MiriamAparicio requested a review from a team as a code owner February 3, 2026 12:37
@MiriamAparicio MiriamAparicio added backport:version Backport to applied version labels v9.3.0 v9.4.0 Team:obs-presentation Focus: APM UI, Infra UI, Hosts UI, Universal Profiling, Obs Overview and left Navigation labels Feb 3, 2026
@elasticmachine
Copy link
Copy Markdown
Contributor

Pinging @elastic/obs-presentation-team (Team:obs-presentation)

Copy link
Copy Markdown
Contributor

@crespocarlos crespocarlos 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 the quick fix. left some questions

span_id: event[OTEL_SPAN_LINKS_SPAN_ID],
}),
[SPAN_LINKS]: Array.isArray(source?.span?.links)
? source.span.links
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If Elasticsearch returns a valid span.links but not as an array, aren't we discarding it?

Copy link
Copy Markdown
Contributor Author

@MiriamAparicio MiriamAparicio Feb 3, 2026

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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' } }],
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For this scenario, why is the expected result to be {}?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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 {}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

but we could enforce it to be an array, right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fixed the tests

Copy link
Copy Markdown
Member

@jennypavlova jennypavlova left a comment

Choose a reason for hiding this comment

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

The code LGTM 💯 Thanks for the fix!

? Array.isArray(source?.span?.links)
? source.span.links
: [source.span.links]
: [];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
: [];
: undefined;

If we return an empty array here, mapOtelToSpanLink will never be called if span.links is undefined.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fixed here f34644b

@elasticmachine
Copy link
Copy Markdown
Contributor

💚 Build Succeeded

Metrics [docs]

✅ unchanged

History

@MiriamAparicio MiriamAparicio merged commit fe0a748 into elastic:main Feb 4, 2026
16 checks passed
@kibanamachine
Copy link
Copy Markdown
Contributor

Starting backport for target branches: 9.3

https://github.com/elastic/kibana/actions/runs/21669973216

kibanamachine pushed a commit to kibanamachine/kibana that referenced this pull request Feb 4, 2026
…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)
@kibanamachine
Copy link
Copy Markdown
Contributor

💚 All backports created successfully

Status Branch Result
9.3

Note: Successful backport PRs will be merged automatically after passing CI.

Questions ?

Please refer to the Backport tool documentation

kibanamachine added a commit that referenced this pull request Feb 4, 2026
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport:version Backport to applied version labels release_note:skip Skip the PR/issue when compiling release notes Team:obs-presentation Focus: APM UI, Infra UI, Hosts UI, Universal Profiling, Obs Overview and left Navigation v9.3.0 v9.4.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants