forked from instana/go-sensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapters.go
265 lines (218 loc) · 8.5 KB
/
adapters.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// (c) Copyright IBM Corp. 2021
// (c) Copyright Instana Inc. 2019
package instana
import (
"context"
"net/http"
"runtime"
"github.com/opentracing/opentracing-go"
ot "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
otlog "github.com/opentracing/opentracing-go/log"
)
var _ TracerLogger = (*Sensor)(nil)
type SensorLogger interface {
Tracer() ot.Tracer
Logger() LeveledLogger
SetLogger(l LeveledLogger)
}
// SpanSensitiveFunc is a function executed within a span context
//
// Deprecated: use instana.ContextWithSpan() and instana.SpanFromContext() to inject and retrieve spans
type SpanSensitiveFunc func(span ot.Span)
// ContextSensitiveFunc is a SpanSensitiveFunc that also takes context.Context
//
// Deprecated: use instana.ContextWithSpan() and instana.SpanFromContext() to inject and retrieve spans
type ContextSensitiveFunc func(span ot.Span, ctx context.Context)
// Tracer extends the opentracing.Tracer interface
type Tracer interface {
opentracing.Tracer
// Options gets the current tracer options
Options() TracerOptions
// Flush sends all finished spans to the agent
Flush(context.Context) error
// StartSpanWithOptions starts a span with the given options and return the span reference
StartSpanWithOptions(string, ot.StartSpanOptions) ot.Span
}
// Sensor is used to inject tracing information into requests
type Sensor struct {
tracer ot.Tracer
logger LeveledLogger
}
// NewSensor creates a new [Sensor]
func NewSensor(serviceName string) *Sensor {
return NewSensorWithTracer(NewTracerWithOptions(
&Options{
Service: serviceName,
},
))
}
// NewSensorWithTracer returns a new [Sensor] that uses provided tracer to report spans
func NewSensorWithTracer(tracer ot.Tracer) *Sensor {
return &Sensor{
tracer: tracer,
logger: defaultLogger,
}
}
// Tracer returns the tracer instance for this sensor
func (s *Sensor) Tracer() ot.Tracer {
return s.tracer
}
// Logger returns the logger instance for this sensor
func (s *Sensor) Logger() LeveledLogger {
return s.logger
}
// SetLogger sets the logger for this sensor
func (s *Sensor) SetLogger(l LeveledLogger) {
s.logger = l
}
// TraceHandler is similar to TracingHandler in regards, that it wraps an existing http.HandlerFunc
// into a named instance to support capturing tracing information and data. The returned values are
// compatible with handler registration methods, e.g. http.Handle()
//
// Deprecated: please use instana.TracingHandlerFunc() instead
func (s *Sensor) TraceHandler(name, pattern string, handler http.HandlerFunc) (string, http.HandlerFunc) {
return pattern, s.TracingHandler(name, handler)
}
// TracingHandler wraps an existing http.HandlerFunc into a named instance to support capturing tracing
// information and response data
//
// Deprecated: please use instana.TracingHandlerFunc() instead
func (s *Sensor) TracingHandler(name string, handler http.HandlerFunc) http.HandlerFunc {
return TracingHandlerFunc(s, name, handler)
}
// TracingHttpRequest wraps an existing http.Request instance into a named instance to inject tracing and span
// header information into the actual HTTP wire transfer
//
// Deprecated: please use instana.RoundTripper() instead
func (s *Sensor) TracingHttpRequest(name string, parent, req *http.Request, client http.Client) (*http.Response, error) {
client.Transport = RoundTripper(s, client.Transport)
return client.Do(req.WithContext(context.Background()))
}
// WithTracingSpan takes the given SpanSensitiveFunc and executes it under the scope of a child span, which is
// injected as an argument when calling the function. It uses the name of the caller as a span operation name
// unless a non-empty value is provided
//
// Deprecated: please use instana.TracingHandlerFunc() to instrument an HTTP handler
func (s *Sensor) WithTracingSpan(operationName string, w http.ResponseWriter, req *http.Request, f SpanSensitiveFunc) {
if operationName == "" {
pc, _, _, _ := runtime.Caller(1)
f := runtime.FuncForPC(pc)
operationName = f.Name()
}
opts := []ot.StartSpanOption{
ext.SpanKindRPCServer,
ot.Tags{
string(ext.PeerHostname): req.Host,
string(ext.HTTPUrl): req.URL.Path,
string(ext.HTTPMethod): req.Method,
},
}
wireContext, err := s.tracer.Extract(ot.HTTPHeaders, ot.HTTPHeadersCarrier(req.Header))
switch err {
case nil:
opts = append(opts, ext.RPCServerOption(wireContext))
case ot.ErrSpanContextNotFound:
s.Logger().Debug("no span context provided with ", req.Method, " ", req.URL.Path)
case ot.ErrUnsupportedFormat:
s.Logger().Info("unsupported span context format provided with ", req.Method, " ", req.URL.Path)
default:
s.Logger().Warn("failed to extract span context from the request:", err)
}
if ps, ok := SpanFromContext(req.Context()); ok {
opts = append(opts, ot.ChildOf(ps.Context()))
}
span := s.tracer.StartSpan(operationName, opts...)
defer span.Finish()
defer func() {
// Capture outgoing headers
s.tracer.Inject(span.Context(), ot.HTTPHeaders, ot.HTTPHeadersCarrier(w.Header()))
// Be sure to capture any kind of panic / error
if err := recover(); err != nil {
if e, ok := err.(error); ok {
span.LogFields(otlog.Error(e))
} else {
span.LogFields(otlog.Object("error", err))
}
// re-throw the panic
panic(err)
}
}()
f(span)
}
// WithTracingContext executes the given ContextSensitiveFunc and executes it under the scope of a newly created context.Context,
// that provides access to the parent span as 'parentSpan'.
//
// Deprecated: please use instana.TracingHandlerFunc() to instrument an HTTP handler
func (s *Sensor) WithTracingContext(name string, w http.ResponseWriter, req *http.Request, f ContextSensitiveFunc) {
s.WithTracingSpan(name, w, req, func(span ot.Span) {
f(span, ContextWithSpan(req.Context(), span))
})
}
// Compliance with TracerLogger
// Extract() returns a SpanContext instance given `format` and `carrier`. It matches [opentracing.Tracer.Extract].
func (s *Sensor) Extract(format interface{}, carrier interface{}) (ot.SpanContext, error) {
return s.tracer.Extract(format, carrier)
}
// Inject() takes the `sm` SpanContext instance and injects it for
// propagation within `carrier`. The actual type of `carrier` depends on
// the value of `format`. It matches [opentracing.Tracer.Inject]
func (s *Sensor) Inject(sm ot.SpanContext, format interface{}, carrier interface{}) error {
return s.tracer.Inject(sm, format, carrier)
}
// Create, start, and return a new Span with the given `operationName` and
// incorporate the given StartSpanOption `opts`. (Note that `opts` borrows
// from the "functional options" pattern, per
// http://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis)
//
// It matches [opentracing.Tracer.StartSpan].
func (s *Sensor) StartSpan(operationName string, opts ...ot.StartSpanOption) ot.Span {
return s.tracer.StartSpan(operationName, opts...)
}
// StartSpanWithOptions creates and starts a span by setting Instana relevant data within the span.
// It matches [instana.Tracer.StartSpanWithOptions].
func (s *Sensor) StartSpanWithOptions(operationName string, opts ot.StartSpanOptions) ot.Span {
if t, ok := s.tracer.(Tracer); ok {
return t.StartSpanWithOptions(operationName, opts)
}
s.logger.Warn("Sensor.StartSpanWithOptions() not implemented by interface: ", s.tracer, " - returning nil")
return nil
}
// Options gets the current tracer options
// It matches [instana.Tracer.Options].
func (s *Sensor) Options() TracerOptions {
if t, ok := s.tracer.(Tracer); ok {
return t.Options()
}
s.logger.Warn("Sensor.Options() not implemented by interface: ", s.tracer, " - returning DefaultTracerOptions()")
return DefaultTracerOptions()
}
// Flush sends all finished spans to the agent
// It matches [instana.Tracer.Flush].
func (s *Sensor) Flush(ctx context.Context) error {
if t, ok := s.tracer.(Tracer); ok {
return t.Flush(ctx)
}
s.logger.Warn("Sensor.Flush() not implemented by interface: ", s.tracer, " - returning nil")
return nil
}
// Debug logs a debug message by calling [LeveledLogger] underneath
func (s *Sensor) Debug(v ...interface{}) {
s.logger.Debug(v...)
}
// Info logs an info message by calling [LeveledLogger] underneath
func (s *Sensor) Info(v ...interface{}) {
s.logger.Info(v...)
}
// Warn logs a warning message by calling [LeveledLogger] underneath
func (s *Sensor) Warn(v ...interface{}) {
s.logger.Warn(v...)
}
// Error logs a error message by calling [LeveledLogger] underneath
func (s *Sensor) Error(v ...interface{}) {
s.logger.Error(v...)
}
// LegacySensor returns a reference to [Sensor].
func (s *Sensor) LegacySensor() *Sensor {
return s
}