splunkpq

package module
v1.15.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 11, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

README

Splunk Instrumentation for the PostgreSQL Driver Package lib/pq

Go Reference

This package instruments the github.com/lib/pq package using the 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.

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 (
	"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.Ping(); 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

func DSNParser

func DSNParser(dataSourceName string) (splunksql.ConnectionConfig, error)

DSNParser parses the data source connection name for a connection to a Postgres database using the github.com/lib/pq client package.

Types

This section is empty.

Directories

Path Synopsis
Package internal provides copied conversion functionality internal to the github.com/lib/pq package.
Package internal provides copied conversion functionality internal to the github.com/lib/pq package.
test module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL