Skip to content

Commit 14f012e

Browse files
MrAliaspellared
andauthored
Add instrumentation for the pgx package (#91)
* pgx driver instrumentation PoC * Add instrumentation to README * Update instrumentation/github.com/jackc/pgx/splunkpgx/sql_test.go * Update instrumentation/github.com/jackc/pgx/splunkpgx/sql.go Co-authored-by: Robert Pająk <[email protected]> * Fix use of splunksql * Add changes to changelog * Add README * Update example to be complete * Add test module to validate end-to-end * Fix CHANGELOG markdown syntax * Fix docs on what import to replace * Add test module to dependabot config * Fix misspelling of conjunction * Use testing cleanup * Apply feedback Co-authored-by: Robert Pająk <[email protected]>
1 parent bad256c commit 14f012e

File tree

13 files changed

+1071
-0
lines changed

13 files changed

+1071
-0
lines changed

.github/dependabot.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ updates:
3030
directory: "/instrumentation/github.com/go-sql-driver/mysql/splunkmysql/test"
3131
schedule:
3232
interval: "daily"
33+
- package-ecosystem: "gomod"
34+
directory: "/instrumentation/github.com/jackc/pgx/splunkpgx"
35+
schedule:
36+
interval: "daily"
37+
- package-ecosystem: "gomod"
38+
directory: "/instrumentation/github.com/jackc/pgx/splunkpgx/test"
39+
schedule:
40+
interval: "daily"
3341
- package-ecosystem: "gomod"
3442
directory: "/instrumentation/github.com/lib/pq/splunkpq"
3543
schedule:

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1313
- Add the
1414
`github.com/signalfx/splunk-otel-go/instrumentation/github.com/go-sql-driver/mysql/splunkmysql`
1515
instrumentation for the `github.com/go-sql-driver/mysql` package. (#90)
16+
- Add the
17+
`github.com/signalfx/splunk-otel-go/instrumentation/github.com/jackc/pgx/splunkpgx`
18+
instrumentation for the `github.com/jackc/pgx` package. (#91)
1619
- Add the
1720
`github.com/signalfx/splunk-otel-go/instrumentation/github.com/lib/pq/splunkpq`
1821
instrumentation for the `github.com/lib/pq` package. (#92)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ Additional recommended Splunk specific instrumentations:
103103
- [`splunksql`](./instrumentation/database/sql/splunksql)
104104
- [`splunkhttp`](./instrumentation/net/http/splunkhttp)
105105
- [`splunkmysql`](./instrumentation/github.com/go-sql-driver/mysql/splunkmysql)
106+
- [`splunkpgx`](./instrumentation/github.com/jackc/pgx/splunkpgx)
106107
- [`splunkpq`](./instrumentation/github.com/lib/pq/splunkpq)
107108
108109
## Manual Instrumentation
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Splunk Instrumentation for the PostgreSQL Driver Package pgx
2+
3+
[![Go Reference](https://pkg.go.dev/badge/github.com/signalfx/splunk-otel-go/instrumentation/github.com/jackc/pgx/splunkpgx.svg)](https://pkg.go.dev/github.com/signalfx/splunk-otel-go/instrumentation/github.com/jackc/pgx/splunkpgx)
4+
5+
This package instruments the
6+
[`github.com/jackc/pgx`](https://github.com/jackc/pgx) package using the
7+
[`splunksql`](../../../../database/sql/splunksql) package.
8+
9+
## Getting Started
10+
11+
This package is design to be a drop-in replacement for the existing use of the
12+
`pgx` package when it is used in conjunction with the `database/sql` package.
13+
The blank identified import of `github.com/jackc/pgx/v4/stdlib` can be replaced
14+
with this package, and the standard library `sql.Open` function can be replaced
15+
with the equivalent `Open` from `splunksql`. An example can be found
16+
[here](./example_test.go).
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright Splunk Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package splunkpgx_test
16+
17+
import (
18+
"context"
19+
"database/sql"
20+
"fmt"
21+
"net/http"
22+
"strconv"
23+
"strings"
24+
25+
"github.com/signalfx/splunk-otel-go/distro"
26+
"github.com/signalfx/splunk-otel-go/instrumentation/database/sql/splunksql"
27+
28+
// Make sure to import this so the instrumented driver is registered.
29+
_ "github.com/signalfx/splunk-otel-go/instrumentation/github.com/jackc/pgx/splunkpgx"
30+
)
31+
32+
type server struct {
33+
DB *sql.DB
34+
}
35+
36+
func (s *server) listenAndServe() error {
37+
// Requests to /square/n will return the square of n.
38+
http.HandleFunc("/square/", s.handle)
39+
return http.ListenAndServe(":80", nil)
40+
}
41+
42+
func (s *server) handle(w http.ResponseWriter, req *http.Request) {
43+
idx := strings.LastIndex(req.URL.Path, "/")
44+
n, err := strconv.Atoi(req.URL.Path[idx+1:])
45+
if err != nil {
46+
http.Error(w, err.Error(), http.StatusBadRequest)
47+
return
48+
}
49+
50+
query := "SELECT squareNumber FROM squarenum WHERE number = ?"
51+
var nSquared int
52+
// Propagate the context to ensure created spans are included in any
53+
// existing trace.
54+
if err := s.DB.QueryRowContext(req.Context(), query, n).Scan(&nSquared); err != nil {
55+
http.Error(w, err.Error(), http.StatusInternalServerError)
56+
return
57+
}
58+
59+
fmt.Fprintf(w, "%d", nSquared)
60+
}
61+
62+
func Example() {
63+
// Setup the Splunk OTel Go distribution.
64+
sdk, err := distro.Run()
65+
if err != nil {
66+
panic(err)
67+
}
68+
// Ensure all spans are flushed before the application exits.
69+
defer func() {
70+
sErr := sdk.Shutdown(context.Background())
71+
if sErr != nil {
72+
panic(sErr)
73+
}
74+
}()
75+
76+
// Create a traced connection to the Postgres database.
77+
db, err := splunksql.Open("pgx", "postgres://localhost:5432/dbname")
78+
if err != nil {
79+
panic(err)
80+
}
81+
defer db.Close()
82+
83+
// Validate DSN data by opening a connection. There is no parent context
84+
// to pass here so the span created from this operation will be in its own
85+
// trace.
86+
if err := db.Ping(); err != nil {
87+
panic(err)
88+
}
89+
90+
srv := &server{DB: db}
91+
if err := srv.listenAndServe(); err != nil {
92+
panic(err)
93+
}
94+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module github.com/signalfx/splunk-otel-go/instrumentation/github.com/jackc/pgx/splunkpgx
2+
3+
go 1.15
4+
5+
require (
6+
github.com/jackc/fake v0.0.0-20150926172116-812a484cc733 // indirect
7+
github.com/jackc/pgx v3.6.2+incompatible
8+
github.com/jackc/pgx/v4 v4.13.0
9+
github.com/signalfx/splunk-otel-go v0.6.0
10+
github.com/signalfx/splunk-otel-go/instrumentation/database/sql/splunksql v0.0.0-00010101000000-000000000000
11+
github.com/stretchr/testify v1.7.0
12+
)
13+
14+
replace (
15+
github.com/signalfx/splunk-otel-go => ../../../../..
16+
github.com/signalfx/splunk-otel-go/instrumentation/database/sql/splunksql => ../../../../database/sql/splunksql
17+
)

0 commit comments

Comments
 (0)