forked from DataDog/dd-trace-go
/
redis.go
177 lines (158 loc) · 5.09 KB
/
redis.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
// 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 redis provides tracing functions for tracing the go-redis/redis package (https://github.com/go-redis/redis).
// This package supports versions up to go-redis 6.15.
package redis
import (
"bytes"
"context"
"math"
"net"
"strconv"
"strings"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
"github.com/go-redis/redis/v8"
)
type datadogHook struct {
*params
}
// params holds the tracer and a set of parameters which are recorded with every trace.
type params struct {
config *clientConfig
additionalTags []ddtrace.StartSpanOption
}
// NewClient returns a new Client that is traced with the default tracer under
// the service name "redis".
func NewClient(opt *redis.Options, opts ...ClientOption) redis.UniversalClient {
client := redis.NewClient(opt)
WrapClient(client, opts...)
return client
}
// WrapClient adds a hook to the given client that traces with the default tracer under
// the service name "redis".
func WrapClient(client redis.UniversalClient, opts ...ClientOption) {
cfg := new(clientConfig)
defaults(cfg)
for _, fn := range opts {
fn(cfg)
}
hookParams := ¶ms{
additionalTags: additionalTagOptions(client),
config: cfg,
}
client.AddHook(&datadogHook{params: hookParams})
}
type clientOptions interface {
Options() *redis.Options
}
type clusterOptions interface {
Options() *redis.ClusterOptions
}
func additionalTagOptions(client redis.UniversalClient) []ddtrace.StartSpanOption {
additionalTags := []ddtrace.StartSpanOption{}
if clientOptions, ok := client.(clientOptions); ok {
opt := clientOptions.Options()
if opt.Addr == "FailoverClient" {
additionalTags = []ddtrace.StartSpanOption{
tracer.Tag("out.db", strconv.Itoa(opt.DB)),
}
} else {
host, port, err := net.SplitHostPort(opt.Addr)
if err != nil {
host = opt.Addr
port = "6379"
}
additionalTags = []ddtrace.StartSpanOption{
tracer.Tag(ext.TargetHost, host),
tracer.Tag(ext.TargetPort, port),
tracer.Tag("out.db", strconv.Itoa(opt.DB)),
}
}
} else if clientOptions, ok := client.(clusterOptions); ok {
addrs := []string{}
for _, addr := range clientOptions.Options().Addrs {
addrs = append(addrs, addr)
}
additionalTags = []ddtrace.StartSpanOption{
tracer.Tag("addrs", strings.Join(addrs, ", ")),
}
}
return additionalTags
}
func (ddh *datadogHook) BeforeProcess(ctx context.Context, cmd redis.Cmder) (context.Context, error) {
raw := cmd.String()
parts := strings.Split(raw, " ")
length := len(parts) - 1
p := ddh.params
opts := []ddtrace.StartSpanOption{
tracer.SpanType(ext.SpanTypeRedis),
tracer.ServiceName(p.config.serviceName),
tracer.ResourceName(parts[0]),
tracer.Tag("redis.raw_command", raw),
tracer.Tag("redis.args_length", strconv.Itoa(length)),
}
opts = append(opts, ddh.additionalTags...)
if !math.IsNaN(p.config.analyticsRate) {
opts = append(opts, tracer.Tag(ext.EventSampleRate, p.config.analyticsRate))
}
_, ctx = tracer.StartSpanFromContext(ctx, "redis.command", opts...)
return ctx, nil
}
func (ddh *datadogHook) AfterProcess(ctx context.Context, cmd redis.Cmder) error {
var span tracer.Span
span, _ = tracer.SpanFromContext(ctx)
var finishOpts []ddtrace.FinishOption
errRedis := cmd.Err()
if errRedis != redis.Nil {
finishOpts = append(finishOpts, tracer.WithError(errRedis))
}
span.Finish(finishOpts...)
return nil
}
func (ddh *datadogHook) BeforeProcessPipeline(ctx context.Context, cmds []redis.Cmder) (context.Context, error) {
raw := commandsToString(cmds)
parts := strings.Split(raw, " ")
length := len(parts) - 1
p := ddh.params
opts := []ddtrace.StartSpanOption{
tracer.SpanType(ext.SpanTypeRedis),
tracer.ServiceName(p.config.serviceName),
tracer.ResourceName(parts[0]),
tracer.Tag("redis.raw_command", raw),
tracer.Tag("redis.args_length", strconv.Itoa(length)),
tracer.Tag(ext.ResourceName, raw),
tracer.Tag("redis.pipeline_length", strconv.Itoa(len(cmds))),
}
opts = append(opts, ddh.additionalTags...)
if !math.IsNaN(p.config.analyticsRate) {
opts = append(opts, tracer.Tag(ext.EventSampleRate, p.config.analyticsRate))
}
_, ctx = tracer.StartSpanFromContext(ctx, "redis.command", opts...)
return ctx, nil
}
func (ddh *datadogHook) AfterProcessPipeline(ctx context.Context, cmds []redis.Cmder) error {
var span tracer.Span
span, _ = tracer.SpanFromContext(ctx)
var finishOpts []ddtrace.FinishOption
for _, cmd := range cmds {
errCmd := cmd.Err()
if errCmd != redis.Nil {
finishOpts = append(finishOpts, tracer.WithError(errCmd))
}
}
span.Finish(finishOpts...)
return nil
}
// commandsToString returns a string representation of a slice of redis Commands, separated by newlines.
func commandsToString(cmds []redis.Cmder) string {
var b bytes.Buffer
for _, cmd := range cmds {
b.WriteString(cmd.String())
b.WriteString("\n")
}
return b.String()
}