Star us on GitHub
Star
Menu

GORM Tracing

Learn how to set up auto-instrumented tracing for your database calls using the GORM library.

1

Install the Highlight Go SDK.

Install the highlight-go package with go get.

go get -u github.com/highlight/highlight/sdk/highlight-go
Copy
2

Initialize the Highlight Go SDK.

highlight.Start starts a goroutine for recording and sending backend traces and errors. Setting your project id lets Highlight record errors for background tasks and processes that aren't associated with a frontend session.

import ( "github.com/highlight/highlight/sdk/highlight-go" ) func main() { // ... highlight.SetProjectID("<YOUR_PROJECT_ID>") highlight.Start( highlight.WithServiceName("my-app"), highlight.WithServiceVersion("git-sha"), ) defer highlight.Stop() // ... }
Copy
3

Initialize the GORM library with the Highlight hooks

Import the Highlight GORM library and call the SetupGORMTracing hook with any attributes wanted for context.

import ( "github.com/highlight/highlight/sdk/highlight-go" htrace "github.com/highlight/highlight/sdk/highlight-go/trace" "go.opentelemetry.io/otel/attribute" ) DB, err = gorm.Open(<DB_SETTINGS>) if err := htrace.SetupGORMTracing(DB, attribute.String(highlight.ProjectIDAttribute, <YOUR_PROJECT_ID>)); err != nil { highlight.RecordError(ctx, err) }
Copy
4

Call GORM with the trace context

When making any database calls with GORM, attach a WithContext hook to provide more data about the trace.

DB.WithContext(ctx).Find(&user)
Copy
5

Verify your backend traces are being recorded.

Visit the highlight traces portal and check that backend traces are coming in.