2021-05-20 04:16:55 -05:00
|
|
|
package httpclientprovider
|
|
|
|
|
|
|
|
import (
|
2022-01-20 04:10:12 -06:00
|
|
|
"fmt"
|
2021-05-20 04:16:55 -05:00
|
|
|
"net/http"
|
2022-01-20 04:10:12 -06:00
|
|
|
"strconv"
|
2021-05-20 04:16:55 -05:00
|
|
|
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2022-01-20 04:10:12 -06:00
|
|
|
"github.com/grafana/grafana/pkg/infra/tracing"
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/codes"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2021-05-20 04:16:55 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
TracingMiddlewareName = "tracing"
|
|
|
|
httpContentLengthTagKey = "http.content_length"
|
|
|
|
)
|
|
|
|
|
2022-01-20 04:10:12 -06:00
|
|
|
func TracingMiddleware(logger log.Logger, tracer tracing.Tracer) httpclient.Middleware {
|
2021-05-20 04:16:55 -05:00
|
|
|
return httpclient.NamedMiddlewareFunc(TracingMiddlewareName, func(opts httpclient.Options, next http.RoundTripper) http.RoundTripper {
|
|
|
|
return httpclient.RoundTripperFunc(func(req *http.Request) (*http.Response, error) {
|
2022-01-20 04:10:12 -06:00
|
|
|
ctx, span := tracer.Start(req.Context(), "HTTP Outgoing Request", trace.WithSpanKind(trace.SpanKindClient))
|
|
|
|
defer span.End()
|
2021-05-20 04:16:55 -05:00
|
|
|
|
|
|
|
req = req.WithContext(ctx)
|
|
|
|
for k, v := range opts.Labels {
|
2022-01-20 04:10:12 -06:00
|
|
|
span.SetAttributes(k, v, attribute.Key(k).String(v))
|
2021-05-20 04:16:55 -05:00
|
|
|
}
|
|
|
|
|
2022-01-20 04:10:12 -06:00
|
|
|
tracer.Inject(ctx, req.Header, span)
|
2021-05-20 04:16:55 -05:00
|
|
|
res, err := next.RoundTrip(req)
|
|
|
|
|
2022-01-20 04:10:12 -06:00
|
|
|
span.SetAttributes("http.url", req.URL.String(), attribute.String("http.url", req.URL.String()))
|
|
|
|
span.SetAttributes("http.method", req.Method, attribute.String("http.method", req.Method))
|
|
|
|
// ext.SpanKind.Set(span, ext.SpanKindRPCClientEnum)
|
2021-05-20 04:16:55 -05:00
|
|
|
|
|
|
|
if err != nil {
|
2022-01-20 04:10:12 -06:00
|
|
|
span.RecordError(err)
|
2021-05-20 04:16:55 -05:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if res != nil {
|
|
|
|
// we avoid measuring contentlength less than zero because it indicates
|
|
|
|
// that the content size is unknown. https://godoc.org/github.com/badu/http#Response
|
|
|
|
if res.ContentLength > 0 {
|
2022-01-20 04:10:12 -06:00
|
|
|
span.SetAttributes(httpContentLengthTagKey, res.ContentLength, attribute.Key(httpContentLengthTagKey).Int64(res.ContentLength))
|
2021-05-20 04:16:55 -05:00
|
|
|
}
|
|
|
|
|
2022-01-20 04:10:12 -06:00
|
|
|
span.SetAttributes("http.status_code", res.StatusCode, attribute.Int("http.status_code", res.StatusCode))
|
2021-05-20 04:16:55 -05:00
|
|
|
if res.StatusCode >= 400 {
|
2022-01-20 04:10:12 -06:00
|
|
|
span.SetStatus(codes.Error, fmt.Sprintf("error with HTTP status code %s", strconv.Itoa(res.StatusCode)))
|
2021-05-20 04:16:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, err
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|