Documentation
¶
Overview ¶
Package splunkpq provides instrumentation for the github.com/lib/pq package when using database/sql.
To use this package, replace any blank identified imports of the github.com/lib/pq package with an import of this package and use the splunksql.Open function as a replacement for any sql.Open function use. For example, if your code looks like this to start.
import (
"database/sql"
_ "github.com/lib/pq"
)
// ...
db, err := sql.Open("postgres", "postgres://localhost:5432/dbname")
// ...
Update to this.
import (
_ "github.com/signalfx/splunk-otel-go/instrumentation/github.com/lib/pq/splunkpq"
"github.com/signalfx/splunk-otel-go/instrumentation/database/sql/splunksql"
)
// ...
db, err := splunksql.Open("postgres", "postgres://localhost:5432/dbname")
// ...
Example ¶
package main
import (
"context"
"database/sql"
"fmt"
"net/http"
"strconv"
"strings"
"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 main() {
// 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.PingContext(context.Background()); err != nil {
panic(err)
}
srv := &server{DB: db}
if err := srv.listenAndServe(); err != nil {
panic(err)
}
}
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.