/
gclient_tracing.go
104 lines (91 loc) · 3.54 KB
/
gclient_tracing.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/joy12825/gf.
package gclient
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptrace"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
"github.com/joy12825/gf"
"github.com/joy12825/gf/internal/httputil"
"github.com/joy12825/gf/internal/utils"
"github.com/joy12825/gf/net/gtrace"
"github.com/joy12825/gf/os/gctx"
"github.com/joy12825/gf/text/gstr"
"github.com/joy12825/gf/util/gconv"
)
const (
tracingInstrumentName = "github.com/joy12825/gf/net/gclient.Client"
tracingAttrHttpAddressRemote = "http.address.remote"
tracingAttrHttpAddressLocal = "http.address.local"
tracingAttrHttpDnsStart = "http.dns.start"
tracingAttrHttpDnsDone = "http.dns.done"
tracingAttrHttpConnectStart = "http.connect.start"
tracingAttrHttpConnectDone = "http.connect.done"
tracingEventHttpRequest = "http.request"
tracingEventHttpRequestHeaders = "http.request.headers"
tracingEventHttpRequestBaggage = "http.request.baggage"
tracingEventHttpRequestBody = "http.request.body"
tracingEventHttpResponse = "http.response"
tracingEventHttpResponseHeaders = "http.response.headers"
tracingEventHttpResponseBody = "http.response.body"
tracingMiddlewareHandled gctx.StrKey = `MiddlewareClientTracingHandled`
)
// internalMiddlewareTracing is a client middleware that enables tracing feature using standards of OpenTelemetry.
func internalMiddlewareTracing(c *Client, r *http.Request) (response *Response, err error) {
var ctx = r.Context()
// Mark this request is handled by server tracing middleware,
// to avoid repeated handling by the same middleware.
if ctx.Value(tracingMiddlewareHandled) != nil {
return c.Next(r)
}
ctx = context.WithValue(ctx, tracingMiddlewareHandled, 1)
tr := otel.GetTracerProvider().Tracer(
tracingInstrumentName,
trace.WithInstrumentationVersion(gf.VERSION),
)
ctx, span := tr.Start(ctx, r.URL.String(), trace.WithSpanKind(trace.SpanKindClient))
defer span.End()
span.SetAttributes(gtrace.CommonLabels()...)
// Inject tracing content into http header.
otel.GetTextMapPropagator().Inject(ctx, propagation.HeaderCarrier(r.Header))
// If it is now using default trace provider, it then does no complex tracing jobs.
if gtrace.IsUsingDefaultProvider() {
response, err = c.Next(r)
return
}
// Continue client handler executing.
response, err = c.Next(
r.WithContext(
httptrace.WithClientTrace(
ctx, newClientTrace(ctx, span, r),
),
),
)
if err != nil {
span.SetStatus(codes.Error, fmt.Sprintf(`%+v`, err))
}
if response == nil || response.Response == nil {
return
}
reqBodyContentBytes, _ := ioutil.ReadAll(response.Body)
response.Body = utils.NewReadCloser(reqBodyContentBytes, false)
span.AddEvent(tracingEventHttpResponse, trace.WithAttributes(
attribute.String(tracingEventHttpResponseHeaders, gconv.String(httputil.HeaderToMap(response.Header))),
attribute.String(tracingEventHttpResponseBody, gstr.StrLimit(
string(reqBodyContentBytes),
gtrace.MaxContentLogSize(),
"...",
)),
))
return
}