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.
$ go get github.com/XSAM/otelsql
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.
Getting started with otelsql, the OpenTelemetry instrumentation for Go SQL, is a blog post that explains how to use otelsql in miutes.
This project provides two docker-compose examples to show how to use it.
It creates spans on corresponding methods.
Use SpanOptions to adjust creation of spans.
db.operation.name (method name), error.type (if error occurs)status (idle, inuse)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:
For standard driver errors: Special handling for common driver errors:
database/sql/driver.ErrBadConndatabase/sql/driver.ErrSkipdatabase/sql/driver.ErrRemoveArgumentFor custom errors: The fully qualified type name is used (e.g., github.com/your/package.CustomError).
For built-in errors: The type name is used (e.g., *errors.errorString for errors created with errors.New()).
This project is tested on the following systems.
| OS | Go Version | Architecture |
|---|---|---|
| Ubuntu | 1.27 | amd64 |
| Ubuntu | 1.26 | amd64 |
| Ubuntu | 1.27 | 386 |
| Ubuntu | 1.26 | 386 |
| MacOS | 1.27 | amd64 |
| MacOS | 1.26 | amd64 |
| Windows | 1.27 | amd64 |
| Windows | 1.26 | amd64 |
| Windows | 1.27 | 386 |
| Windows | 1.26 | 386 |
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.
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.
I use GitHub discussions/issues for most communications. Feel free to contact me on CNCF slack.
410 followers · starred Feb 2025
477 followers · starred Jan 2022
160 followers · starred Sep 2021
105 followers · starred May 2021
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.
$ go get github.com/XSAM/otelsql
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.
Getting started with otelsql, the OpenTelemetry instrumentation for Go SQL, is a blog post that explains how to use otelsql in miutes.
This project provides two docker-compose examples to show how to use it.
It creates spans on corresponding methods.
Use SpanOptions to adjust creation of spans.
db.operation.name (method name), error.type (if error occurs)status (idle, inuse)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:
For standard driver errors: Special handling for common driver errors:
database/sql/driver.ErrBadConndatabase/sql/driver.ErrSkipdatabase/sql/driver.ErrRemoveArgumentFor custom errors: The fully qualified type name is used (e.g., github.com/your/package.CustomError).
For built-in errors: The type name is used (e.g., *errors.errorString for errors created with errors.New()).
This project is tested on the following systems.
| OS | Go Version | Architecture |
|---|---|---|
| Ubuntu | 1.27 | amd64 |
| Ubuntu | 1.26 | amd64 |
| Ubuntu | 1.27 | 386 |
| Ubuntu | 1.26 | 386 |
| MacOS | 1.27 | amd64 |
| MacOS | 1.26 | amd64 |
| Windows | 1.27 | amd64 |
| Windows | 1.26 | amd64 |
| Windows | 1.27 | 386 |
| Windows | 1.26 | 386 |
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.
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.
I use GitHub discussions/issues for most communications. Feel free to contact me on CNCF slack.
410 followers · starred Feb 2025
477 followers · starred Jan 2022
160 followers · starred Sep 2021
105 followers · starred May 2021