Skip to content

Commit

Permalink
Add instrumentation for the lib/pq posgres driver package (#92)
Browse files Browse the repository at this point in the history
* lib/pq driver instrumentation PoC

* Add changes to changelog

* Add README

* Add example_test

* Update splunksql usage

* Add end-to-end integration test

* Update instrumentation/github.com/lib/pq/splunkpq/README.md

Co-authored-by: Robert Pająk <pellared@hotmail.com>

Co-authored-by: Robert Pająk <pellared@hotmail.com>
  • Loading branch information
MrAlias and pellared authored Sep 27, 2021
1 parent e640666 commit d73eb46
Show file tree
Hide file tree
Showing 14 changed files with 1,099 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ updates:
directory: "/instrumentation/github.com/go-sql-driver/mysql/splunkmysql/test"
schedule:
interval: "daily"
- package-ecosystem: "gomod"
directory: "/instrumentation/github.com/lib/pq/splunkpq"
schedule:
interval: "daily"
- package-ecosystem: "gomod"
directory: "/instrumentation/github.com/lib/pq/splunkpq/test"
schedule:
interval: "daily"
- package-ecosystem: "gomod"
directory: "/instrumentation/net/http/splunkhttp"
schedule:
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Add the
`github.com/signalfx/splunk-otel-go/instrumentation/github.com/go-sql-driver/mysql/splunkmysql`
instrumentation for the `github.com/go-sql-driver/mysql` package. (#90)
- Add the
`github.com/signalfx/splunk-otel-go/instrumentation/github.com/lib/pq/splunkpq`
instrumentation for the `github.com/lib/pq` package. (#92)

### Changed

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ Additional recommended Splunk specific instrumentations:
- [`splunksql`](./instrumentation/database/sql/splunksql)
- [`splunkhttp`](./instrumentation/net/http/splunkhttp)
- [`splunkmysql`](./instrumentation/github.com/go-sql-driver/mysql/splunkmysql)
- [`splunkpq`](./instrumentation/github.com/lib/pq/splunkpq)
## Manual Instrumentation
Expand Down
16 changes: 16 additions & 0 deletions instrumentation/github.com/lib/pq/splunkpq/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Splunk Instrumentation for the PostgreSQL Driver Package lib/pq

[![Go Reference](https://pkg.go.dev/badge/github.com/signalfx/splunk-otel-go/instrumentation/github.com/lib/pq/splunkpq.svg)](https://pkg.go.dev/github.com/signalfx/splunk-otel-go/instrumentation/github.com/lib/pq/splunkpq)

This package instruments the [`github.com/lib/pq`](https://github.com/lib/pq)
package using the [`splunksql`](../../../../database/sql/splunksql) package.

## Getting Started

This package is design to be a drop-in replacement for the existing use of the
`lib/pg` package when it is used in conjunction with the `database/sql`
package. The blank identified imports of that package can be replaced with
this package, and the standard library `sql.Open` function can be replaced with
the equivalent `Open` from `splunksql`.

An example can be found [here](./example_test.go).
94 changes: 94 additions & 0 deletions instrumentation/github.com/lib/pq/splunkpq/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// 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 splunkpq_test

import (
"context"
"database/sql"
"fmt"
"net/http"
"strconv"
"strings"

"github.com/signalfx/splunk-otel-go/distro"
"github.com/signalfx/splunk-otel-go/instrumentation/database/sql/splunksql"

// Make sure to import this so the instrumented driver is registered.
_ "github.com/signalfx/splunk-otel-go/instrumentation/github.com/lib/pq/splunkpq"
)

type server struct {
DB *sql.DB
}

func (s *server) listenAndServe() error {
// Requests to /square/n will return the square of n.
http.HandleFunc("/square/", s.handle)
return http.ListenAndServe(":80", nil)
}

func (s *server) handle(w http.ResponseWriter, req *http.Request) {
idx := strings.LastIndex(req.URL.Path, "/")
n, err := strconv.Atoi(req.URL.Path[idx+1:])
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

query := "SELECT squareNumber FROM squarenum WHERE number = ?"
var nSquared int
// Propagate the context to ensure created spans are included in any
// existing trace.
if err := s.DB.QueryRowContext(req.Context(), query, n).Scan(&nSquared); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

fmt.Fprintf(w, "%d", nSquared)
}

func Example() {
// Setup the Splunk OTel Go distribution.
sdk, err := distro.Run()
if err != nil {
panic(err)
}
// Ensure all spans are flushed before the application exits.
defer func() {
sErr := sdk.Shutdown(context.Background())
if sErr != nil {
panic(sErr)
}
}()

// Create a traced connection to the Postgres database.
db, err := splunksql.Open("postgres", "postgres://localhost:5432/dbname")
if err != nil {
panic(err)
}
defer db.Close()

// Validate DSN data by opening a connection. There is no parent context
// to pass here so the span created from this operation will be in its own
// trace.
if err := db.Ping(); err != nil {
panic(err)
}

srv := &server{DB: db}
if err := srv.listenAndServe(); err != nil {
panic(err)
}
}
15 changes: 15 additions & 0 deletions instrumentation/github.com/lib/pq/splunkpq/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module github.com/signalfx/splunk-otel-go/instrumentation/github.com/lib/pq/splunkpq

go 1.15

require (
github.com/lib/pq v1.10.3
github.com/signalfx/splunk-otel-go v0.6.0
github.com/signalfx/splunk-otel-go/instrumentation/database/sql/splunksql v0.0.0-00010101000000-000000000000
github.com/stretchr/testify v1.7.0
)

replace (
github.com/signalfx/splunk-otel-go => ../../../../..
github.com/signalfx/splunk-otel-go/instrumentation/database/sql/splunksql => ../../../../database/sql/splunksql
)
30 changes: 30 additions & 0 deletions instrumentation/github.com/lib/pq/splunkpq/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/lib/pq v1.10.3 h1:v9QZf2Sn6AmjXtQeFpdoq/eaNtYP6IN+7lcrygsIAtg=
github.com/lib/pq v1.10.3/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
go.opentelemetry.io/contrib/propagators/b3 v0.24.0 h1:pY3a0R/fP8Zrxcq6cQ3GtdtUGhNLjj5rEOZXG2BUWTA=
go.opentelemetry.io/contrib/propagators/b3 v0.24.0/go.mod h1:8zejVdED2pabka2VLti4kussRPFgSkRUv3JUSbljn1E=
go.opentelemetry.io/otel v1.0.0 h1:qTTn6x71GVBvoafHK/yaRUmFzI4LcONZD0/kXxl5PHI=
go.opentelemetry.io/otel v1.0.0/go.mod h1:AjRVh9A5/5DE7S+mZtTR6t8vpKKryam+0lREnfmS4cg=
go.opentelemetry.io/otel/exporters/jaeger v1.0.0 h1:cLhx8llHw02h5JTqGqaRbYn+QVKHmrzD9vEbKnSPk5U=
go.opentelemetry.io/otel/exporters/jaeger v1.0.0/go.mod h1:q10N1AolE1JjqKrFJK2tYw0iZpmX+HBaXBtuCzRnBGQ=
go.opentelemetry.io/otel/sdk v1.0.0 h1:BNPMYUONPNbLneMttKSjQhOTlFLOD9U22HNG1KrIN2Y=
go.opentelemetry.io/otel/sdk v1.0.0/go.mod h1:PCrDHlSy5x1kjezSdL37PhbFUMjrsLRshJ2zCzeXwbM=
go.opentelemetry.io/otel/trace v1.0.0 h1:TSBr8GTEtKevYMG/2d21M989r5WJYVimhTHBKVEZuh4=
go.opentelemetry.io/otel/trace v1.0.0/go.mod h1:PXTWqayeFUlJV1YDNhsJYB184+IvAH814St6o6ajzIs=
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7 h1:iGu644GcxtEcrInvDsQRCwJjtCIOlT2V7IRt6ah2Whw=
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading

0 comments on commit d73eb46

Please sign in to comment.