Send distributed traces and metrics to Last9 from Golang Gorilla Mux application using OpenTelemetry
Gorilla Mux is a powerful HTTP router and dispatcher for Go. This comprehensive guide will help you instrument your Gorilla Mux application with OpenTelemetry and smoothly send the traces to Last9.
Pre-requisites
You have a Gorilla Mux application.
You have signed up for Last9, and obtained the following OTLP credentials from the Integrations page:
endpoint
auth_header
Install OpenTelemetry packages
To install the required packages, run the following command:
Redis instrumentation
For setting up Redis instrumentation, first verify which go-redis version you are using.
If you are using go-redis v8 then
If you are using go-redis v9 then
Database instrumentation
If you are using sql.DB then use otelsql to instrument the database queries.
If you are using pgx then use otelpgx to instrument the database queries.
Traces
This application generates traces for the following:
Set the following environment variables to send traces and metrics to Last9:
Instrument HTTP requests
In your Gorilla Mux application, create a new file last9/instrumentation.go and add the following code:
The above code configures the OpenTelemetry SDK to use the OTLP exporter and initializes the TracerProvider.
Next, at the entry point of your application, add the following code to instrument your application:
Instrument database operations
Instrumenting with sql.DB
Add the following code to instrument the database queries. It uses the otelsql package to wrap the sql.DB object and emit traces and metrics for database queries and connections.
Instrumenting with pgx
For database instrumentation where pgx is used, use otelpgx to wrap the pgx connection pool.
Add the following code to instrument the database queries.
Instrument Redis operations
Add the following code to instrument the Redis operations. It uses the redisotel package to wrap the redis.Client object and emit traces for redis operations.
go-redis v9
If you are using go-redis v9 then use the following code.
go-redis v8
If you are using go-redis v8 then use the following code.
Run the application
Start your Gorilla Mux application by running the following command:
This will start the Gorilla Mux application and the OpenTelemetry SDK will automatically collect traces and metrics from the application.
These traces and metrics will be sent to Last9 automatically based on the environment variables set.
View traces and metrics in Last9
After running the Gorilla Mux app, you can visualize the traces and metrics in Trace Explorer.
Troubleshooting
Please get in touch with us on Discord
or Email if you have any questions.