[gin-dynamodb-valkey] demonstrate ctx propagation, SQS poller, outbound HTTP#182
Open
karthikeyangs9 wants to merge 2 commits into
Open
[gin-dynamodb-valkey] demonstrate ctx propagation, SQS poller, outbound HTTP#182karthikeyangs9 wants to merge 2 commits into
karthikeyangs9 wants to merge 2 commits into
Conversation
…nd HTTP Refactor example to show the patterns required for spans to link into one trace per request: - Service struct holds clients only; methods take ctx context.Context as first arg (no struct-cached ctx — the most common cause of orphan span trees in real customer code). - Custom internal spans via tracer.Start(ctx, "Service.X") so handler-side business logic appears as children of the gin SERVER span. - Outbound HTTP via httpagent.NewClient + http.NewRequestWithContext — emits CLIENT spans nested under ctx AND injects W3C traceparent for cross-service trace continuation. - SQS long-poll worker with trace.WithNewRoot() per iteration and a long-lived ctx (signal.NotifyContext on SIGINT/SIGTERM) so polls aren't cancelled mid-flight and recorded as STATUS_CODE_ERROR. - Graceful shutdown via http.Server.Shutdown. README adds a Context Propagation note linking to the product integration doc for the full pattern guide. Removed stale go.mod.new.
…CodeQL #58) CodeQL flagged the /external handler for SSRF — the request URL came from the `?url=` query parameter, which is user-controlled. The endpoint exists purely to demonstrate httpagent.NewClient + ctx propagation, so the URL never needed to be configurable. Hardcoding to httpbin.org/get removes the attack surface without changing what the example teaches.
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
Refactor the
gin-dynamodb-valkeyexample to demonstrate the patterns required for spans to link into one trace per request. Came out of debugging a customer (dev-quests-service) whose traces were producing single-span orphans even thoughagent.Start,ginagent.Middleware, andotelawswere all wired correctly —context.Contextwas not threaded through service / repository layers.Changes
Servicestruct holds clients only; methods takectx context.Contextas first arg. No struct-cached ctx — that's the most common cause of orphan span trees in real customer code.tracer.Start(ctx, "Service.X")so handler-side business logic appears as children of the gin SERVER span.httpagent.NewClient+http.NewRequestWithContext— emits CLIENT spans nested under ctx and injects W3Ctraceparentfor cross-service trace continuation. Barehttp.Clientorphans spans AND breaks downstream traces.trace.WithNewRoot()per iteration and a long-lived ctx (signal.NotifyContexton SIGINT/SIGTERM) so polls aren't cancelled mid-flight and recorded asSTATUS_CODE_ERROR.http.Server.Shutdown.go.mod.new.Companion product-doc PR: last9/product-integrations#fix/golang-gin-ctx-propagation
Test plan
gofmt -l .cleango vet ./...cleango build ./...cleandocker compose up -d+ create+seed table +go run .bootscurl /users/u1produces a trace with SERVER →Service.GetUser→DynamoDB.GetItemlinkedcurl /external?url=https://httpbin.org/getproduces SERVER →Service.CallExternal→HTTP GETlinked, withtraceparentheader sentSQS_QUEUE_URLset: poller emitssqs.pollroot spans withprocess.messagechildren; noSTATUS_CODE_ERRORon empty queues