-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OB-35469: fix: add subscription request parent span as child of disco…
…very request
- Loading branch information
1 parent
52a46e9
commit 9d575aa
Showing
2 changed files
with
51 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
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} | ||
} |
This file contains 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