lewtur
lewtur

Reputation: 643

How to check if AWS X-Ray has been configured

We have an AWS Lambda running in Go, and upon initialisation runs the following to initialise AWS X-Ray

err := xray.Configure(xray.Config{
  LogLevel:       "info",
  ServiceVersion: "1.2.3",
})

In a seperate repository, we have a utils repository which exposes a HTTP library for our internal stuff. This is imported as a git submodule to all other Lambdas. The code is as follows:

ctx, subseg := xray.BeginSubsegment(incomingContext, "Outbound HTTP call")
client := xray.Client(&http.Client{Transport: tr})
// further down
client.Do(req)
// finally
subseg.Close(resp)

This works as expected when deployed on AWS, producing a nice graph.

The problem is running unit tests on the utils repository. In the context of that repository alone, X-Ray has not been configured, so on the BeginSubsegment call I get a panic:

panic: failed to begin subsegment named 'Outbound HTTP call': segment cannot be found.

I want to gracefully handle the case when X-Ray has not been configured, log it, and carry on execution regardless.

How can I ensure to properly error handle the call to BeginSubsegment when it does not return an error object?

Upvotes: 2

Views: 514

Answers (1)

Bhautik Pipaliya
Bhautik Pipaliya

Reputation: 46

In the case of lambda this code executes without any panic is because lambda creates a facade segment and then your code will be creating subsegments. In the non lambda environment you will have to create a segment first before creating a subsegment. If you don't then it will generate a panic. Now, If you want to log this panic and continue executing your unit tests then I would recommend you to set AWS_XRAY_CONTEXT_MISSING environment variable to LOG_ERROR. It will basically log your panic and continue executing your unit tests.

Upvotes: 2

Related Questions