Monitor Go Ent with OpenTelemetry

Ent is a simple, yet powerful ORM for modeling and querying data. It can generate Go code from an Ent schema for any database.

This article will teach you how to monitor Ent performance using OpenTelemetry Ent instrumentation.

What is OpenTelemetry?

OpenTelemetryopen in new window 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 backendsopen in new window or observability platforms for analysis and visualization.

Go Ent instrumentation

Because Ent works on top of database/sql, you can use otelsql instrumentation with Ent:

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

Usage

To instrument Ent database client, use OpenTelemetry database/sql instrumentation to patch the database driver you are using.

Exampleopen in new window:

- client, err := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
- if err != nil {
- 	log.Fatalf("failed opening connection to sqlite: %v", err)
- }
+ import (
+ 	"github.com/uptrace/opentelemetry-go-extra/otelsql"
+ 	semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
+ )

+ db, err := otelsql.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1",
+ 	otelsql.WithAttributes(semconv.DBSystemSqlite),
+ 	otelsql.WithDBName("mydb"))
+ if err != nil {
+ 	panic(err)
+ }
+
+ drv := entsql.OpenDB(dialect.SQLite, db)
+ client := ent.NewClient(ent.Driver(drv))

What is Uptrace?

Uptrace is a DataDog alternativeopen in new window 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 demoopen in new window (no login required) or running it locally with Dockeropen in new window. The source code is available on GitHubopen in new window.

What's next?

Next, instrument more operations to get a more detailed picture. Try to prioritize network calls, disk operations, database queries, error and logs.

You can also create your own instrumentations using OpenTelemetry Go Tracing APIopen in new window.

Last Updated: 7/25/2024, 12:36:08 PM
Get insights and updates in your inbox: