forked from lightstep/opentelemetry-exporter-go
/
lightstep.go
246 lines (207 loc) · 6.66 KB
/
lightstep.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
package lightstep
import (
"context"
"encoding/binary"
"sync"
"time"
"github.com/opentracing/opentracing-go/log"
"github.com/InVisionApp/opentelemetry-go/api/core"
"github.com/InVisionApp/opentelemetry-go/sdk/export/trace"
"github.com/InVisionApp/opentelemetry-go/sdk/resource"
"github.com/opentracing/opentracing-go"
ls "github.com/lightstep/lightstep-tracer-go"
)
// Option struct is used to configre the LightStepExpoter options.
type Option func(*config)
// WithAccessToken sets the LightStep access token used to authenticate and associate data.
// with a LightStep project
func WithAccessToken(accessToken string) Option {
return func(c *config) {
c.options.AccessToken = accessToken
}
}
// WithHost sets the URL hostname for sending data to LightStep satellites.
func WithHost(host string) Option {
return func(c *config) {
c.options.Collector.Host = host
c.options.SystemMetrics.Endpoint.Host = host
}
}
// WithPort sets the URL port for sending data to LightStep satellites.
func WithPort(port int) Option {
return func(c *config) {
c.options.Collector.Port = port
c.options.SystemMetrics.Endpoint.Port = port
}
}
// WithServiceName sets the service name tag used to identify a service in
// the LightStep application.
func WithServiceName(serviceName string) Option {
return func(c *config) {
if c.options.Tags == nil {
c.options.Tags = make(map[string]interface{})
}
c.options.Tags[ls.ComponentNameKey] = serviceName
}
}
// WithServiceVersion sets the service version used to identify a service's
// version in the LightStep application.
func WithServiceVersion(serviceVersion string) Option {
return func(c *config) {
if c.options.Tags == nil {
c.options.Tags = make(map[string]interface{})
}
c.options.Tags[ls.ServiceVersionKey] = serviceVersion
}
}
// WithPlainText indicates if data should be sent in plaintext to the LightStep
// Satelites. Default is false.
func WithPlainText(pt bool) Option {
return func(c *config) {
c.options.Collector.Plaintext = pt
c.options.SystemMetrics.Endpoint.Plaintext = pt
}
}
// WithMaxBufferedSpans sets the maximum number of spans that will be buffered
// before sending them to a collector.
// Should be used to override the `DefaultMaxSpan` value of 1000.
func WithMaxBufferedSpans(value int) Option {
return func(c *config) {
c.options.MaxBufferedSpans = value
}
}
// WithSystemMetricsDisabled determines if system metrics are disabled or not.
// Default is false.
func WithSystemMetricsDisabled(disabled bool) Option {
return func(c *config) {
c.options.SystemMetrics.Disabled = disabled
}
}
// WithSystemMetricTimeout sets the tineout duration for sending metrics
// reports to the LightStep application.
func WithSystemMetricTimeout(timeout time.Duration) Option {
return func(c *config) {
c.options.SystemMetrics.Timeout = timeout
}
}
// WithSystemMetricMeasurementFrequency sets the tineout duration for sending metrics
// reports to the LightStep application.
func WithSystemMetricMeasurementFrequency(frequency time.Duration) Option {
return func(c *config) {
c.options.SystemMetrics.MeasurementFrequency = frequency
}
}
type config struct {
options ls.Options
}
func newConfig(opts ...Option) config {
var c config
var defaultOpts []Option
for _, opt := range append(defaultOpts, opts...) {
opt(&c)
}
return c
}
// Exporter is an implementation of trace.Exporter that sends spans to LightStep.
type Exporter struct {
once sync.Once
tracer ls.Tracer
}
// NewExporter is an implementation of trace.Exporter that sends spans to LightStep.
func NewExporter(opts ...Option) (*Exporter, error) {
c := newConfig(opts...)
tracer := ls.NewTracer(c.options)
checkOptions := tracer.Options()
if err := checkOptions.Validate(); err != nil {
return nil, err
}
return &Exporter{
tracer: tracer,
}, nil
}
// ExportSpan exports an OpenTelementry SpanData object to an OpenTracing Span on the LightStep tracer.
func (e *Exporter) ExportSpan(ctx context.Context, data *trace.SpanData) {
e.tracer.StartSpan(
data.Name,
ls.SetTraceID(convertTraceID(data.SpanContext.TraceID)),
ls.SetSpanID(convertSpanID(data.SpanContext.SpanID)),
ls.SetParentSpanID(convertSpanID(data.ParentSpanID)),
opentracing.StartTime(data.StartTime),
opentracing.Tags(toTags(data.Attributes, data.Resource)),
).FinishWithOptions(
opentracing.FinishOptions{
FinishTime: data.EndTime,
LogRecords: toLogRecords(data.MessageEvents),
},
)
}
var _ trace.SpanSyncer = (*Exporter)(nil)
// Close flushes all spans in the tracer to LightStep and then closes the tracer.
// You should call Close() before your application exits.
func (e *Exporter) Close() {
e.tracer.Close(context.Background())
}
// Flush forces all unflushed to flush.
// This is normally handled by the exporter. However, you may call this to explicitly flush all spans without closing the exporter.
func (e *Exporter) Flush() {
e.tracer.Flush(context.Background())
}
// this replicates StartSpan behavior for testing
func lightStepSpan(data *trace.SpanData) *ls.RawSpan {
spanContext := ls.SpanContext{
TraceID: convertTraceID(data.SpanContext.TraceID),
SpanID: convertSpanID(data.SpanContext.SpanID),
}
tags := toTags(data.Attributes, data.Resource)
lsSpan := &ls.RawSpan{
Context: spanContext,
ParentSpanID: convertSpanID(data.ParentSpanID),
Operation: data.Name,
Start: data.StartTime,
Tags: tags,
Logs: toLogRecords(data.MessageEvents),
}
lsSpan.Duration = data.EndTime.Sub(data.StartTime)
return lsSpan
}
func convertTraceID(id core.TraceID) uint64 {
return binary.BigEndian.Uint64(id[8:])
}
func convertSpanID(id core.SpanID) uint64 {
return binary.BigEndian.Uint64(id[:])
}
func toLogRecords(input []trace.Event) []opentracing.LogRecord {
if len(input) == 0 {
return nil
}
output := make([]opentracing.LogRecord, 0, len(input))
for _, l := range input {
output = append(output, toLogRecord(l))
}
return output
}
func toTags(input []core.KeyValue, resource *resource.Resource) map[string]interface{} {
output := make(map[string]interface{})
for iter := resource.Iter(); iter.Next(); {
kv := iter.Label()
output[string(kv.Key)] = kv.Value.Emit()
}
for _, value := range input {
output[string(value.Key)] = value.Value.AsInterface()
}
return output
}
func toLogRecord(ev trace.Event) opentracing.LogRecord {
return opentracing.LogRecord{
Timestamp: ev.Time,
Fields: toFields(ev.Name, ev.Attributes),
}
}
func toFields(name string, input []core.KeyValue) []log.Field {
output := make([]log.Field, 0, len(input)+1)
output = append(output, log.String("name", name))
for _, value := range input {
output = append(output, log.Object(string(value.Key), value.Value.AsInterface()))
}
return output
}