Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add instrumentation for the pgx package #91

Merged
merged 20 commits into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/jackc/pgx/splunkpgx"
schedule:
interval: "daily"
- package-ecosystem: "gomod"
directory: "/instrumentation/github.com/jackc/pgx/splunkpgx/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/jackc/pgx/splunkpgx`
instrumentation for the `github.com/jackc/pgx` package. (#91)

### 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)
- [`splunkpgx`](./instrumentation/github.com/jackc/pgx/splunkpgx)

## Manual Instrumentation

Expand Down
17 changes: 17 additions & 0 deletions instrumentation/github.com/jackc/pgx/splunkpgx/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Splunk Instrumentation for the Postgres Driver Package pgx
MrAlias marked this conversation as resolved.
Show resolved Hide resolved

[![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)

This package instruments the
[`github.com/jackc/pgx`](https://github.com/jackc/pgx) 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
`pgx` 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,
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
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/jackc/pgx/splunkpgx/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 splunkpgx_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/jackc/pgx/splunkpgx"
)

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("pgx", "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)
}
}
17 changes: 17 additions & 0 deletions instrumentation/github.com/jackc/pgx/splunkpgx/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module github.com/signalfx/splunk-otel-go/instrumentation/github.com/jackc/pgx/splunkpgx

go 1.15

require (
github.com/jackc/fake v0.0.0-20150926172116-812a484cc733 // indirect
github.com/jackc/pgx v3.6.2+incompatible
github.com/jackc/pgx/v4 v4.13.0
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
)
Loading