feat: header-based client-type classification, user_type on spans - #37
Merged
Conversation
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.
Why
Test traffic is currently identified by regexing
user_agent.originalin the admin console, and every consumer hand-rolls its ownclassifyfor the request-loguser_type. The estate already declares itself: the e2e suite sendsX-Client-Type: test, service-to-service calls sendservice. This makes that header the kit's own classifier and puts the result on SPANS, so the admin console can filter tool/entry spans onattributes_string['user_type']instead of guessing from the UA.What
New
src/client-type.ts- mirrorsweb packages/shared/src/client-type.tsand the Vector VRL edge rules 1:1 (headerservice/testwins -> test UA -> bot UA / scanner path ->user). One deliberate superset: the test UA regex also matchespuppeteer. No new runtime deps.New exports from
@agentage/observability:CLIENT_TYPE_HEADER,USER_TYPE_FIELD,UserType(const + type),type ClientTypeInputclassifyClientType({ header, userAgent, path }): UserTypecontextWithUserType(userType, ctx?),userTypeFromContext(ctx?),stampUserType(span?)Span stamping
createRequestLogclassifies at request ENTRY (was: atfinish), stampsuser_typeon the active span, and runsnext()inside a context carryinguser_typein OTel baggage.withSpanstamps every span it creates from that context.setMcpToolstamps the active tool span - MCP consumers get it with no call-site change.stampUserType(span)is exported for spans consumers create themselves.request-log now defaults
classifyto the kit classifier (header -> UA -> path). Injected classifiers keep winning unchanged;() => undefineddrops the field. Only canonicalUserTypevalues propagate to spans; a custom classifier's own vocabulary still lands on the log line.Behaviour changes to know
classifyis invoked at request entry rather than atfinish(it needs to exist while the request runs). Callers whose classifier reads state set by later middleware would see a different value - none in the estate do (all read headers/UA/path).classifygain auser_typefield on every request line.Tests
test/client-type.test.ts(header precedence, UA fallback, scanner paths, span attribute, baggage propagation, no-op without classification) + request-log cases (header classification, UA/path fallback, span stamp + descendant read, opt-out).test/stack-context-manager.tsregisters a synchronous context manager, since the bare API ships a noop one. 238 tests pass.Release
package.jsonbumped to 0.16.0 + CHANGELOG entry. NOT published: publishing needs achore(release): 0.16.0squash-merge subject on master, which is the merge step, not this PR.Verify
npm ci && npm run verifygreen locally (type-check + lint + format:check + 238 tests + build + dist smoke).