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 observability framework.

What is OpenTelemetry?

OpenTelemetryopen in new window is an open-source observability framework hosted by Cloud Native Computing Foundation. It is a merger of OpenCensus and OpenTracing projects.

OpenTelemetry aims to provide a single standard across all types of observability signals such as OpenTemetry tracingopen in new window, and OpenTelemetry metricsopen in new window.

OpenTelemetry specifies how to collect and send telemetry data to backend platforms. With OpenTelemetry, you can instrument your application once and then add or change vendors without changing the instrumentation.

OpenTelemetry is available for most programming languages and provides interoperability across different languages and environments.

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 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 source-available APMopen in new window 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, instrumentopen in new window more operations, for example, database queries, errors, and logs. You can also learn about OpenTelemetry Go Tracing APIopen in new window to create your own instrumentations.

Popular instrumentations:

See also:

Last Updated: