forked from newrelic/go-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nrpq.go
160 lines (145 loc) · 4.22 KB
/
nrpq.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
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
// +build go1.10
// Package nrpq instruments https://github.com/lib/pq.
//
// Use this package to instrument your PostgreSQL calls without having to manually
// create DatastoreSegments. This is done in a two step process:
//
// 1. Use this package's driver in place of the postgres driver.
//
// If your code is using sql.Open like this:
//
// import (
// _ "github.com/lib/pq"
// )
//
// func main() {
// db, err := sql.Open("postgres", "user=pqgotest dbname=pqgotest sslmode=verify-full")
// }
//
// Then change the side-effect import to this package, and open "nrpostgres" instead:
//
// import (
// _ "github.com/newrelic/go-agent/_integrations/nrpq"
// )
//
// func main() {
// db, err := sql.Open("nrpostgres", "user=pqgotest dbname=pqgotest sslmode=verify-full")
// }
//
// If your code is using pq.NewConnector, simply use nrpq.NewConnector
// instead.
//
// 2. Provide a context containing a newrelic.Transaction to all exec and query
// methods on sql.DB, sql.Conn, and sql.Tx. This requires using the
// context methods ExecContext, QueryContext, and QueryRowContext in place of
// Exec, Query, and QueryRow respectively. For example, instead of the
// following:
//
// row := db.QueryRow("SELECT count(*) FROM pg_catalog.pg_tables")
//
// Do this:
//
// ctx := newrelic.NewContext(context.Background(), txn)
// row := db.QueryRowContext(ctx, "SELECT count(*) FROM pg_catalog.pg_tables")
//
// Unfortunately, sql.Stmt exec and query calls are not supported since pq.stmt
// does not have ExecContext and QueryContext methods (as of June 2019, see
// https://github.com/lib/pq/pull/768).
//
// A working example is shown here:
// https://github.com/newrelic/go-agent/tree/master/_integrations/nrpq/example/main.go
package nrpq
import (
"database/sql"
"database/sql/driver"
"os"
"path"
"regexp"
"strings"
"github.com/lib/pq"
newrelic "github.com/newrelic/go-agent"
"github.com/newrelic/go-agent/internal"
"github.com/newrelic/go-agent/internal/sqlparse"
)
var (
baseBuilder = newrelic.SQLDriverSegmentBuilder{
BaseSegment: newrelic.DatastoreSegment{
Product: newrelic.DatastorePostgres,
},
ParseQuery: sqlparse.ParseQuery,
ParseDSN: parseDSN(os.Getenv),
}
)
// NewConnector can be used in place of pq.NewConnector to get an instrumented
// PostgreSQL connector.
func NewConnector(dsn string) (driver.Connector, error) {
connector, err := pq.NewConnector(dsn)
if nil != err || nil == connector {
// Return nil rather than 'connector' since a nil pointer would
// be returned as a non-nil driver.Connector.
return nil, err
}
bld := baseBuilder
bld.ParseDSN(&bld.BaseSegment, dsn)
return newrelic.InstrumentSQLConnector(connector, bld), nil
}
func init() {
sql.Register("nrpostgres", newrelic.InstrumentSQLDriver(&pq.Driver{}, baseBuilder))
internal.TrackUsage("integration", "driver", "postgres")
}
var dsnSplit = regexp.MustCompile(`(\w+)\s*=\s*('[^=]*'|[^'\s]+)`)
func getFirstHost(value string) string {
host := strings.SplitN(value, ",", 2)[0]
host = strings.Trim(host, "[]")
return host
}
func parseDSN(getenv func(string) string) func(*newrelic.DatastoreSegment, string) {
return func(s *newrelic.DatastoreSegment, dsn string) {
if strings.HasPrefix(dsn, "postgres://") || strings.HasPrefix(dsn, "postgresql://") {
var err error
dsn, err = pq.ParseURL(dsn)
if nil != err {
return
}
}
host := getenv("PGHOST")
hostaddr := ""
ppoid := getenv("PGPORT")
dbname := getenv("PGDATABASE")
for _, split := range dsnSplit.FindAllStringSubmatch(dsn, -1) {
if len(split) != 3 {
continue
}
key := split[1]
value := strings.Trim(split[2], `'`)
switch key {
case "dbname":
dbname = value
case "host":
host = getFirstHost(value)
case "hostaddr":
hostaddr = getFirstHost(value)
case "port":
ppoid = strings.SplitN(value, ",", 2)[0]
}
}
if "" != hostaddr {
host = hostaddr
} else if "" == host {
host = "localhost"
}
if "" == ppoid {
ppoid = "5432"
}
if strings.HasPrefix(host, "/") {
// this is a unix socket
ppoid = path.Join(host, ".s.PGSQL."+ppoid)
host = "localhost"
}
s.Host = host
s.PortPathOrID = ppoid
s.DatabaseName = dbname
}
}