forked from krakend/krakend-opencensus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
56 lines (46 loc) · 1.73 KB
/
http.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
package opencensus
import (
"context"
"net/http"
"github.com/luraproject/lura/v2/config"
transport "github.com/luraproject/lura/v2/transport/http/client"
"go.opencensus.io/plugin/ochttp"
"go.opencensus.io/tag"
"go.opencensus.io/trace"
)
var defaultClient = &http.Client{Transport: &ochttp.Transport{}}
func NewHTTPClient(ctx context.Context) *http.Client {
if !IsBackendEnabled() {
return transport.NewHTTPClient(ctx)
}
return defaultClient
}
func HTTPRequestExecutor(clientFactory transport.HTTPClientFactory) transport.HTTPRequestExecutor {
return HTTPRequestExecutorFromConfig(clientFactory, nil)
}
func HTTPRequestExecutorFromConfig(clientFactory transport.HTTPClientFactory, cfg *config.Backend) transport.HTTPRequestExecutor {
if !IsBackendEnabled() {
return transport.DefaultHTTPRequestExecutor(clientFactory)
}
pathExtractor := GetAggregatedPathForBackendMetrics(cfg)
return func(ctx context.Context, req *http.Request) (*http.Response, error) {
httpClient := clientFactory(ctx)
if _, ok := httpClient.Transport.(*Transport); ok {
return httpClient.Do(req.WithContext(trace.NewContext(ctx, fromContext(ctx))))
}
c := &http.Client{
Transport: &Transport{
Base: httpClient.Transport,
tags: []tagGenerator{
func(r *http.Request) tag.Mutator { return tag.Upsert(ochttp.KeyClientHost, req.Host) },
func(r *http.Request) tag.Mutator { return tag.Upsert(ochttp.KeyClientPath, pathExtractor(r)) },
func(r *http.Request) tag.Mutator { return tag.Upsert(ochttp.KeyClientMethod, req.Method) },
},
},
CheckRedirect: httpClient.CheckRedirect,
Jar: httpClient.Jar,
Timeout: httpClient.Timeout,
}
return c.Do(req.WithContext(trace.NewContext(ctx, fromContext(ctx))))
}
}