Skip to content

Commit

Permalink
OB-35469: fix: add subscription request parent span as child of disco…
Browse files Browse the repository at this point in the history
…very request
  • Loading branch information
obs-gh-virjramakrishnan committed Aug 29, 2024
1 parent 52a46e9 commit 9d575aa
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
48 changes: 48 additions & 0 deletions pkg/handler/subscriber/wrap_sqs_context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package subscriber

import (
"bytes"
"context"
"encoding/json"

"github.com/aws/aws-lambda-go/events"

"github.com/aws/aws-lambda-go/lambda"
)

type SQSCheckerWrappedHandler struct {
handler lambda.Handler
}

// Compile time check our Handler implements lambda.Handler.
var _ lambda.Handler = SQSCheckerWrappedHandler{}

// Invoke checks if the incoming payload is from a SQS event and, if so,
// extracts the context from the SQS message and injects it into the context.
// This means that the spans created with this context will appear as children
// of a span in the discover request that created the SQS event
func (h SQSCheckerWrappedHandler) Invoke(ctx context.Context, payload []byte) ([]byte, error) {

var event events.SQSEvent
dec := json.NewDecoder(bytes.NewReader(payload))
dec.DisallowUnknownFields()
if err := dec.Decode(&event); err == nil {
for _, record := range event.Records {
ctx = NewSQSCarrier().Extract(ctx, record.MessageAttributes)
break
}
}

response, err := h.handler.Invoke(ctx, payload)
if err != nil {
return nil, err
}

return response, nil
}

// WrapHandlerSQSCheck Provides a Handler which wraps an existing Handler while
// injecting the SQS context into the context if the payload is a SQS event.
func WrapHandlerSQSCheck(handler lambda.Handler) lambda.Handler {
return SQSCheckerWrappedHandler{handler: handler}
}
5 changes: 3 additions & 2 deletions pkg/tracing/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tracing

import (
"context"
"github.com/observeinc/aws-sam-apps/pkg/handler/subscriber"

"github.com/aws/aws-lambda-go/lambda"
"go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-lambda-go/otellambda"
Expand All @@ -14,11 +15,11 @@ import (
var payloadKey = attribute.Key("payload")

func NewLambdaHandler(handler lambda.Handler, tp *sdktrace.TracerProvider) lambda.Handler {
return otellambda.WrapHandler(
return subscriber.WrapHandlerSQSCheck(otellambda.WrapHandler(
&LambdaHandler{handler},
otellambda.WithTracerProvider(tp),
otellambda.WithFlusher(tp),
)
))
}

type LambdaHandler struct {
Expand Down

0 comments on commit 9d575aa

Please sign in to comment.