modulepackage
0.3.0-alpha-2
Repository: https://github.com/cspinetta/otsql.git
Documentation: pkg.go.dev
# README
otsql
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 .
# Packages
No description provided by the author
# Functions
RecordStats records database statistics for provided sql.DB.
Register initializes and registers our otsql wrapped database driver identified by its driverName and using provided TraceOptions.
WithAllowRoot if set to true, will allow otsql to create root spans in absence of exisiting spans or even context.
WithDefaultLabels will be set to each span as default.
WithInstanceName sets database instance name.
WithLastInsertID if set to true, will enable the creation of spans on LastInsertId calls.
WithOptions sets our otsql tracing middleware options through a single TraceOptions object.
WithPing if set to true, will enable the creation of spans on Ping requests.
WithQuery if set to true, will enable recording of sql queries in spans.
WithQueryParams if set to true, will enable recording of parameters used with parametrized queries.
WithRowsAffected if set to true, will enable the creation of spans on RowsAffected calls.
WithRowsClose if set to true, will enable the creation of spans on RowsClose calls.
WithRowsNext if set to true, will enable the creation of spans on RowsNext calls.
WithSpanNameFormatter sets name for each span.
Wrap takes a SQL driver and wraps it with OpenCensus instrumentation.
WrapConn allows an existing driver.Conn to be wrapped by otsql.
WrapConnector allows wrapping a database driver.Connector which eliminates the need to register otsql as an available driver.Driver.
# Structs
TraceOptions holds configuration of our otsql tracing middleware.
# Type aliases
TraceOption allows for managing otsql configuration using functional options.