/
twirp.go
227 lines (204 loc) · 6.73 KB
/
twirp.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016 Datadog, Inc.
// Package twirp provides tracing functions for tracing clients and servers generated
// by the twirp framework (https://github.com/twitchtv/twirp).
package twirp // import "gopkg.in/DataDog/dd-trace-go.v1/contrib/twitchtv/twirp"
import (
"context"
"fmt"
"math"
"net/http"
"strconv"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
"gopkg.in/DataDog/dd-trace-go.v1/internal/log"
"github.com/twitchtv/twirp"
)
type (
twirpErrorKey struct{}
twirpSpanKey struct{}
)
// HTTPClient is duplicated from twirp's generated service code.
// It is declared in this package so that the client can be wrapped
// to initiate traces.
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}
type wrappedClient struct {
c HTTPClient
cfg *config
}
// WrapClient wraps an HTTPClient to add distributed tracing to its requests.
func WrapClient(c HTTPClient, opts ...Option) HTTPClient {
cfg := new(config)
defaults(cfg)
for _, fn := range opts {
fn(cfg)
}
log.Debug("contrib/twitchtv/twirp: Wrapping Client: %#v", cfg)
return &wrappedClient{c: c, cfg: cfg}
}
func (wc *wrappedClient) Do(req *http.Request) (*http.Response, error) {
opts := []tracer.StartSpanOption{
tracer.SpanType(ext.SpanTypeHTTP),
tracer.ServiceName(wc.cfg.clientServiceName()),
tracer.Tag(ext.HTTPMethod, req.Method),
tracer.Tag(ext.HTTPURL, req.URL.Path),
}
ctx := req.Context()
if pkg, ok := twirp.PackageName(ctx); ok {
opts = append(opts, tracer.Tag("twirp.package", pkg))
}
if svc, ok := twirp.ServiceName(ctx); ok {
opts = append(opts, tracer.Tag("twirp.service", svc))
}
if method, ok := twirp.MethodName(ctx); ok {
opts = append(opts, tracer.Tag("twirp.method", method))
}
if !math.IsNaN(wc.cfg.analyticsRate) {
opts = append(opts, tracer.Tag(ext.EventSampleRate, wc.cfg.analyticsRate))
}
if spanctx, err := tracer.Extract(tracer.HTTPHeadersCarrier(req.Header)); err == nil {
opts = append(opts, tracer.ChildOf(spanctx))
}
span, ctx := tracer.StartSpanFromContext(req.Context(), "twirp.request", opts...)
defer span.Finish()
err := tracer.Inject(span.Context(), tracer.HTTPHeadersCarrier(req.Header))
if err != nil {
log.Warn("contrib/twitchtv/twirp.wrappedClient: failed to inject http headers: %v\n", err)
}
req = req.WithContext(ctx)
res, err := wc.c.Do(req)
if err != nil {
span.SetTag(ext.Error, err)
} else {
span.SetTag(ext.HTTPCode, strconv.Itoa(res.StatusCode))
// treat 4XX and 5XX as errors for a client
if res.StatusCode >= 400 {
span.SetTag(ext.Error, true)
span.SetTag(ext.ErrorMsg, fmt.Sprintf("%d: %s", res.StatusCode, http.StatusText(res.StatusCode)))
}
}
return res, err
}
// WrapServer wraps an http.Handler to add distributed tracing to a Twirp server.
func WrapServer(h http.Handler, opts ...Option) http.Handler {
cfg := new(config)
defaults(cfg)
for _, fn := range opts {
fn(cfg)
}
log.Debug("contrib/twitchtv/twirp: Wrapping Server: %#v", cfg)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
opts := []tracer.StartSpanOption{
tracer.SpanType(ext.SpanTypeWeb),
tracer.ServiceName(cfg.serverServiceName()),
tracer.Tag(ext.HTTPMethod, r.Method),
tracer.Tag(ext.HTTPURL, r.URL.Path),
tracer.Measured(),
}
if !math.IsNaN(cfg.analyticsRate) {
opts = append(opts, tracer.Tag(ext.EventSampleRate, cfg.analyticsRate))
}
if spanctx, err := tracer.Extract(tracer.HTTPHeadersCarrier(r.Header)); err == nil {
opts = append(opts, tracer.ChildOf(spanctx))
}
span, ctx := tracer.StartSpanFromContext(r.Context(), "twirp.handler", opts...)
defer span.Finish()
r = r.WithContext(ctx)
h.ServeHTTP(w, r)
})
}
// NewServerHooks creates the callback hooks for a twirp server to perform tracing.
// It is used in conjunction with WrapServer.
func NewServerHooks(opts ...Option) *twirp.ServerHooks {
cfg := new(config)
defaults(cfg)
for _, fn := range opts {
fn(cfg)
}
log.Debug("contrib/twitchtv/twirp: Creating Server Hooks: %#v", cfg)
return &twirp.ServerHooks{
RequestReceived: requestReceivedHook(cfg),
RequestRouted: requestRoutedHook(cfg),
ResponsePrepared: responsePreparedHook(cfg),
ResponseSent: responseSentHook(cfg),
Error: errorHook(cfg),
}
}
func spanNameFromContext(ctx context.Context) string {
svc, ok := twirp.ServiceName(ctx)
if !ok {
return "twirp.service"
}
return fmt.Sprintf("twirp.%s", svc)
}
func requestReceivedHook(cfg *config) func(context.Context) (context.Context, error) {
return func(ctx context.Context) (context.Context, error) {
opts := []tracer.StartSpanOption{
tracer.SpanType(ext.SpanTypeWeb),
tracer.ServiceName(cfg.serverServiceName()),
tracer.Measured(),
}
if pkg, ok := twirp.PackageName(ctx); ok {
opts = append(opts, tracer.Tag("twirp.package", pkg))
}
if svc, ok := twirp.ServiceName(ctx); ok {
opts = append(opts, tracer.Tag("twirp.service", svc))
}
if !math.IsNaN(cfg.analyticsRate) {
opts = append(opts, tracer.Tag(ext.EventSampleRate, cfg.analyticsRate))
}
span, ctx := tracer.StartSpanFromContext(ctx, spanNameFromContext(ctx), opts...)
ctx = context.WithValue(ctx, twirpSpanKey{}, span)
return ctx, nil
}
}
func requestRoutedHook(cfg *config) func(context.Context) (context.Context, error) {
return func(ctx context.Context) (context.Context, error) {
maybeSpan := ctx.Value(twirpSpanKey{})
if maybeSpan == nil {
log.Error("contrib/twitchtv/twirp.requestRoutedHook: found no span in context")
return ctx, nil
}
span, ok := maybeSpan.(tracer.Span)
if !ok {
log.Error("contrib/twitchtv/twirp.requestRoutedHook: found invalid span type in context")
return ctx, nil
}
if method, ok := twirp.MethodName(ctx); ok {
span.SetTag(ext.ResourceName, method)
span.SetTag("twirp.method", method)
}
return ctx, nil
}
}
func responsePreparedHook(cfg *config) func(context.Context) context.Context {
return func(ctx context.Context) context.Context {
return ctx
}
}
func responseSentHook(cfg *config) func(context.Context) {
return func(ctx context.Context) {
maybeSpan := ctx.Value(twirpSpanKey{})
if maybeSpan == nil {
return
}
span, ok := maybeSpan.(tracer.Span)
if !ok {
return
}
if sc, ok := twirp.StatusCode(ctx); ok {
span.SetTag(ext.HTTPCode, sc)
}
err, _ := ctx.Value(twirpErrorKey{}).(twirp.Error)
span.Finish(tracer.WithError(err))
}
}
func errorHook(cfg *config) func(context.Context, twirp.Error) context.Context {
return func(ctx context.Context, err twirp.Error) context.Context {
return context.WithValue(ctx, twirpErrorKey{}, err)
}
}