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?