forked from signalfx/splunk-otel-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql.go
More file actions
99 lines (88 loc) · 3.49 KB
/
Copy pathsql.go
File metadata and controls
99 lines (88 loc) · 3.49 KB
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
// Copyright Splunk Inc.
//
// 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 splunksql provides functions to trace the database/sql package
// (https://golang.org/pkg/database/sql) using the OpenTelemetry API. It will
// automatically augment operations such as connections, statements and
// transactions with tracing.
package splunksql // import "github.com/signalfx/splunk-otel-go/instrumentation/database/sql/splunksql"
import (
"context"
"database/sql"
"database/sql/driver"
"sync"
)
var (
registryMu sync.RWMutex
registry = make(map[string]InstrumentationConfig)
)
// Register makes the InstrumentationConfig for the setup of a database
// driverwith the provided name available. If Register is called twice for the
// same name it panics.
func Register(name string, c InstrumentationConfig) {
registryMu.Lock()
defer registryMu.Unlock()
if _, dup := registry[name]; dup {
panic("splunksql: Register called twice for " + name)
}
registry[name] = c
}
func retrieve(name string) InstrumentationConfig {
registryMu.RLock()
defer registryMu.RUnlock()
return registry[name]
}
// Open opens a database specified by its database driver name and a
// driver-specific data source name, usually consisting of at least a database
// name and connection information. The returned database uses a traced driver
// for all connections.
func Open(driverName, dataSourceName string, opts ...Option) (*sql.DB, error) {
// The instrumented driver needs to already have been registered with the
// database/sql package. This is something instrumentation libraries can
// do by importing the package containing the driver (if it correctly
// initializes with the registration of its driver).
db, err := sql.Open(driverName, dataSourceName)
if err != nil {
return nil, err
}
// Setup any instrumentation that is registered for this driverName. If no
// instrumentation was registered for the driver, this will give a "best
// effort" to setup the driver.
regOpt := withRegistrationConfig(retrieve(driverName), dataSourceName)
// Allow users to override default instrumentation setup values by
// appending opts to the Option returned from withRegistrationConfig. This
// will allow similar instrumentation to be used with minor corrections
// being applied here (e.g. using `lib/pg` instead of `pgx`).
opts = append([]Option{regOpt}, opts...)
d := newDriver(db.Driver(), newTraceConfig(opts...))
// Use the instrumented driver to open a connection to the database.
if driverCtx, ok := d.(driver.DriverContext); ok {
connector, err := driverCtx.OpenConnector(dataSourceName)
if err != nil {
return nil, err
}
return sql.OpenDB(connector), nil
}
return sql.OpenDB(dsnConnector{dsn: dataSourceName, driver: d}), nil
}
// dsnConnector wraps a driver to be used as a DriverContext.
type dsnConnector struct {
dsn string
driver driver.Driver
}
func (t dsnConnector) Connect(context.Context) (driver.Conn, error) {
return t.driver.Open(t.dsn)
}
func (t dsnConnector) Driver() driver.Driver {
return t.driver
}