👁 Image
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.
Semantic Convention Stability Migration
The environment variable OTEL_SEMCONV_STABILITY_OPT_IN will be supported for at least six months. After this period, support for legacy metrics and Semantic Conventions v1.24.0 may be removed in the next release.
Check the CHANGELOG.md for more details.
Trace Instruments
It creates spans on corresponding methods.
Use SpanOptions to adjust creation of spans.
Trace Semantic Convention Stability
The instrumentation supports different OpenTelemetry semantic convention stability levels, configured through the OTEL_SEMCONV_STABILITY_OPT_IN environment variable:
| Setting | Description |
|---|---|
| empty (default) | Only uses db.statement attribute. |
database/dup |
Uses both db.statement and db.query.text attributes. |
database |
Uses db.query.text attribute. |
Metric Instruments
Two types of metrics are provided depending on the semantic convention stability setting:
Legacy Metrics (default)
- db.sql.latency: The latency of calls in milliseconds
- Unit: milliseconds
- Attributes:
method(method name),status(ok, error)
OpenTelemetry Semantic Convention Metrics
- db.client.operation.duration: Duration of database client operations
- Unit: seconds
- Attributes:
db.operation.name(method name),error.type(if error occurs)
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)
- Attributes:
- 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
Metric Semantic Convention Stability
The instrumentation supports different OpenTelemetry semantic convention stability levels, configured through the OTEL_SEMCONV_STABILITY_OPT_IN environment variable:
| Setting | Metrics Emitted | Description |
|---|---|---|
| empty (default) | db.sql.latency only |
Only uses legacy metric |
database/dup |
Both db.sql.latency and db.client.operation.duration |
Emits both legacy and new OTel metric formats |
database |
db.client.operation.duration only |
Only uses the new OTel semantic convention metric |
Connection statistics metrics (db.sql.connection.*) are always emitted regardless of the stability setting.
This allows users to gradually migrate to the new OpenTelemetry semantic conventions while maintaining backward compatibility with existing dashboards and alerts.
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:
-
For standard driver errors: Special handling for common driver errors:
database/sql/driver.ErrBadConndatabase/sql/driver.ErrSkipdatabase/sql/driver.ErrRemoveArgument
-
For 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.errorStringfor errors created witherrors.New()).
Note: The error.type attribute is only available when using the new stable OpenTelemetry semantic convention metrics. This requires setting OTEL_SEMCONV_STABILITY_OPT_IN to either database/dup or database. With the default setting (empty), which only uses legacy metrics, the error.type attribute will not be populated.
Compatibility
This project is tested on the following systems.
| OS | Go Version | Architecture |
|---|---|---|
| Ubuntu | 1.26 | amd64 |
| Ubuntu | 1.25 | amd64 |
| Ubuntu | 1.26 | 386 |
| Ubuntu | 1.25 | 386 |
| MacOS | 1.26 | amd64 |
| MacOS | 1.25 | amd64 |
| Windows | 1.26 | amd64 |
| Windows | 1.25 | amd64 |
| Windows | 1.26 | 386 |
| Windows | 1.25 | 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.
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.
👁 Image
Documentation
¶
Overview ¶
Package otelsql instruments the database/sql package.
otelsql will trace every interface from database/sql/driver package which has context except driver.Pinger.
Index ¶
- func AttributesFromDSN(dsn string) []attribute.KeyValue
- func Open(driverName, dataSourceName string, options ...Option) (*sql.DB, error)
- func OpenDB(c driver.Connector, options ...Option) *sql.DB
- func Register(driverName string, options ...Option) (string, error)
- func RegisterDBStatsMetrics(db *sql.DB, opts ...Option) (metric.Registration, error)
- func Version() string
- func WrapDriver(dri driver.Driver, options ...Option) driver.Driver
- type AttributesGetter
- type Event
- type InstrumentAttributesGetter
- type InstrumentErrorAttributesGetter
- type Method
- type Option
- func WithAttributes(attributes ...attribute.KeyValue) Option
- func WithAttributesGetter(attributesGetter AttributesGetter) Option
- func WithDisableSkipErrMeasurement(disable bool) Option
- func WithInstrumentAttributesGetter(instrumentAttributesGetter InstrumentAttributesGetter) Option
- func WithInstrumentErrorAttributesGetter(instrumentErrorAttributesGetter InstrumentErrorAttributesGetter) Option
- func WithMeterProvider(provider metric.MeterProvider) Option
- func WithSQLCommenter(enabled bool) Option
- func WithSpanNameFormatter(spanNameFormatter SpanNameFormatter) Option
- func WithSpanOptions(opts SpanOptions) Option
- func WithTextMapPropagator(propagator propagation.TextMapPropagator) Option
- func WithTracerProvider(provider trace.TracerProvider) Option
- type OptionFunc
- type SpanFilter
- type SpanNameFormatter
- type SpanOptions
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AttributesFromDSN ¶ added in v0.37.0
AttributesFromDSN returns attributes extracted from a DSN string. It makes the best effort to retrieve values for semconv.ServerAddressKey and semconv.ServerPortKey.
func Register ¶
Register initializes and registers OTel wrapped database driver identified by its driverName, using provided Option. It is possible to register multiple wrappers for the same database driver if needing different Option for different connections.
func RegisterDBStatsMetrics ¶ added in v0.13.0
RegisterDBStatsMetrics registers sql.DBStats metrics with OTel instrumentation. Call Unregister on the returned Registration when the db is no longer used.
Types ¶
type AttributesGetter ¶ added in v0.17.0
type AttributesGetter func(ctx context.Context, method Method, query string, args []driver.NamedValue) []attribute.KeyValue
AttributesGetter provides additional attributes on spans creation.
type Event ¶
type Event string
Event specifics events in the database/sql package.
const ( // EventRowsNext is triggered during driver.Rows.Next iteration to track each row fetching operation. EventRowsNext Event = "sql.rows.next" )
type InstrumentAttributesGetter ¶ added in v0.33.0
type InstrumentAttributesGetter func(ctx context.Context, method Method, query string, args []driver.NamedValue) []attribute.KeyValue
InstrumentAttributesGetter provides additional attributes while recording metrics to instruments.
type InstrumentErrorAttributesGetter ¶ added in v0.38.0
InstrumentErrorAttributesGetter provides additional error-related attributes while recording metrics to instruments.
type Method ¶
type Method string
Method specifics operation in the database/sql package.
const ( // MethodConnectorConnect is used when driver.Connector.Connect is called to establish a new connection. MethodConnectorConnect Method = "sql.connector.connect" // MethodConnPing is used with driver.Pinger.Ping to verify database connection is alive. MethodConnPing Method = "sql.conn.ping" // MethodConnExec is used with driver.ExecerContext.ExecContext for direct query execution through a connection. MethodConnExec Method = "sql.conn.exec" // MethodConnQuery is used with driver.QueryerContext.QueryContext for executing queries directly through a connection. MethodConnQuery Method = "sql.conn.query" // MethodConnPrepare is used with driver.ConnPrepareContext.PrepareContext for creating prepared statements. MethodConnPrepare Method = "sql.conn.prepare" // MethodConnBeginTx is used with driver.ConnBeginTx.BeginTx for starting a new transaction. MethodConnBeginTx Method = "sql.conn.begin_tx" // MethodConnResetSession is used with driver.SessionResetter.ResetSession to reset connection session state. MethodConnResetSession Method = "sql.conn.reset_session" // MethodTxCommit is used with driver.Tx.Commit to commit a transaction. MethodTxCommit Method = "sql.tx.commit" // MethodTxRollback is used with driver.Tx.Rollback to rollback a transaction. MethodTxRollback Method = "sql.tx.rollback" // MethodStmtExec is used with driver.StmtExecContext.ExecContext to execute a prepared statement. MethodStmtExec Method = "sql.stmt.exec" // MethodStmtQuery is used with driver.StmtQueryContext.QueryContext to query using a prepared statement. MethodStmtQuery Method = "sql.stmt.query" // MethodRows is used to track the lifecycle of driver.Rows returned by query operations. MethodRows Method = "sql.rows" )
type Option ¶
type Option interface {
// Apply sets the Option value of a config.
Apply(cfg *config)
}
Option is the interface that applies a configuration option.
func WithAttributes ¶
WithAttributes adds the attributes to each span and measurement. If multiple of WithAttributes are passed, the attributes will be extended instead of being overwriting.
func WithAttributesGetter ¶ added in v0.17.0
func WithAttributesGetter(attributesGetter AttributesGetter) Option
WithAttributesGetter takes AttributesGetter that will be called on every span creations.
func WithDisableSkipErrMeasurement ¶ added in v0.36.0
WithDisableSkipErrMeasurement controls whether driver.ErrSkip is treated as an error in metrics. When enabled, metric measurements with driver.ErrSkip will be recorded as status=ok instead of error.
This option does not affect span error recording. To suppress driver.ErrSkip in spans, use WithSpanOptions(SpanOptions{DisableErrSkip: true}).
func WithInstrumentAttributesGetter ¶ added in v0.33.0
func WithInstrumentAttributesGetter(instrumentAttributesGetter InstrumentAttributesGetter) Option
WithInstrumentAttributesGetter takes InstrumentAttributesGetter that will be called every time metric is recorded to instruments.
func WithInstrumentErrorAttributesGetter ¶ added in v0.38.0
func WithInstrumentErrorAttributesGetter(instrumentErrorAttributesGetter InstrumentErrorAttributesGetter) Option
WithInstrumentErrorAttributesGetter takes InstrumentErrorAttributesGetter that will be called every time metric is recorded to instruments.
func WithMeterProvider ¶ added in v0.13.0
func WithMeterProvider(provider metric.MeterProvider) Option
WithMeterProvider specifies a tracer provider to use for creating a tracer. If none is specified, the global provider is used.
func WithSQLCommenter ¶ added in v0.16.0
WithSQLCommenter will enable or disable context propagation for database by injecting a comment into SQL statements.
e.g., a SQL query
SELECT * from FOO
will become
SELECT * from FOO /*traceparent='00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01',tracestate='congo%3Dt61rcWkgMzE%2Crojo%3D00f067aa0ba902b7'*/
This option defaults to disable.
Notice: This option is EXPERIMENTAL and may be changed or removed in a later release.
func WithSpanNameFormatter ¶
func WithSpanNameFormatter(spanNameFormatter SpanNameFormatter) Option
WithSpanNameFormatter takes an interface that will be called on every operation and the returned string will become the span name.
func WithSpanOptions ¶
func WithSpanOptions(opts SpanOptions) Option
WithSpanOptions specifies configuration for span to decide whether to enable some features.
func WithTextMapPropagator ¶ added in v0.41.0
func WithTextMapPropagator(propagator propagation.TextMapPropagator) Option
WithTextMapPropagator specifies a text map propagator to used by the SQLCommenter option. If none is specified, the global text map propagator is used.
Notice: This option is EXPERIMENTAL and may be changed or removed in a later release.
func WithTracerProvider ¶
func WithTracerProvider(provider trace.TracerProvider) Option
WithTracerProvider specifies a tracer provider to use for creating a tracer. If none is specified, the global provider is used.
type OptionFunc ¶
type OptionFunc func(*config)
OptionFunc implements the Option interface.
func (OptionFunc) Apply ¶
func (f OptionFunc) Apply(c *config)
Apply sets the Option value of a config by calling the wrapped function.
type SpanFilter ¶ added in v0.24.0
type SpanFilter func(ctx context.Context, method Method, query string, args []driver.NamedValue) bool
SpanFilter is a function that determines whether a span should be created for a given SQL operation. It returns true if the span should be created, or false to skip span creation.
type SpanNameFormatter ¶
SpanNameFormatter supports formatting span names.
type SpanOptions ¶
type SpanOptions struct {
// Ping, if set to true, will enable the creation of spans on Ping requests.
Ping bool
// RowsNext, if set to true, will enable the creation of events in spans on RowsNext
// calls. This can result in many events.
RowsNext bool
// DisableErrSkip, if set to true, will suppress driver.ErrSkip errors in spans.
DisableErrSkip bool
// DisableQuery if set to true, will suppress db.statement in spans.
DisableQuery bool
// RecordError, if set, will be invoked with the current error, and if the func returns true
// the record will be recorded on the current span.
//
// If this is not set it will default to record all errors (possible not ErrSkip, see option
// DisableErrSkip).
RecordError func(err error) bool
// OmitConnResetSession if set to true will suppress sql.conn.reset_session spans
OmitConnResetSession bool
// OmitConnPrepare if set to true will suppress sql.conn.prepare spans
OmitConnPrepare bool
// OmitConnQuery if set to true will suppress sql.conn.query spans
OmitConnQuery bool
// OmitRows if set to true will suppress sql.rows spans
OmitRows bool
// OmitConnectorConnect if set to true will suppress sql.connector.connect spans
OmitConnectorConnect bool
// SpanFilter, if set, will be invoked before each call to create a span. If it returns
// false, the span will not be created.
SpanFilter SpanFilter
}
SpanOptions holds configuration of tracing span to decide whether to enable some features. By default all options are set to false intentionally when creating a wrapped driver and provide the most sensible default with both performance and security in mind.
👁 Image
Directories
¶
| Path | Synopsis |
|---|---|
|
example
module
|
|
|
otel-collector
module
|
|
|
stdout
module
|
|
|
internal
|
|
|
semconv
Package semconv contains semantic convention definitions and utilities for database attributes used by the otelsql package.
|
Package semconv contains semantic convention definitions and utilities for database attributes used by the otelsql package. |
