-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add instrumentation for the lib/pq posgres driver package (#92)
* 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
Showing
14 changed files
with
1,099 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
94
instrumentation/github.com/lib/pq/splunkpq/example_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
Oops, something went wrong.