XSAM/otelsql

OpenTelemetry instrumentation for database/sql

Go

421

465 commits

updated Oct 9, 2026

See the code

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)
}

reg, err := otelsql.RegisterDBStatsMetrics(db, otelsql.WithAttributes(
	semconv.DBSystemMySQL,
))
if err != nil {
	panic(err)
}
defer func() {
	_ = db.Close()
	_ = reg.Unregister()
}()

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

See godoc for details.

Blog

Getting started with otelsql, the OpenTelemetry instrumentation for Go SQL, is a blog post that explains how to use otelsql in miutes.

Examples

This project provides two docker-compose examples to show how to use it.

  • The stdout example is a simple example to show how to use it with a MySQL database. It prints the trace data to stdout and serves metrics data via prometheus client.
  • The otel-collector example is a more complex example to show how to use it with a MySQL database and an OpenTelemetry Collector. It sends the trace data and metrics data to an OpenTelemetry Collector. Then, it shows data visually on Jaeger and Prometheus servers.

Trace Instruments

It creates spans on corresponding methods.

Use SpanOptions to adjust creation of spans.

Metric Instruments

Connection Statistics Metrics (from Go's sql.DBStats)

  • db.sql.connection.max_open: Maximum number of open connections to the database
  • db.sql.connection.open: The number of established connections
    • Attributes: status (idle, inuse)
  • db.sql.connection.wait: The total number of connections waited for
  • db.sql.connection.wait_duration: The total time blocked waiting for a new connection (ms)
  • db.sql.connection.closed_max_idle: The total number of connections closed due to SetMaxIdleConns
  • db.sql.connection.closed_max_idle_time: The total number of connections closed due to SetConnMaxIdleTime
  • db.sql.connection.closed_max_lifetime: The total number of connections closed due to SetConnMaxLifetime

Error Type Attribution

When errors occur during database operations, the error.type attribute is automatically populated with the type of the error. This provides more detailed information for debugging and monitoring:

  1. For standard driver errors: Special handling for common driver errors:

    • database/sql/driver.ErrBadConn
    • database/sql/driver.ErrSkip
    • database/sql/driver.ErrRemoveArgument
  2. For custom errors: The fully qualified type name is used (e.g., github.com/your/package.CustomError).

  3. For built-in errors: The type name is used (e.g., *errors.errorString for errors created with errors.New()).

Compatibility

This project is tested on the following systems.

OSGo VersionArchitecture
Ubuntu1.27amd64
Ubuntu1.26amd64
Ubuntu1.27386
Ubuntu1.26386
MacOS1.27amd64
MacOS1.26amd64
Windows1.27amd64
Windows1.26amd64
Windows1.27386
Windows1.26386

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.

golang
instrumentation
opentelemetry
sql

XSAM/otelsql

OpenTelemetry instrumentation for database/sql

Go

421

465 commits

updated Oct 9, 2026

See the code

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)
}

reg, err := otelsql.RegisterDBStatsMetrics(db, otelsql.WithAttributes(
	semconv.DBSystemMySQL,
))
if err != nil {
	panic(err)
}
defer func() {
	_ = db.Close()
	_ = reg.Unregister()
}()

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

See godoc for details.

Blog

Getting started with otelsql, the OpenTelemetry instrumentation for Go SQL, is a blog post that explains how to use otelsql in miutes.

Examples

This project provides two docker-compose examples to show how to use it.

  • The stdout example is a simple example to show how to use it with a MySQL database. It prints the trace data to stdout and serves metrics data via prometheus client.
  • The otel-collector example is a more complex example to show how to use it with a MySQL database and an OpenTelemetry Collector. It sends the trace data and metrics data to an OpenTelemetry Collector. Then, it shows data visually on Jaeger and Prometheus servers.

Trace Instruments

It creates spans on corresponding methods.

Use SpanOptions to adjust creation of spans.

Metric Instruments

Connection Statistics Metrics (from Go's sql.DBStats)

  • db.sql.connection.max_open: Maximum number of open connections to the database
  • db.sql.connection.open: The number of established connections
    • Attributes: status (idle, inuse)
  • db.sql.connection.wait: The total number of connections waited for
  • db.sql.connection.wait_duration: The total time blocked waiting for a new connection (ms)
  • db.sql.connection.closed_max_idle: The total number of connections closed due to SetMaxIdleConns
  • db.sql.connection.closed_max_idle_time: The total number of connections closed due to SetConnMaxIdleTime
  • db.sql.connection.closed_max_lifetime: The total number of connections closed due to SetConnMaxLifetime

Error Type Attribution

When errors occur during database operations, the error.type attribute is automatically populated with the type of the error. This provides more detailed information for debugging and monitoring:

  1. For standard driver errors: Special handling for common driver errors:

    • database/sql/driver.ErrBadConn
    • database/sql/driver.ErrSkip
    • database/sql/driver.ErrRemoveArgument
  2. For custom errors: The fully qualified type name is used (e.g., github.com/your/package.CustomError).

  3. For built-in errors: The type name is used (e.g., *errors.errorString for errors created with errors.New()).

Compatibility

This project is tested on the following systems.

OSGo VersionArchitecture
Ubuntu1.27amd64
Ubuntu1.26amd64
Ubuntu1.27386
Ubuntu1.26386
MacOS1.27amd64
MacOS1.26amd64
Windows1.27amd64
Windows1.26amd64
Windows1.27386
Windows1.26386

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.

golang
instrumentation
opentelemetry
sql

Project health

Latest release

v0.44.0 · Sep 6, 2026

Releases this year

4

Significant stargazers

Aleksandr Razumov

410 followers · starred Feb 2025

Dave Henderson

477 followers · starred Jan 2022

Robert Pająk

160 followers · starred Sep 2021

Tyler Yahn

105 followers · starred May 2021