Skip to content

jacob-elektronik/go-spanctx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Span Context Module

This module helps with the propagation of Jaeger SpanContexts. It provides functions to add SpanContexts to AWS Lambda calls, as well as to AMQP, AWS SNS and AWS SQS messages.

AWS Lambda

Passing SpanContexts to Lambda functions only works for synchronous calls, i.e. if the Lambda's invocation type is RequestResponse (which is the default). The reason for this limitation is that the SpanContext is embedded in the ClientContext which not send along for asynchronous calls, i.e. when the invocation type is Event.

Hint: Avoid excessive amounts of baggage since AWS limits the size of ClientContext.

Here's how to pass a SpanContext to a Lambda function:

    params := &lambda.InvokeInput{
        FunctionName: aws.String(fnName),
        Payload:      []byte(payload),
    }
    err := spanctx.AddToLambdaInvokeInput(span.Context(), params)
    if err != nil { /* handle error */ }
    resp, err := lambdaClient.Invoke(params)

Here's how to retrieve the SpanContext from the Lambda handler's context argument:

func main() {
    lambda.Start(func(ctx context.Context) error {
        spanCtx, err := spanctx.GetFromLambdaContext(ctx)
        if err != nil { /* handle error */ }
        ...
    })
}

AMQP

SpanContexts are added to the headers of AMQP message.

Here's how to add a SpanContext to an AMQP message:

message := amqp.Publishing{
    ContentType: msgMimeType,
    Body:        msgBody,
}
err := spanctx.AddToAMQPPublishing(span.Context(), &message)
if err != nil { /* handle error */ }
err = channel.Publish(
    exchangeName,
    routingKey,
    isMandatory,
    isImmediate,
    message,
)

Here's how to retrieve a SpanContext from an AMQP message:

delivery, more := <- msgChan
if !more { /* deal with closed channel */ }
spanCtx, err := spanctx.GetFromAMQPDelivery(delivery)
if err != nil { /* handle error */ }

AWS SNS

SpanContexts are added to the SNS message attributes.

Note that these message attributes are not send, if PublishInput.MessageStructure is set to json (i.e. if you want to send different strings to different subscription types).

Here's how to add a SpanContext to an SNS message:

params := &sns.PublishInput{
    Message: aws.String(message),
    QueueUrl:    aws.String(queueURL),
}
err := spanctx.AddToSNSPublishInput(span.Context(), params)
if err != nil { /* handle error */ }
resp, err := sqsClient.SendMessage(params)

Here's how to retrieve a SpanContext from an SNS message:

func main() {
    lambda.Start(func(ctx context.Context, event events.SNSEvent) error {
        spanCtx, err := spanctx.GetFromSNSEvent(event)
        if err != nil { /* handle error */ }
        ...
    })
}

AWS SQS

SpanContexts are added to the SQS message attributes.

Here's how to add a SpanContext to an SQS message:

params := &sqs.SendMessageInput{
    MessageBody: aws.String(message),
    QueueUrl:    aws.String(queueURL),
}
err := spanctx.AddToSQSMessageInput(span.Context(), params)
if err != nil { /* handle error */ }
resp, err := sqsClient.SendMessage(params)

Here's how to retrieve a SpanContext from an SQS message:

func main() {
    lambda.Start(func(ctx context.Context, event events.SQSEvent) error {
        spanCtx, err := spanctx.GetFromSQSEvent(event)
        ...
    })
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages