Documentation Index
Fetch the complete documentation index at: https://launchdarkly-preview.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
GORM is a popular ORM for Go that allows you to interact with databases using Go structs. It’s a great way to improve the ergonomics of interacting with your database, but it does come with some tradeoffs.
When you use an ORM, you are abstracting away the details of the actual queries. This can make it difficult to diagnose issues and optimize for performance.
Using OpenTelemetry, we can collect observability data about how our database is performing and help identify and fix issues quickly.
In this blog post, we’ll walk through how to integrate OpenTelemetry with GORM to provide traces and metrics for your database.
Why Observability Matters for Databases
It may not be obvious why observability matters for databases, so let’s quickly cover the benefits. We can collect a few types of data about our database:- Infrastructure metrics (e.g. CPU, memory, disk, etc.)
- Query performance (latency, errors, etc.)
- Resource utilization (connections, threads, etc.)
- Slow queries
- High error rates
- Connection pool bottlenecks
- Resource contention
How to Integrate Open
Telemetry with GORM The GORM OpenTelemetry plugin is a great way to integrate OpenTelemetry with GORM. It’s easy to install and use, and it provides a lot of useful data about your database. First, install the plugin:How to Use Traces and Metrics to Diagnose and Optimize
Now that we have traces and metrics, how do we use them to diagnose and optimize our database? For our use case, we’ll use LaunchDarkly to visualize and work with the data.Working with Traces
The GORM plugin will emit spans for each database interaction. If the query is part of an existing trace, the span will be connected to that trace. Here is an example of a web request that triggered a database query:
db key. We get information about the query that can be used to help understand and optimize it. We even get the SQL statement to so we have the exact query to run in a debugging session.
Aggregated Traces
Viewing an individual trace is helpful, but it’s often useful to see aggregate data for database interactions. Since we have attributes likeduration, db.sql.table, and db.operation, we can use these to group and aggregate data.
