forked from olivere/elastic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transport.go
81 lines (69 loc) · 2.36 KB
/
transport.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright 2012-present Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.
package opentelemetry
import (
"net/http"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
)
// Transport for tracing Elastic operations.
type Transport struct {
rt http.RoundTripper
}
// Option signature for specifying options, e.g. WithRoundTripper.
type Option func(t *Transport)
// WithRoundTripper specifies the http.RoundTripper to call
// next after this transport. If it is nil (default), the
// transport will use http.DefaultTransport.
func WithRoundTripper(rt http.RoundTripper) Option {
return func(t *Transport) {
t.rt = rt
}
}
// NewTransport specifies a transport that will trace Elastic
// and report back via OpenTracing.
func NewTransport(opts ...Option) *Transport {
t := &Transport{}
for _, o := range opts {
o(t)
}
return t
}
// RoundTrip captures the request and starts an OpenTracing span
// for Elastic PerformRequest operation.
func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
ctx, span := otel.Tracer("Elastic").Start(req.Context(), "PerformRequest")
defer span.End()
req = req.WithContext(ctx)
// See General (https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/span-general.md)
// and HTTP (https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md)
span.SetAttributes(
attribute.String("code.namespace", "github.com/DaniilD/elastic/v7"),
attribute.String("code.function", "PerformRequest"),
attribute.String("http.url", req.URL.Redacted()),
attribute.String("http.method", req.Method),
attribute.String("http.scheme", req.URL.Scheme),
attribute.String("http.host", req.URL.Hostname()),
attribute.String("http.path", req.URL.Path),
attribute.String("http.user_agent", req.UserAgent()),
)
var (
resp *http.Response
err error
)
if t.rt != nil {
resp, err = t.rt.RoundTrip(req)
} else {
resp, err = http.DefaultTransport.RoundTrip(req)
}
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
}
if resp != nil {
span.SetAttributes(attribute.Int64("http.status_code", int64(resp.StatusCode)))
}
return resp, err
}