-
Notifications
You must be signed in to change notification settings - Fork 37
/
adapters.go
170 lines (143 loc) · 5.44 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
// (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"
)
// 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
}
// Sensor is used to inject tracing information into requests
type Sensor struct {
tracer ot.Tracer
logger LeveledLogger
}
// NewSensor creates a new instana.Sensor
func NewSensor(serviceName string) *Sensor {
return NewSensorWithTracer(NewTracerWithOptions(
&Options{
Service: serviceName,
},
))
}
// NewSensorWithTracer returns a new instana.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))
})
}