Categorygithub.com/cspinetta/otsql
repositorypackage
0.3.0-alpha-2
Repository: https://github.com/cspinetta/otsql.git
Documentation: pkg.go.dev

# Packages

No description provided by the author

# README

otsql

Docs Go Report Card

OpenTelemetry SQL database driver wrapper, not official.

Add an otsql wrapper to your existing database code to instrument the interactions with the database.

First version transformed from ocsql.

Install

$ go get -u github.com/j2gg0s/otsql

Usage

To use otsql with your application, register an otsql wrapper of a database driver as shown below.

Example:

import (
    "database/sql"

    "github.com/j2gg0s/otsql"
    _ "github.com/lib/pq"
)

var dsn = "postgres://otsql_user:otsql_password@localhost/otsql_db?sslmode=disable"

driverName, err := otsql.Register("postgres", otsql.WithQuery(true))
if err != nil {
    panic(err)
}

db, err := sql.Open(driverName, dsn)
if err != nil {
    panic(err)
}
defer db.Close()

Finally database drivers that support the driver.Connector interface can be wrapped directly by otsql without the need for otsql to register a driver.Driver.

Example:

import (
    "database/sql"

    "github.com/j2gg0s/otsql"
    _ "github.com/lib/pq"
)

var dsn = "postgres://otsql_user:otsql_password@localhost/otsql_db?sslmode=disable"

connector, err := pq.NewConnector(dsn)
if err != nil {
    panic(err)
}

db := sql.OpenDB(
    WrapConnector(connector, otsql.WithQuery(true)))
defer db.Close()

See more specific case in example/.

Metric And Span

MetricSearch suffixTags
Latency in microsecondgo.sql/latencysql.instance, sql.method, sql.status

If use RecordStats, all metric supprt tag sql.instance.

MetricSearch suffix
The number of connections currently in usego.sql.conn.in_use
The number of idle connectionsgo.sql.conn.idle
The total number of connections wait forgo.sql.conn.wait
The total number of connections closed because of SetMaxIdleConnsgo.sql.conn.idle_closed
The total number of connections closed because of SetConnMaxLifetimego.sql.conn.lifetime_closed
The total time blocked by waiting for a new connection, nanosecondgo.sql.conn.wait_ns

Test

We add wrap to gorm and run its test with a forked repo j2gg0s/gorm .