-
Notifications
You must be signed in to change notification settings - Fork 47
/
config.go
131 lines (106 loc) · 3.75 KB
/
config.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
// Copyright Sam Xie
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package otelsql
import (
"context"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
metricglobal "go.opentelemetry.io/otel/metric/global"
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
"go.opentelemetry.io/otel/trace"
)
const (
instrumentationName = "github.com/XSAM/otelsql"
)
var (
connectionStatusKey = attribute.Key("status")
queryStatusKey = attribute.Key("status")
queryMethodKey = attribute.Key("method")
)
// SpanNameFormatter is an interface that used to format span names.
type SpanNameFormatter interface {
Format(ctx context.Context, method Method, query string) string
}
type config struct {
TracerProvider trace.TracerProvider
Tracer trace.Tracer
MeterProvider metric.MeterProvider
Meter metric.Meter
Instruments *instruments
SpanOptions SpanOptions
// Attributes will be set to each span.
Attributes []attribute.KeyValue
// SpanNameFormatter will be called to produce span's name.
// Default use method as span name
SpanNameFormatter SpanNameFormatter
}
// SpanOptions holds configuration of tracing span to decide
// whether to enable some features.
// By default all options are set to false intentionally when creating a wrapped
// driver and provide the most sensible default with both performance and
// security in mind.
type SpanOptions struct {
// Ping, if set to true, will enable the creation of spans on Ping requests.
Ping bool
// RowsNext, if set to true, will enable the creation of events in spans on RowsNext
// calls. This can result in many events.
RowsNext bool
// DisableErrSkip, if set to true, will suppress driver.ErrSkip errors in spans.
DisableErrSkip bool
// DisableQuery if set to true, will suppress db.statement in spans.
DisableQuery bool
// RecordError, if set, will be invoked with the current error, and if the func returns true
// the record will be recorded on the current span.
//
// If this is not set it will default to record all errors (possible not ErrSkip, see option
// DisableErrSkip).
RecordError func(err error) bool
// AllowRoot, if set to true, will create root spans in absence of existing spans or even context.
AllowRoot bool
}
type defaultSpanNameFormatter struct{}
func (f *defaultSpanNameFormatter) Format(ctx context.Context, method Method, query string) string {
return string(method)
}
// newConfig returns a config with all Options set.
func newConfig(options ...Option) config {
cfg := config{
TracerProvider: otel.GetTracerProvider(),
MeterProvider: metricglobal.MeterProvider(),
SpanNameFormatter: &defaultSpanNameFormatter{},
}
for _, opt := range options {
opt.Apply(&cfg)
}
cfg.Tracer = cfg.TracerProvider.Tracer(
instrumentationName,
trace.WithInstrumentationVersion(Version()),
)
cfg.Meter = cfg.MeterProvider.Meter(
instrumentationName,
metric.WithInstrumentationVersion(Version()),
)
var err error
if cfg.Instruments, err = newInstruments(cfg.Meter); err != nil {
otel.Handle(err)
}
return cfg
}
func withDBStatement(cfg config, query string) []attribute.KeyValue {
if cfg.SpanOptions.DisableQuery {
return cfg.Attributes
}
return append(cfg.Attributes, semconv.DBStatementKey.String(query))
}