Go Echo OpenTelemetry Integration Guide
Table of Contents
Installation
Install the APIToolkit echo SDK using the following command go get
command:
go get github.com/apitoolkit/apitoolkit-go/echo
Configuration
Before configuration open telemetery and setting up the APItoolkit middleware, you need to configure a few environment variables. These variables provide essential information for setting up openTelemetry and APItoolkit.
OTEL_RESOURCE_ATTRIBUTES="at-project-key=YOUR_API_KEY" # Your apitoolkit API key
OTEL_SERVICE_NAME="apitoolkit-otel-go-demo" # Service name for your the service you're integrating in
OTEL_SERVICE_VERSION="0.0.1" # Your application's service version
Usage
After setting up the environment variables, you can configure the OpenTelemetry SDK and APItoolkit middleware like so:
package main
import (
"log"
apitoolkit "github.com/apitoolkit/apitoolkit-go/echo"
"github.com/labstack/echo/v4"
_ "github.com/joho/godotenv/autoload" // autoload .env file for otel configuration
)
func main() {
// Configure OpenTelemetry
shutdown, err := apitoolkit.ConfigureOpenTelemetry()
if err != nil {
log.Printf("error configuring openTelemetry: %v", err)
}
defer shutdown()
router := echo.New()
// Register APItoolkit's middleware
router.Use(apitoolkit.Middleware(apitoolkit.Config{
RedactHeaders: []string{"Authorization", "X-Api-Key"},
RedactRequestBody: []string{"password", "credit_card"},
RedactResponseBody: []string{"password", "credit_card"},
}))
router.GET("/:slug/test", func(c echo.Context) error {
return c.String(http.StatusOK, "Ok, success!")
})
router.Start(":8000")
}
All Environment Variables
Set the following environment variables in your application to enable the SDK:
Variable Name | Description | Required | Example |
---|---|---|---|
OTEL_RESOURCE_ATTRIBUTES | APItoolkit project key (at-project-key= ) | Yes | at-project-key=my-api-key |
OTEL_SERVICE_NAME | The name of the service being monitored | No | example-chi-server |
OTEL_SERVICE_VERSION | The version of your application or service | No | 0.0.1 |
OTEL_EXPORTER_OTLP_ENDPOINT | The grpc endpoint for the OpenTelemetry collector. | No | otelcol.apitoolkit.io:4317 |
OTEL_TRACES_ENABLED | Enable or disable tracing | No | true |
OTEL_METRICS_ENABLED | Enable or disable metrics | No | true |
OTEL_LOG_LEVEL | The log level for the SDK (Set to debug to enable debug logs) | No | info |
OTEL_EXPORTER_OTLP_METRICS_PERIOD | The period at which metrics are exported. | No | 30s |
OTEL_PROPAGATORS | The propagators to use for tracing. | No | tracecontext,baggage |
All Middleware Configuration Fields
The middleware configuration specifies how the APItoolkit SDK should handle requests and responses. Below are the available fields:
Field Name | Type | Description | Default Value | Example |
---|---|---|---|---|
Debug | bool | Enable detailed logs during development | false | true |
ServiceName | string | Name of the service being monitored | - | "example-chi-server" |
ServiceVersion | string | Version of the service | - | "0.0.1" |
Tags | []string | Additional tags for contextual information | [] | []string{"env:dev", "team:backend"} |
CaptureRequestBody | bool | Enable capturing of request body | false | true |
CaptureResponseBody | bool | Enable capturing of response body | false | true |
RedactHeaders | []string | List of headers to redact | [] | []string{"Authorization", "X-Api-Key"} |
RedactRequestBody | []string | JSONPath list of request body fields to redact | [] | []string{"$.password", "$.credit_card"} |
RedactResponseBody | []string | JSONPath list of response body fields to redact | [] | []string{"$.password", "$.credit_card"} |
Tips
-
Remember to keep your APIToolkit project key (
at-project-key
) secure and not expose it in public repositories or logs.