instapgx

package module
v2.10.0 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2025 License: MIT Imports: 8 Imported by: 0

README

Instana instrumentation for pgx/v5

This package provides Instana instrumentation for the pgx/v5 package.

PkgGoDev

Installation

To add the module to your go.mod file, run the following command in your project directory:

$ go get github.com/instana/go-sensor/instrumentation/instapgx/v2

Usage

// Create an Instana collector
c := instana.InitCollector(&instana.Options{
    Service: "pgx-v5-service",
})

// Parse Config
cfg, err := pgx.ParseConfig("postgres://username:password@localhost/database")

// Assign the tracer interface with Instana tracer
cfg.Tracer = instapgx.InstanaTracer(cfg, c)

// Create the connection using the cfg with Instana tracer
conn, err := pgx.ConnectConfig(ctx, cfg)
defer conn.Close(ctx)

Documentation

Overview

Package instapgx provides Instana instrumentation for pgx/v5 package.

Example (BasicUsage)
urlExample := "postgres://postgres:mysecretpassword@localhost:5432/postgres"
cfg, err := pgx.ParseConfig(urlExample)

// Initialising Instana Collector
c := instana.InitCollector(&instana.Options{
	Service: "pgx-v5-service",
})

// Assigning the Instana tracer to the cfg.Tracer interface
cfg.Tracer = instapgx.InstanaTracer(cfg, c)

// Use the cfg in the normal way to create a connection and use it
ctx := context.Background()
var conn *pgx.Conn
conn, err = pgx.ConnectConfig(ctx, cfg)
if err != nil {
	fmt.Printf("unable to connect to database: %v\n\n", err)
	os.Exit(1)
} else {
	fmt.Println("connection successful")
}
defer func() {
	err := conn.Close(ctx)
	if err != nil {
		fmt.Printf("unable to close connection: %v\n", err)
	}
}()

var val string
query := "<valid-query>"
err = conn.QueryRow(ctx, query).Scan(&val)
if err != nil {
	fmt.Printf("queryRow failed: %v\n\n", err)
	os.Exit(1)
}
Output:

Index

Examples

Constants

View Source
const Version = "2.10.0"

Version is the instrumentation module semantic version

Variables

This section is empty.

Functions

func InstanaTracer

func InstanaTracer(cfg *pgx.ConnConfig, collector instana.TracerLogger) pgx.QueryTracer

InstanaTracer returns Instana tracer which can be used for instrumenting pgx/v5 database calls.

Types

This section is empty.

Jump to

Keyboard shortcuts

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