-
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, and simplify invocaton call stack In HandleSQS, the context is being injected from the SQS event, when the lambda is triggered by a SQS request. This means that the HandleRequest from the Subscription request, which is properly part of another trace, is assuming the parent trace of the Discovery Request. Hence, the Discover call “steals” the spans from the Subscriber, at least, all of the spans that are descendant from the HandleRequest call of the Subscribe request. That leaves only the parent span of Subscriber, which is created (and thus has context provided to it) before we extract the context from the SQS event. Thus, it shows up as its own trace. Wrapping the OTEL handler wrapper (which is what makes the first span in the trace) with another handler that will add context from the SQS event if the payload is of that format means that the first span should also get the context from the discovery request.
- Loading branch information
1 parent
52a46e9
commit 21c529c
Showing
4 changed files
with
54 additions
and
5 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
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
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,49 @@ | ||
package tracing | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
|
||
"github.com/aws/aws-lambda-go/events" | ||
"github.com/aws/aws-lambda-go/lambda" | ||
"github.com/go-logr/logr" | ||
) | ||
|
||
type SQSWithContextHandler struct { | ||
handler lambda.Handler | ||
} | ||
|
||
// Compile time check our Handler implements lambda.Handler. | ||
var _ lambda.Handler = SQSWithContextHandler{} | ||
|
||
// 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 SQSWithContextHandler) Invoke(ctx context.Context, payload []byte) ([]byte, error) { | ||
logger := logr.FromContextOrDiscard(ctx) | ||
logger.V(3).Info("Getting context from message attributes") | ||
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 | ||
} | ||
|
||
// WrapHandlerSQSContext Provides a Handler which wraps an existing Handler while | ||
// injecting the SQS context into the context if the payload is a SQS event. | ||
func WrapHandlerSQSContext(handler lambda.Handler) lambda.Handler { | ||
return SQSWithContextHandler{handler: handler} | ||
} |
2 changes: 1 addition & 1 deletion
2
pkg/handler/subscriber/sqspropagator.go → pkg/tracing/sqspropagator.go
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package subscriber | ||
package tracing | ||
|
||
import ( | ||
"context" | ||
|