Repository navigation
Conversation
Adds an APM plugin that propagates W3C baggage and tracestate headers on configured trace URLs so browser journey executions can be cross-linked with the traced application's APM data. The monitor context (id, type, location) and per-execution trace id are read from the ELASTIC_SYNTHETICS_* env vars injected by Heartbeat (elastic/beats#51083), falling back to the journey's monitor config. Re-applied on top of the multi step api runner refactor (#997): the APM plugin is browser-only and is registered via PluginManager only for browser drivers. Relates to #595 Co-authored-by: Cursor <cursoragent@cursor.com>
Importing from the `../core` barrel created a load-time cycle (runner.ts -> plugins -> apm -> core -> globals -> runner) that left the `runner` binding undefined, breaking journey registration. Import directly from `../core/globals`, which does not pull in the plugins barrel. Co-authored-by: Cursor <cursoragent@cursor.com>
The baggage header joined members with ';', but per the W3C Baggage spec
members are separated by ',' (';' delimits per-member properties). Spec
compliant parsers (e.g. OpenTelemetry's W3CBaggagePropagator) therefore
read the whole string as a single member and drop everything after the
first key, so only synthetics.trace.id survived cross-linking.
Join members with ',' and percent-encode values. Verified end-to-end
against an OTel-instrumented app (baggage span processor -> APM Server ->
Elasticsearch): all of synthetics.monitor.id/type/location and
synthetics.trace.id now land as labels on the APM transaction.
Co-authored-by: Cursor <cursoragent@cursor.com>
Bind the route handler once so unroute matches the registered function, and only set the baggage header when there is synthetics context to share.
…alidate apm config - merge our baggage members and es tracestate entry into the headers the page already sends instead of replacing them - round sample rates between 0 and 0.0001 up to 0.0001 per the agent sampling spec (was 0.001) - validate the apm config block with clear errors and only apply it for the run command - document ApmOptions caveats and cover the wiring with tests
| } | ||
|
|
||
| options.screenshots = cliArgs.screenshots ?? 'on'; | ||
| options.apm = normalizeApmOptions(config.apm); |
There was a problem hiding this comment.
from CC:
apm never reaches Heartbeat-run monitors, where the baggage data exists**
-
Why:
options.apmis set only fromconfig.apminrunmode. That has three consequences:- Project monitor bundles contain only the bundled journey code (
src/push/bundler.ts:71, which runsarchive.append(code, …)), notsynthetics.config.ts. pushmode explicitly dropsapm, as the testoptions.test.ts › is not applied when pushing monitorsshows.- Inline runs skip reading the config (
src/options.ts:68-71).
There is no CLI flag either. Meanwhile,
generateBaggageHeaderdepends onELASTIC_SYNTHETICS_TRACE_ID,_MONITOR_TYPEand_LOCATION, which only Heartbeat sets. So the feature works locally, where the baggage holds onlysynthetics.monitor.id=<journey id>. It is inert in production, where the cross-link would matter. That misses the PR's stated goal ("support cross-linking synthetics & apm"). - Project monitor bundles contain only the bundled journey code (
-
Example: A user adds
apm: { traceUrls: ['**/api/**'] }tosynthetics.config.tsand runsnpx @elastic/synthetics push. The private location runs the monitor and its/apirequests carry nobaggageortracestate. Nothing warns the user. -
Fix: Pick a delivery channel and test it:
- a CLI flag such as
--apm-trace-urls/--apm-sample-rate, which Heartbeat/Kibana can pass throughsynthetics_args, or - pushing
apmas part of the monitor config so Heartbeat can forward it.
At minimum, document the feature as local-only and warn during
pushwhenconfig.apmis set. Confirm with the Heartbeat/Kibana owners (see questions 1–2). - a CLI flag such as
| if (baggage) { | ||
| headers[BAGGAGE_HEADER] = mergeBaggage(existing[BAGGAGE_HEADER], baggage); | ||
| } | ||
| route.continue({ headers }); |
| const existing = request.headers(); | ||
| // Merge into whatever the page already sends so the traced app keeps its | ||
| // own baggage and other vendors keep their tracestate entries | ||
| const headers = { |
- add --apm-trace-urls so monitors run by Heartbeat, which never read synthetics.config.ts, can enable the baggage propagation through synthetics_args. The flag takes precedence over apm.traceUrls - warn on push when apm is set in the config, since project monitors do not carry it to the location that runs them - stop injecting tracestate and drop apm.sampleRate: tracestate without a traceparent is ignored per the W3C trace context spec, and overriding the es entry of a page that already sends traceparent (EDOT RUM, OTel) would corrupt that trace's sampling rate - await route.continue in the handler - note in the ApmOptions docs that request interception disables the browser HTTP cache
📋 Summary
Adds cross-linking between Synthetics journey executions and the APM data of the applications they exercise. 🔗 Revives and rebases #595 (originally by @vigneshshanmugam) onto current
main, now that the Heartbeat side (elastic/beats#51083) has merged.For browser journeys, a new APM plugin intercepts requests to the URLs you configure and adds a W3C
baggageheader that carries the synthetics context, so the traced app's agent can attach it to its APM documents:synthetics.trace.id: the per-execution id (equalsmonitor.check_group)synthetics.monitor.idsynthetics.monitor.typesynthetics.monitor.locationOur members are appended to any
baggagethe page already sends.traceparentandtracestateare never touched, so a page that is already instrumented (EDOT RUM, an OTel SDK) stays the owner of its trace and its sampling decision.The monitor context and trace id come from the
ELASTIC_SYNTHETICS_*environment variables injected by Heartbeat (elastic/beats#51083), falling back to the journey's own monitor config when run standalone.List only the origins of the apps you trace. Enabling request interception turns off the browser HTTP cache for the journey, and cross-origin requests that carry
baggagetrigger a CORS preflight the server has to allow.⚙️ How the cross-link works end to end
ELASTIC_SYNTHETICS_{TRACE_ID,MONITOR_ID,MONITOR_TYPE,MONITOR_LOCATION}(TRACE_ID == monitor.check_group).baggageto requests matching the trace URLs.traces-apm.*.synthetics.trace.id == synthetics.monitor.check_group.🚚 How the trace URLs reach the runner
apm.traceUrlsinsynthetics.config.ts, or--apm-trace-urlssynthetics_args: ['--apm-trace-urls', '<url>']pushwarns whenapmis set in the config. Tracked in elastic/kibana#304060, with an optional typed Heartbeat setting in elastic/beats#54564.🛠️ Implementation notes
driver.context), registered throughPluginManagerfor browser drivers only, so API journeys skip it. Re-applied on top of the multi step API runner refactor (feat: multi step api runner #997).,and values are percent-encoded, per the W3C Baggage spec. (An earlier;-joined form collapsed to the first key on spec-compliant parsers, caught during E2E.)stop()can actually unroute it.apmblock is validated when the config is read, so a bad value fails with a clear message instead of aTypeErrorwhen the journey starts.🚫 Not included
tracestateandapm.sampleRate. The earlier revision injectedtracestate: es=s:<rate>. Without atraceparent, the W3C spec saystracestatemust not be parsed, so it had no effect. On a page that already sendstraceparent, overriding theesentry would corrupt that trace's sampling rate. This comes back together withtraceparentpropagation in a follow-up.🤖 Agent support (important)
The
synthetics.*context only lands on APM docs if the traced app's agent captures W3C baggage:For agents that lift baggage, the resulting label naming differs by agent (e.g. OTel/APM Server stores
labels.synthetics_trace_id; classic agents prefix withbaggage.). The Kibana-side join query must account for this.✅ Test plan
baggage,traceparentandtracestate), config validation, and the CLI flag end to end with Heartbeat-style env vars (__tests__/plugins/apm.test.ts,__tests__/options.test.ts,__tests__/cli.test.ts)synthetics.*fields land on the APM transaction andsynthetics.trace.idmatches the injected trace id.synthetics-browser-*docs'monitor.check_groupequals the APM docs'labels.synthetics_trace_id(andmonitor.locationflows fromrun_from).Relates to elastic/beats#51083
Closes #265
Supersedes #595