Categorygithub.com/LeonPev/otelsql
repositorypackage
0.0.0-20230616111254-e34857e83278
Repository: https://github.com/leonpev/otelsql.git
Documentation: pkg.go.dev

# README

otelsql

ci codecov Go Report Card Documentation

It is an OpenTelemetry instrumentation for Golang database/sql, a port from https://github.com/open-telemetry/opentelemetry-go-contrib/pull/505.

It instruments traces and metrics.

Install

$ go get github.com/XSAM/otelsql

Usage

This project provides four different ways to instrument database/sql:

otelsql.Open, otelsql.OpenDB, otesql.Register and otelsql.WrapDriver.

And then use otelsql.RegisterDBStatsMetrics to instrument sql.DBStats with metrics.

db, err := otelsql.Open("mysql", mysqlDSN, otelsql.WithAttributes(
	semconv.DBSystemMySQL,
))
if err != nil {
	panic(err)
}
defer db.Close()

err = otelsql.RegisterDBStatsMetrics(db, otelsql.WithAttributes(
	semconv.DBSystemMySQL,
))
if err != nil {
	panic(err)
}

Check Option for more features like adding context propagation to SQL queries when enabling WithSQLCommenter.

See godoc and a docker-compose example for details.

Trace Instruments

It creates spans on corresponding methods.

Use SpanOptions to adjust creation of spans.

Metric Instruments

NameDescriptionUnitsInstrument TypeValue TypeAttribute Key(s)Attribute Values
db.sql.latencyThe latency of calls in millisecondsmsHistogramfloat64statusok, error
methodmethod name, like sql.conn.query
db.sql.connection.max_openMaximum number of open connections to the databaseAsynchronous Gaugeint64
db.sql.connection.openThe number of established connections both in use and idleAsynchronous Gaugeint64statusidle, inuse
db.sql.connection.waitThe total number of connections waited forAsynchronous Counterint64
db.sql.connection.wait_durationThe total time blocked waiting for a new connectionmsAsynchronous Counterfloat64
db.sql.connection.closed_max_idleThe total number of connections closed due to SetMaxIdleConnsAsynchronous Counterint64
db.sql.connection.closed_max_idle_timeThe total number of connections closed due to SetConnMaxIdleTimeAsynchronous Counterint64
db.sql.connection.closed_max_lifetimeThe total number of connections closed due to SetConnMaxLifetimeAsynchronous Counterint64

Compatibility

This project is tested on the following systems.

OSGo VersionArchitecture
Ubuntu1.20amd64
Ubuntu1.19amd64
Ubuntu1.20386
Ubuntu1.19386
MacOS1.20amd64
MacOS1.19amd64
Windows1.20amd64
Windows1.19amd64
Windows1.20386
Windows1.19386

While this project should work for other systems, no compatibility guarantees are made for those systems currently.

The project follows the Release Policy to support major Go releases.

Why port this?

Based on this comment, OpenTelemetry SIG team like to see broader usage and community consensus on an approach before they commit to the level of support that would be required of a package in contrib. But it is painful for users without a stable version, and they have to use replacement in go.mod to use this instrumentation.

Therefore, I host this module independently for convenience and make improvements based on users' feedback.

Communication

I use GitHub discussions/issues for most communications. Feel free to contact me on CNCF slack.