feat(tracking): capture utm_source alongside client family#1106
Merged
Conversation
Add server-side resolveUtmSource() to client-family.ts that buckets the raw utm_source query param into a fixed set of known values (currently "plugin"; anything else maps to "other") to keep metric cardinality bounded. Wire it through: - metrics.ts: getMcpRequestAttributes reads utm_source and both getMetricAttributes / annotateTrackedRequestSpan emit app.utm_source when present - mcp-handler.ts: reads utm_source and sets app.utm_source on the active span This lets traffic from the AI plugin (utm_source=plugin, added in getsentry/sentry-for-ai#191) be attributed in traces and the app.server.response metric. Co-Authored-By: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
3ce9264 to
2fa190f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds server-side UTM attribution tracking so traffic tagged with
?utm_source=plugin(added by getsentry/sentry-for-ai#191 to the AI plugin's MCP URL) is visible in spans and metrics.Why
Without this,
utm_source=pluginarrives at/mcpbut never lands on any span attribute or metric dimension.Changes
New:
server/lib/attribution.tsresolveUtmSource(raw)— buckets raw values into"plugin" | "other" | null(absent param →null, so absence and unknown stay distinct)resolveUtmSourceFromUrl(url)— convenience wrapper that reads directly from a URL objectUTM_SOURCE_ATTRIBUTE = "app.utm_source"— exported constant used at every call site to prevent driftclient-family.ts(which is purely User-Agent → client family) and from the browser-sideclient/utils/attribution.ts(different known values, different API)server/index.tsapp.utm_sourceearly on the active span for/mcprequests, matching the existingapp.client.familyearly-annotation pattern. Covers requests that are rate-limited or rejected before reachingmcp-handler.ts.server/lib/mcp-handler.tsapp.utm_sourceon the active span for authenticated MCP requests (alongside the existingapp.client.family,app.server.mode.agent,app.server.mode.experimental).server/metrics.tsgetMcpRequestAttributesreadsutm_sourceviaresolveUtmSourceFromUrlgetMetricAttributesemitsapp.utm_sourceon theapp.server.responsemetric for MCP routesannotateTrackedRequestSpansetsapp.utm_sourceon the span for MCP routesserver/lib/attribution.test.ts10 tests:
resolveUtmSource,resolveUtmSourceFromUrl, and constant value.OTel note
OTel has no specific semantic convention for UTM params. Following the existing
app.*custom namespace used by the rest of the codebase (app.client.family,app.server.mode.agent, etc.).Verified
attribution.test.ts: 10/10 tests passmcp-cloudflaresuite: 356/356 tests passView Session in Sentry