epsagon / epsagon-go

Automated tracing library for Go 1.x ⚡️
https://epsagon.com
MIT License
28 stars 11 forks source link

Wrappable TracingTransport; lazy tracer setting in RoundTrip #58

Closed mgarski closed 3 years ago

mgarski commented 3 years ago

Just wrapping the DefaultTransport in the RoundTripper implementation is not very flexible if the http.Client required has it's own custom transport. A good example of this is in calling the AWS Elasticsearch service with signed requests which uses a RoundTripper to sign the request. Kicking myself for not doing this in my initial PR.

Also added a check on the tracer being nil in the RoundTrip method, and setting it to the global tracer if it is. We initialize our dependencies in main and inject them into the handler before calling WrapLambdaHandler, and in doing so the tracer is nil in the TracingTransport as the global tracer has not yet been created. I'm not sure if this solution is the best approach for that and am open to other approaches. Here is an example of the approach we use in our main() functions:

func main() {
    logger := logger.NewLogrus(logger.LogrusConfigEnv("payment-helpers", AppVersion, logrus.New()))

    key := os.Getenv("STRIPE_SECRET_KEY")
    h := handler{stripe: stripe.NewClient(key)}
    a := apigw.NewAPIGWHandler(h, logger)

    lambda.Start(
        epsagon.WrapLambdaHandler(
            instrumentation.EpsagonConfig("payment-helpers"),
            a.Handle))
}

The http.Client with TracingTransport is created in the stripe.NewClient function, which occurs before the global tracer is created.