Iris
Send distributed traces and metrics to Last9 from Golang Iris application using OpenTelemetry
Iris is a fast, flexible HTTP server framework written in Go (Golang). This comprehensive guide will help you instrument your Iris application with OpenTelemetry and smoothly send the traces to a Last9 cluster. You can also check out the example application on GitHub↗.
Pre-requisites
- You have a Iris application.
- You have signed up for Last9, created a cluster, 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:
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
Using these packages, you can instrument your iris application to send traces to Last9.
Traces
This application generates traces for the following:
- HTTP requests using last9/otelMiddleware
- Database queries using otelsql
- Redis commands using redisotel
For HTTP requests, wrap the iris router with the last9.otelMiddleware
middleware.
Refer to main.go for more details.
For database queries, use the otelsql
package to wrap the sql.DB
object.
Refer to
initDB()
in users/controller.go for more details.
For Redis commands, use the redisotel
package to wrap the redis.Client
object.
Refer to
initRedis()
in users/controller.go for more details.
Set the environment variables
Set the following environment variables:
These environment variables are used to configure the OpenTelemetry SDK to send traces and metrics to Last9.
Instrument HTTP requests
Setup instrumentation for iris application using last9/instrumentation.
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:
Now, add the otel iris middleware to 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.
Refer to users/controller.go for more details.
Instrumenting with pgx
For database instrumentation where pgx is used, we use otelpgx to wrap the pgx connection pool. Add the following code to instrument the database queries.
Refer to the complete example using pgx adapter and Otel instrumentation here.
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 commands.
go-redis v9
If you are using go-redis
v9 then use the following code.
Refer to users/controller.go for more details.
go-redis v8
If you are using go-redis
v8 then use the following code.
Refer to main.go for more details.
Run the application
Start your iris application by running the following command:
This will start the iris application and the OpenTelemetry SDK will automatically collect traces and metrics from the iris application. These traces will be sent to Last9 automatically based on the environment variables set.
View traces in Last9
After running the iris app, you can visualize the traces in Last9’s APM dashboard.
Troubleshooting
Please get in touch with us on Discord or Email if you have any questions.