-
Notifications
You must be signed in to change notification settings - Fork 436
/
memcache.go
203 lines (178 loc) · 5.95 KB
/
memcache.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
// 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 memcache provides functions to trace the bradfitz/gomemcache package (https://github.com/bradfitz/gomemcache).
//
// `WrapClient` will wrap a memcache `Client` and return a new struct with all
// the same methods, so should be seamless for existing applications. It also
// has an additional `WithContext` method which can be used to connect a span
// to an existing trace.
package memcache // import "gopkg.in/DataDog/dd-trace-go.v1/contrib/bradfitz/gomemcache/memcache"
import (
"context"
"math"
"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"
"gopkg.in/DataDog/dd-trace-go.v1/internal/log"
"gopkg.in/DataDog/dd-trace-go.v1/internal/telemetry"
"github.com/bradfitz/gomemcache/memcache"
)
const componentName = "bradfitz/gomemcache/memcache"
func init() {
telemetry.LoadIntegration(componentName)
tracer.MarkIntegrationImported("github.com/bradfitz/gomemcache")
}
// WrapClient wraps a memcache.Client so that all requests are traced using the
// default tracer with the service name "memcached".
func WrapClient(client *memcache.Client, opts ...ClientOption) *Client {
cfg := new(clientConfig)
defaults(cfg)
for _, opt := range opts {
opt(cfg)
}
log.Debug("contrib/bradfitz/gomemcache/memcache: Wrapping Client: %#v", cfg)
return &Client{
Client: client,
cfg: cfg,
context: context.Background(),
}
}
// A Client is used to trace requests to the memcached server.
type Client struct {
*memcache.Client
cfg *clientConfig
context context.Context
}
// WithContext creates a copy of the Client with the given context.
func (c *Client) WithContext(ctx context.Context) *Client {
// the existing memcache client doesn't support context, but may in the
// future, so we do a runtime check to detect this
mc := c.Client
if wc, ok := (interface{})(c.Client).(interface {
WithContext(context.Context) *memcache.Client
}); ok {
mc = wc.WithContext(ctx)
}
return &Client{
Client: mc,
cfg: c.cfg,
context: ctx,
}
}
// startSpan starts a span from the context set with WithContext.
func (c *Client) startSpan(resourceName string) ddtrace.Span {
opts := []ddtrace.StartSpanOption{
tracer.SpanType(ext.SpanTypeMemcached),
tracer.ServiceName(c.cfg.serviceName),
tracer.ResourceName(resourceName),
tracer.Tag(ext.Component, componentName),
tracer.Tag(ext.SpanKind, ext.SpanKindClient),
tracer.Tag(ext.DBSystem, ext.DBSystemMemcached),
}
if !math.IsNaN(c.cfg.analyticsRate) {
opts = append(opts, tracer.Tag(ext.EventSampleRate, c.cfg.analyticsRate))
}
span, _ := tracer.StartSpanFromContext(c.context, c.cfg.operationName, opts...)
return span
}
// wrapped methods:
// Add invokes and traces Client.Add.
func (c *Client) Add(item *memcache.Item) error {
span := c.startSpan("Add")
err := c.Client.Add(item)
span.Finish(tracer.WithError(err))
return err
}
// Append invokes and traces Client.Append.
func (c *Client) Append(item *memcache.Item) error {
span := c.startSpan("Append")
err := c.Client.Append(item)
span.Finish(tracer.WithError(err))
return err
}
// CompareAndSwap invokes and traces Client.CompareAndSwap.
func (c *Client) CompareAndSwap(item *memcache.Item) error {
span := c.startSpan("CompareAndSwap")
err := c.Client.CompareAndSwap(item)
span.Finish(tracer.WithError(err))
return err
}
// Decrement invokes and traces Client.Decrement.
func (c *Client) Decrement(key string, delta uint64) (newValue uint64, err error) {
span := c.startSpan("Decrement")
newValue, err = c.Client.Decrement(key, delta)
span.Finish(tracer.WithError(err))
return newValue, err
}
// Delete invokes and traces Client.Delete.
func (c *Client) Delete(key string) error {
span := c.startSpan("Delete")
err := c.Client.Delete(key)
span.Finish(tracer.WithError(err))
return err
}
// DeleteAll invokes and traces Client.DeleteAll.
func (c *Client) DeleteAll() error {
span := c.startSpan("DeleteAll")
err := c.Client.DeleteAll()
span.Finish(tracer.WithError(err))
return err
}
// FlushAll invokes and traces Client.FlushAll.
func (c *Client) FlushAll() error {
span := c.startSpan("FlushAll")
err := c.Client.FlushAll()
span.Finish(tracer.WithError(err))
return err
}
// Get invokes and traces Client.Get.
func (c *Client) Get(key string) (item *memcache.Item, err error) {
span := c.startSpan("Get")
item, err = c.Client.Get(key)
span.Finish(tracer.WithError(err))
return item, err
}
// GetMulti invokes and traces Client.GetMulti.
func (c *Client) GetMulti(keys []string) (map[string]*memcache.Item, error) {
span := c.startSpan("GetMulti")
items, err := c.Client.GetMulti(keys)
span.Finish(tracer.WithError(err))
return items, err
}
// Increment invokes and traces Client.Increment.
func (c *Client) Increment(key string, delta uint64) (newValue uint64, err error) {
span := c.startSpan("Increment")
newValue, err = c.Client.Increment(key, delta)
span.Finish(tracer.WithError(err))
return newValue, err
}
// Prepend invokes and traces Client.Prepend.
func (c *Client) Prepend(item *memcache.Item) error {
span := c.startSpan("Prepend")
err := c.Client.Prepend(item)
span.Finish(tracer.WithError(err))
return err
}
// Replace invokes and traces Client.Replace.
func (c *Client) Replace(item *memcache.Item) error {
span := c.startSpan("Replace")
err := c.Client.Replace(item)
span.Finish(tracer.WithError(err))
return err
}
// Set invokes and traces Client.Set.
func (c *Client) Set(item *memcache.Item) error {
span := c.startSpan("Set")
err := c.Client.Set(item)
span.Finish(tracer.WithError(err))
return err
}
// Touch invokes and traces Client.Touch.
func (c *Client) Touch(key string, seconds int32) error {
span := c.startSpan("Touch")
err := c.Client.Touch(key, seconds)
span.Finish(tracer.WithError(err))
return err
}