{"id":13809017,"url":"https://github.com/XSAM/otelsql","last_synced_at":"2025-05-14T05:32:56.568Z","repository":{"id":38000702,"uuid":"350382700","full_name":"XSAM/otelsql","owner":"XSAM","description":"OpenTelemetry instrumentation for database/sql","archived":false,"fork":false,"pushed_at":"2024-10-24T16:59:18.000Z","size":789,"stargazers_count":305,"open_issues_count":11,"forks_count":50,"subscribers_count":5,"default_branch":"main","last_synced_at":"2024-10-29T17:35:58.322Z","etag":null,"topics":["golang","instrumentation","opentelemetry","sql"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/XSAM.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":"CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-03-22T14:54:57.000Z","updated_at":"2024-10-22T19:59:34.000Z","dependencies_parsed_at":"2024-01-29T18:56:49.057Z","dependency_job_id":"51aedabf-d05f-411b-beba-5ea41c0cf640","html_url":"https://github.com/XSAM/otelsql","commit_stats":{"total_commits":293,"total_committers":18,"mean_commits":16.27777777777778,"dds":0.6655290102389079,"last_synced_commit":"c3358219f8a42e28ee40f90115722c560a9d434a"},"previous_names":[],"tags_count":38,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/XSAM%2Fotelsql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/XSAM%2Fotelsql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/XSAM%2Fotelsql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/XSAM%2Fotelsql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/XSAM","download_url":"https://codeload.github.com/XSAM/otelsql/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224185474,"owners_count":17270062,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["golang","instrumentation","opentelemetry","sql"],"created_at":"2024-08-04T01:01:57.979Z","updated_at":"2025-05-14T05:32:56.550Z","avatar_url":"https://github.com/XSAM.png","language":"Go","readme":"# otelsql\n\n[![ci](https://github.com/XSAM/otelsql/actions/workflows/ci.yaml/badge.svg?branch=main)](https://github.com/XSAM/otelsql/actions/workflows/ci.yaml)\n[![codecov](https://codecov.io/gh/XSAM/otelsql/branch/main/graph/badge.svg?token=21S08PK9K0)](https://codecov.io/gh/XSAM/otelsql)\n[![Go Report Card](https://goreportcard.com/badge/github.com/XSAM/otelsql)](https://goreportcard.com/report/github.com/XSAM/otelsql)\n[![Documentation](https://godoc.org/github.com/XSAM/otelsql?status.svg)](https://pkg.go.dev/mod/github.com/XSAM/otelsql)\n\nIt is an OpenTelemetry instrumentation for Golang `database/sql`, a port from https://github.com/open-telemetry/opentelemetry-go-contrib/pull/505.\n\nIt instruments traces and metrics.\n\n## Install\n\n```bash\n$ go get github.com/XSAM/otelsql\n```\n\n## Usage\n\nThis project provides four different ways to instrument `database/sql`:\n\n`otelsql.Open`, `otelsql.OpenDB`, `otesql.Register` and `otelsql.WrapDriver`.\n\nAnd then use `otelsql.RegisterDBStatsMetrics` to instrument `sql.DBStats` with metrics.\n\n```go\ndb, err := otelsql.Open(\"mysql\", mysqlDSN, otelsql.WithAttributes(\n\tsemconv.DBSystemMySQL,\n))\nif err != nil {\n\tpanic(err)\n}\ndefer db.Close()\n\nerr = otelsql.RegisterDBStatsMetrics(db, otelsql.WithAttributes(\n\tsemconv.DBSystemMySQL,\n))\nif err != nil {\n\tpanic(err)\n}\n```\n\nCheck [Option](https://pkg.go.dev/github.com/XSAM/otelsql#Option) for more features like adding context propagation to SQL queries when enabling [`WithSQLCommenter`](https://pkg.go.dev/github.com/XSAM/otelsql#WithSQLCommenter).\n\nSee [godoc](https://pkg.go.dev/mod/github.com/XSAM/otelsql) for details.\n\n## Blog\n\n[Getting started with otelsql, the OpenTelemetry instrumentation for Go SQL](https://opentelemetry.io/blog/2024/getting-started-with-otelsql), is a blog post that explains how to use otelsql in miutes.\n\n## Examples\n\nThis project provides two docker-compose examples to show how to use it.\n\n- [The stdout example](example/stdout) 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.\n- [The otel-collector example](example/otel-collector) 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.\n\n## Semantic Convention Stability Migration\n\nThe 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.\n\nCheck the [CHANGELOG.md](CHANGELOG.md) for more details.\n\n## Trace Instruments\n\nIt creates spans on corresponding [methods](https://pkg.go.dev/github.com/XSAM/otelsql#Method).\n\nUse [`SpanOptions`](https://pkg.go.dev/github.com/XSAM/otelsql#SpanOptions) to adjust creation of spans.\n\n### Trace Semantic Convention Stability\n\nThe instrumentation supports different OpenTelemetry semantic convention stability levels, configured through the `OTEL_SEMCONV_STABILITY_OPT_IN` environment variable:\n\n| Setting | Description |\n|---------|-------------|\n| empty (default) | Only uses `db.statement` attribute. |\n| `database/dup` | Uses both `db.statement` and `db.query.text` attributes. |\n| `database` | Uses `db.query.text` attribute. |\n\n## Metric Instruments\n\nTwo types of metrics are provided depending on the semantic convention stability setting:\n\n### Legacy Metrics (default)\n- **db.sql.latency**: The latency of calls in milliseconds\n  - Unit: milliseconds\n  - Attributes: `method` (method name), `status` (ok, error)\n\n### OpenTelemetry Semantic Convention Metrics\n- [**db.client.operation.duration**](https://github.com/open-telemetry/semantic-conventions/blob/v1.32.0/docs/database/database-metrics.md#metric-dbclientoperationduration): Duration of database client operations\n  - Unit: seconds\n  - Attributes: [`db.operation.name`](https://github.com/open-telemetry/semantic-conventions/blob/v1.32.0/docs/attributes-registry/db.md#db-operation-name) (method name), [`error.type`](https://github.com/open-telemetry/semantic-conventions/blob/v1.32.0/docs/attributes-registry/error.md#error-type) (if error occurs)\n\n### Connection Statistics Metrics (from Go's sql.DBStats)\n- **db.sql.connection.max_open**: Maximum number of open connections to the database\n- **db.sql.connection.open**: The number of established connections\n  - Attributes: `status` (idle, inuse)\n- **db.sql.connection.wait**: The total number of connections waited for\n- **db.sql.connection.wait_duration**: The total time blocked waiting for a new connection (ms)\n- **db.sql.connection.closed_max_idle**: The total number of connections closed due to SetMaxIdleConns\n- **db.sql.connection.closed_max_idle_time**: The total number of connections closed due to SetConnMaxIdleTime\n- **db.sql.connection.closed_max_lifetime**: The total number of connections closed due to SetConnMaxLifetime\n\n### Metric Semantic Convention Stability\n\nThe instrumentation supports different OpenTelemetry semantic convention stability levels, configured through the `OTEL_SEMCONV_STABILITY_OPT_IN` environment variable:\n\n| Setting | Metrics Emitted | Description |\n|---------|----------------|-------------|\n| empty (default) | `db.sql.latency` only | Only uses legacy metric|\n| `database/dup` | Both `db.sql.latency` and `db.client.operation.duration` | Emits both legacy and new OTel metric formats |\n| `database` | `db.client.operation.duration` only | Only uses the new OTel semantic convention metric |\n\nConnection statistics metrics (`db.sql.connection.*`) are always emitted regardless of the stability setting.\n\nThis allows users to gradually migrate to the new OpenTelemetry semantic conventions while maintaining backward compatibility with existing dashboards and alerts.\n\n## Error Type Attribution\n\nWhen 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:\n\n1. **For standard driver errors**: Special handling for common driver errors:\n   - `database/sql/driver.ErrBadConn`\n   - `database/sql/driver.ErrSkip`\n   - `database/sql/driver.ErrRemoveArgument`\n\n2. **For custom errors**: The fully qualified type name is used (e.g., `github.com/your/package.CustomError`).\n\n3. **For built-in errors**: The type name is used (e.g., `*errors.errorString` for errors created with `errors.New()`).\n\n**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.\n\n## Compatibility\n\nThis project is tested on the following systems.\n\n| OS      | Go Version | Architecture |\n| ------- | ---------- | ------------ |\n| Ubuntu  | 1.24       | amd64        |\n| Ubuntu  | 1.23       | amd64        |\n| Ubuntu  | 1.24       | 386          |\n| Ubuntu  | 1.23       | 386          |\n| MacOS   | 1.24       | amd64        |\n| MacOS   | 1.23       | amd64        |\n| Windows | 1.24       | amd64        |\n| Windows | 1.23       | amd64        |\n| Windows | 1.24       | 386          |\n| Windows | 1.23       | 386          |\n\nWhile this project should work for other systems, no compatibility guarantees\nare made for those systems currently.\n\nThe project follows the [Release Policy](https://golang.org/doc/devel/release#policy) to support major Go releases.\n\n## Why port this?\n\nBased on [this comment](https://github.com/open-telemetry/opentelemetry-go-contrib/pull/505#issuecomment-800452510), 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.\n\nTherefore, I host this module independently for convenience and make improvements based on users' feedback.\n\n## Communication\n\nI use GitHub discussions/issues for most communications. Feel free to contact me on CNCF slack.\n","funding_links":[],"categories":["Go"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FXSAM%2Fotelsql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FXSAM%2Fotelsql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FXSAM%2Fotelsql/lists"}