Skip to content
forked from j2gg0s/otsql

opentelemetry's plugin for databse/sql

License

Notifications You must be signed in to change notification settings

askingcat/otsql

 
 

Repository files navigation

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

Metric Search suffix Tags
Latency in microsecond go.sql/latency sql.instance, sql.method, sql.status

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

Metric Search suffix
The number of connections currently in use go.sql.conn.in_use
The number of idle connections go.sql.conn.idle
The total number of connections wait for go.sql.conn.wait
The total number of connections closed because of SetMaxIdleConns go.sql.conn.idle_closed
The total number of connections closed because of SetConnMaxLifetime go.sql.conn.lifetime_closed
The total time blocked by waiting for a new connection, nanosecond go.sql.conn.wait_ns

Test

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

About

opentelemetry's plugin for databse/sql

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%