-
Notifications
You must be signed in to change notification settings - Fork 39
/
collector.go
137 lines (111 loc) · 3.87 KB
/
collector.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
// (c) Copyright IBM Corp. 2023
package instana
import (
"context"
ot "github.com/opentracing/opentracing-go"
)
// TracerLogger represents the Instana Go collector and is composed by a tracer, a logger and a reference to the legacy sensor.
type TracerLogger interface {
Tracer
LeveledLogger
LegacySensor() *Sensor
SensorLogger
}
// Collector is used to inject tracing information into requests
type Collector struct {
t Tracer
LeveledLogger
*Sensor
}
var _ TracerLogger = (*Collector)(nil)
// InitCollector creates a new [Collector]
func InitCollector(opts *Options) TracerLogger {
// if instana.C is already an instance of Collector, we just return
if _, ok := C.(*Collector); ok {
C.Warn("InitCollector was previously called. instana.C is reused")
return C
}
if opts == nil {
opts = &Options{
Recorder: NewRecorder(),
}
}
if opts.Recorder == nil {
opts.Recorder = NewRecorder()
}
StartMetrics(opts)
tracer := &tracerS{
recorder: opts.Recorder,
}
C = &Collector{
t: tracer,
LeveledLogger: defaultLogger,
Sensor: NewSensorWithTracer(tracer),
}
return C
}
// Extract() returns a SpanContext instance given `format` and `carrier`. It matches [opentracing.Tracer.Extract].
func (c *Collector) Extract(format interface{}, carrier interface{}) (ot.SpanContext, error) {
return c.t.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 (c *Collector) Inject(sm ot.SpanContext, format interface{}, carrier interface{}) error {
return c.t.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 (c *Collector) StartSpan(operationName string, opts ...ot.StartSpanOption) ot.Span {
return c.t.StartSpan(operationName, opts...)
}
// StartSpanWithOptions creates and starts a span by setting Instana relevant data within the span.
// It matches [instana.Tracer.StartSpanWithOptions].
func (c *Collector) StartSpanWithOptions(operationName string, opts ot.StartSpanOptions) ot.Span {
return c.t.StartSpanWithOptions(operationName, opts)
}
// Options gets the current tracer options
// It matches [instana.Tracer.Options].
func (c *Collector) Options() TracerOptions {
return c.t.Options()
}
// Flush sends all finished spans to the agent
// It matches [instana.Tracer.Flush].
func (c *Collector) Flush(ctx context.Context) error {
return c.t.Flush(ctx)
}
// Debug logs a debug message by calling [LeveledLogger] underneath
func (c *Collector) Debug(v ...interface{}) {
c.LeveledLogger.Debug(v...)
}
// Info logs an info message by calling [LeveledLogger] underneath
func (c *Collector) Info(v ...interface{}) {
c.LeveledLogger.Info(v...)
}
// Warn logs a warning message by calling [LeveledLogger] underneath
func (c *Collector) Warn(v ...interface{}) {
c.LeveledLogger.Warn(v...)
}
// Error logs a error message by calling [LeveledLogger] underneath
func (c *Collector) Error(v ...interface{}) {
c.LeveledLogger.Error(v...)
}
// SetLogger changes the [Sensor] logger and [LeveledLogger] both to satisfy [TracerLogger]
func (c *Collector) SetLogger(l LeveledLogger) {
c.Sensor.SetLogger(l)
c.LeveledLogger = l
}
// LegacySensor returns a reference to [Sensor] that can be used for old instrumentations that still require it.
//
// Example:
//
// // Instrumenting HTTP incoming calls
// c := instana.InitCollector("my-service")
// http.HandleFunc("/", instana.TracingNamedHandlerFunc(c.LegacySensor(), "", "/{name}", handle))
func (c *Collector) LegacySensor() *Sensor {
return c.Sensor
}