OpenTelemetry GORM monitoring [otelgorm]

Vladimir Mihailenco
August 01, 2024
2 min read

Learn how to monitor GORM performance using OpenTelemetry GORM instrumentation.

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. For cloud-native applications running on Kubernetes, the OpenTelemetry Operator provides simplified deployment and auto-instrumentation features. Follow the OpenTelemetry Kubernetes guide to correlate ORM performance with pod- and node-level metrics.

What is GORM?

GORM is a popular, high-level ORM (Object-Relational Mapping) library for Go, and OpenTelemetry GORM provides a set of libraries and tools for instrumenting GORM applications to collect telemetry data such as traces, metrics, and logs.

GORM instrumentation

To install GORM OpenTelemetry instrumentation:

shell
go get github.com/uptrace/opentelemetry-go-extra/otelgorm

Usage

OpenTelemetry GORM is designed is easy to use and provides a simple API for instrumenting GORM applications, making it possible for developers to quickly add observability to their applications without having to write a lot of code.

To instrument GORM, you need to install the plugin provided by otelgorm:

go
import (
    "github.com/uptrace/opentelemetry-go-extra/otelgorm"
    "gorm.io/driver/sqlite"
    "gorm.io/gorm"
)

db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
if err != nil {
    panic(err)
}

if err := db.Use(otelgorm.NewPlugin()); err != nil {
    panic(err)
}

And then use db.WithContext(ctx) to propagate the active span via context:

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

What is Uptrace?

Uptrace is a distributed tracing tool that supports distributed tracing, metrics, and logs. You can use it to monitor applications and troubleshoot issues.

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?

GORM instrumentation now provides detailed insights into your database operations, including query tracing and performance metrics. For lower-level SQL monitoring, check out database/sql instrumentation, or explore Ent for an alternative ORM approach.