Commit 0e82bbf
fix(node): Bootstrap Mastra observability under ESM (#24177)
The Mastra integration failed to auto-bootstrap `@mastra/observability`
under ESM when the app configured no observability of its own.
`loadMastraObservability()` anchored module resolution on
`process.cwd()` and the CJS `require.cache`, but an ESM-loaded
`@mastra/core` never lands in that cache, leaving cwd as the only
anchor. When the app's dependencies don't live under cwd — a
serverless/monorepo layout, or concretely the per-suite temp install the
integration tests use — resolution failed silently: no exporter was
attached and no `gen_ai` spans were produced, so the ESM `scenario-auto`
test timed out.
The runtime injection hook already receives the resolved `file:` URL of
every module it instruments and was throwing it away. We now record it
on `__SENTRY_ORCHESTRION__.runtimeFiles` and let the Mastra integration
anchor `@mastra/observability` resolution on the app's actual
`@mastra/core` file. This works under both ESM and CJS and no longer
depends on cwd; the CJS-cache and cwd lookups remain as fallbacks.
Decisions:
- Anchor on `@mastra/core`, not `@mastra/observability`. The latter is
never loaded in the failing case — that is precisely why the SDK has to
bootstrap it — so there is nothing to hook there. `@mastra/core` is
always loaded when the integration runs.
- Fix it in SDK code rather than by changing the scenario child's cwd in
the test runner. The cwd approach would have papered over a real
production gap (ESM apps whose cwd is not the app directory, e.g.
serverless) that the code's own comments already acknowledged.
This also moves `@mastra/core` and `@mastra/observability` out of the
`node-integration-tests` workspace `devDependencies` into the runner's
per-suite `additionalDependencies`. `@mastra/core` declares
`engines.node >= 22.13`, so a workspace `devDependency` broke `yarn
install` on the Node 20.19 CI matrix even though the suite is gated
behind `conditionalTest({ min: 22 })`. Installing them into the test's
temp dir at run time means Node 20 never resolves them, while Node 22+
runs get them exactly as before.
_Root cause_: cwd/`require.cache`-based resolution has no valid anchor
for an optional peer dependency of an ESM-loaded module when the app's
`node_modules` is not under `process.cwd()`.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>1 parent 9596f42 commit 0e82bbf
7 files changed
Lines changed: 314 additions & 507 deletions
File tree
- dev-packages/node-integration-tests
- suites/tracing/mastra
- packages
- core/src/utils
- server-runtime-injection/src
- server-utils
- src/integrations
- test/integrations/mastra
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
47 | | - | |
48 | | - | |
49 | 47 | | |
50 | 48 | | |
51 | 49 | | |
| |||
Lines changed: 228 additions & 177 deletions
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
61 | 61 | | |
62 | 62 | | |
63 | 63 | | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
64 | 73 | | |
65 | 74 | | |
66 | 75 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
97 | 97 | | |
98 | 98 | | |
99 | 99 | | |
100 | | - | |
| 100 | + | |
101 | 101 | | |
102 | 102 | | |
103 | 103 | | |
| |||
110 | 110 | | |
111 | 111 | | |
112 | 112 | | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
113 | 116 | | |
114 | 117 | | |
115 | 118 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | | - | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
169 | 170 | | |
170 | 171 | | |
171 | 172 | | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
172 | 191 | | |
173 | 192 | | |
174 | 193 | | |
| 194 | + | |
175 | 195 | | |
176 | 196 | | |
177 | 197 | | |
| |||
197 | 217 | | |
198 | 218 | | |
199 | 219 | | |
200 | | - | |
201 | | - | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
202 | 223 | | |
203 | 224 | | |
204 | 225 | | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
205 | 230 | | |
206 | 231 | | |
207 | 232 | | |
| |||
Lines changed: 35 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
| 12 | + | |
11 | 13 | | |
12 | 14 | | |
13 | 15 | | |
| |||
59 | 61 | | |
60 | 62 | | |
61 | 63 | | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
62 | 97 | | |
63 | 98 | | |
64 | 99 | | |
| |||
0 commit comments