Monitor Go Ent with OpenTelemetry

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

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

What is OpenTelemetry?

OpenTelemetry is a vendor-neutral standard on how to collect telemetry data for applications and their supporting infrastructures. OpenTelemetry was created via the merger of OpenCensus and OpenTracing projects.

OpenTelemetry aims to standardize how you collect and send telemetry data to backend platforms: tracesopen in new window, metricsopen in new window, and logsopen in new window.

Installation

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 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 an open source and blazingly fast distributed tracing tool powered by OpenTelemetry and ClickHouse. It allows you to identify and fix bugs in production faster knowing what conditions lead to which errors.

You can get startedopen in new window with Uptrace by downloading a DEB/RPM package or a pre-compiled Go binary.

What's next?

Next, instrument more operations, for example, database queries, errors, and logs. You can also learn about OpenTelemetry Go Tracing API to create your own instrumentations.

Popular instrumentations:

See also:

Last Updated: