nri-prelude: thread session id through LogHandler#153
Draft
ArthurJordao wants to merge 1 commit into
Draft
Conversation
Adds a per-request session-id slot to LogHandler with the same
plumbing pattern as trackAnalyticsEventIO: an IORef created by
rootTracingSpanIO and shared across child / new-root handlers via
closure capture in mkHandler.
Public surface:
- Platform.sessionId :: Task e (Maybe Text)
- Platform.setSessionIdIO :: LogHandler -> Maybe Text -> IO ()
The intended use is a WAI middleware on the application root: read an
incoming X-NRI-Session-Id header, call setSessionIdIO once on the
per-request LogHandler, and let every Analytics.track call inside the
request stamp it automatically.
Platform.Analytics.Internal.trackEvent reads the ref and shallow-merges
{ "session_id": ... } onto the event's JSON before invoking the
analytics callback, so per-event schemas don't need a session_id
field. Mirrors how event_id/event_timestamp are stamped centrally in
nri-analytics.
Tests cover four cases:
- trackEvent stamps session_id when set
- existing trackEvent test (no session id) still passes unchanged
- sessionId reflects setSessionIdIO writes
- child handlers see the session id set on the root
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.
Summary
Builds on @juliano's analytics handler design (this branch's base) by adding a per-request session id slot to
LogHandler, so request-context metadata rides alongside the analytics callback rather than each event constructor declaring its ownsessionIdfield.sessionIdRef :: IORef (Maybe Text)onLogHandler.mkHandlerandrootTracingSpanIOthread it the same way they threadtrackAnalyticsEventIO— IORef minted once at the request root, shared with every child / new-root handler via closure capture.Platform.hs:sessionId :: Task e (Maybe Text)setSessionIdIO :: LogHandler -> Maybe Text -> IO ()Platform.Analytics.Internal.trackEventreads the ref and shallow-merges{ "session_id": ... }onto the event JSON before invokingtrackAnalyticsEventIO. So every event automatically carries the session id without per-event schemas declaring it (mirrors howevent_id/event_timestampare stamped centrally innri-analytics).Companion PR
setSessionIdIO, the Ruby header injection onRosteringService, and drops the per-eventsessionIdprops fromAnalytics.Event.Test plan
cabal --project-file=cabal.project testfromnri-prelude/: 279 pass, 1 unrelated pre-existing failure (debug-todo-stacktracegolden line-number drift inTestSpec.hs)PlatformSpec.hs:trackEvent stamps the current request's session_id onto the payloadPlatform.sessionId reflects the value written by setSessionIdIOchild handlers see the session id set on the root🤖 Generated with Claude Code