Send distributed traces and metrics to Last9 from Golang fasthttp application using OpenTelemetry
fasthttp is a fast, flexible HTTP server framework written in Go (Golang). This comprehensive guide will help you instrument your fasthttp 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 fasthttp 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 fasthttp application to send traces to Last9.
Traces
This application generates traces for the following:
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 fasthttp 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.
Start your fasthttp application by running the following command:
This will start the fasthttp application and the OpenTelemetry SDK will automatically collect traces and metrics from the fasthttp application.
These traces will be sent to Last9 automatically based on the environment variables set.
View traces in Last9
After running the fasthttp 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.