Skip to content

Commit

Permalink
feat: add otelsql instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Oct 15, 2021
1 parent c86e9dd commit 68269f9
Show file tree
Hide file tree
Showing 15 changed files with 1,492 additions and 0 deletions.
61 changes: 61 additions & 0 deletions otelsql/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
[![PkgGoDev](https://pkg.go.dev/badge/github.com/uptrace/opentelemetry-go-extra/otelsql)](https://pkg.go.dev/github.com/uptrace/opentelemetry-go-extra/otelsql)

# OpenTelemetry Go instrumentation for database/sql package

otelsql instrumentation records database queries (including `Tx` and `Stmt` queries) and reports
`DBStats` metrics.

## Installation

```shell
go get github.com/uptrace/opentelemetry-go-extra/otelsql
```

## Usage

To instrument database/sql, you need to connect to a database using the API provided by this
package:

- `sql.Open(driverName, dsn)` becomes `otelsql.Open(driverName, dsn)`.
- `sql.OpenDB(connector)` becomes `otelsql.OpenDB(connector)`.

```go
import (
"github.com/uptrace/opentelemetry-go-extra/otelsql"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
)

db, err := otelsql.Open("sqlite", "file::memory:?cache=shared",
otelsql.WithAttributes(semconv.DBSystemSqlite),
otelsql.WithDBName("mydb"))
if err != nil {
panic(err)
}
```

And then use context-aware API to propagate the active span via
[context](https://docs.uptrace.dev/guide/go.html#context):

```go
var num int
if err := db.QueryRowContext(ctx, "SELECT 42").Scan(&num); err != nil {
panic(err)
}
```

See [example](/example/) for details.

## Options

Both [otelsql.Open](https://pkg.go.dev/github.com/uptrace/opentelemetry-go-extra/otelsql#Open) and
[otelsql.OpenDB](https://pkg.go.dev/github.com/uptrace/opentelemetry-go-extra/otelsql#OpenDB) accept
the same [options](https://pkg.go.dev/github.com/uptrace/opentelemetry-go-extra/otelsql#Option):

- [WithAttributes](https://pkg.go.dev/github.com/uptrace/opentelemetry-go-extra/otelsql#WithAttributes)
configures attributes that are used to create a span.
- [WithDBName](https://pkg.go.dev/github.com/uptrace/opentelemetry-go-extra/otelsql#WithDBName)
configures a `db.name` attribute.
- [WithDBSystem](https://pkg.go.dev/github.com/uptrace/opentelemetry-go-extra/otelsql#WithDBSystem)
configures a `db.system` attribute. When possible, you should prefer using WithAttributes and
[semconv](https://pkg.go.dev/go.opentelemetry.io/otel/semconv/v1.4.0), for example,
`otelsql.WithAttributes(semconv.DBSystemSqlite)`.
Loading

0 comments on commit 68269f9

Please sign in to comment.