DEV Community

Cover image for OpenTelemetry database sql [otelsql]
Alexandr Bandurchin for Uptrace

Posted on • Originally published at uptrace.dev

OpenTelemetry database sql [otelsql]

OpenTelemetry database SQL provides libraries and tools for instrumenting SQL databases such as PostgreSQL, MySQL, and Microsoft SQL Server to collect telemetry data.

database/sql package provides a lightweight and idiomatic interface to row-oriented databases in Golang. It supports most popular relational DBMS via 3-rd party drivers.

This article will teach you how to monitor database/sql performance using OpenTelemetry observability framework.

What is OpenTelemetry?

OpenTelemetry is an open-source observability framework that aims to standardize and simplify the collection, processing, and export of telemetry data from applications and systems.

OpenTelemetry supports multiple programming languages and platforms, making it suitable for a wide range of applications and environments.

OpenTelemetry enables developers to instrument their code and collect telemetry data, which can then be exported to various OpenTelemetry backends or observability platforms for analysis and visualization. When deploying on Kubernetes, the OpenTelemetry Operator streamlines the setup and configuration of OpenTelemetry instrumentation across your cluster.

OpenTelemetry database/sql

To install otelsql instrumentation:

go get github.com/uptrace/opentelemetry-go-extra/otelsql
Enter fullscreen mode Exit fullscreen mode

Using database/sql

OpenTelemetry SQL allows developers to monitor the performance and behavior of their SQL databases, providing insights into database usage patterns, resource utilization, and performance bottlenecks

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

sql otelsql
sql.Open(driverName, dsn) otelsql.Open(driverName, dsn)
sql.OpenDB(connector) otelsql.OpenDB(connector)
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)
}

// db is *sql.DB
Enter fullscreen mode Exit fullscreen mode

And then use context-aware API to propagate the active span via context:

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

What is Uptrace?

Uptrace is a OpenTelemetry APM that supports distributed tracing, metrics, and logs. You can use it to monitor applications and troubleshoot issues. For Go instrumentation, see the OpenTelemetry Go guide and compare with top APM tools.

Uptrace Overview

Uptrace comes with an intuitive query builder, rich dashboards, alerting rules with notifications, and integrations for most languages and frameworks.

Uptrace can process billions of spans and metrics on a single server and allows you to monitor your applications at 10x lower cost.

In just a few minutes, you can try Uptrace by visiting the cloud demo (no login required) or running it locally with Docker. The source code is available on GitHub.

What's next?

With database/sql instrumentation enabled, you can track query performance, identify slow queries, and optimize database interactions. For ORM-level monitoring, explore GORM instrumentation or Ent instrumentation for more advanced database operations.

Top comments (0)