APItoolkit full color logo
Sign Up

Go Gorilla Mux OpenTelemetry Integration Guide

Table of Contents

Installation

Install the APIToolkit gorilla mux SDK using the following command go get command:

go get github.com/apitoolkit/apitoolkit-go/gorilla

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"
	"net/http"

	apitoolkit "github.com/apitoolkit/apitoolkit-go/gorilla"
	"github.com/gorilla/mux"
  _ "github.com/joho/godotenv/autoload" // autoload .env file for otel configuration

)

func main() {
	shutdown, err := apitoolkit.ConfigureOpenTelemetry()
	if err != nil {
		log.Printf("error configuring openTelemetry: %v", err)
	}
	defer shutdown()

	router := mux.NewRouter()
	// 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.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusOK)
		w.Write([]byte("ok"))
	})
	http.Handle("/", router)
	http.ListenAndServe(":8000", router)

}

All Environment Variables

Set the following environment variables in your application to enable the SDK:

Variable NameDescriptionRequiredExample
OTEL_RESOURCE_ATTRIBUTESAPItoolkit project key (at-project-key=)Yesat-project-key=my-api-key
OTEL_SERVICE_NAMEThe name of the service being monitoredNoexample-chi-server
OTEL_SERVICE_VERSIONThe version of your application or serviceNo0.0.1
OTEL_EXPORTER_OTLP_ENDPOINTThe grpc endpoint for the OpenTelemetry collector.Nootelcol.apitoolkit.io:4317
OTEL_TRACES_ENABLEDEnable or disable tracingNotrue
OTEL_METRICS_ENABLEDEnable or disable metricsNotrue
OTEL_LOG_LEVELThe log level for the SDK (Set to debug to enable debug logs)Noinfo
OTEL_EXPORTER_OTLP_METRICS_PERIODThe period at which metrics are exported.No30s
OTEL_PROPAGATORSThe propagators to use for tracing.Notracecontext,baggage

All Middleware Configuration Fields

The middleware configuration specifies how the APItoolkit SDK should handle requests and responses. Below are the available fields:

Field NameTypeDescriptionDefault ValueExample
DebugboolEnable detailed logs during developmentfalsetrue
ServiceNamestringName of the service being monitored-"example-chi-server"
ServiceVersionstringVersion of the service-"0.0.1"
Tags[]stringAdditional tags for contextual information[][]string{"env:dev", "team:backend"}
CaptureRequestBodyboolEnable capturing of request bodyfalsetrue
CaptureResponseBodyboolEnable capturing of response bodyfalsetrue
RedactHeaders[]stringList of headers to redact[][]string{"Authorization", "X-Api-Key"}
RedactRequestBody[]stringJSONPath list of request body fields to redact[][]string{"$.password", "$.credit_card"}
RedactResponseBody[]stringJSONPath list of response body fields to redact[][]string{"$.password", "$.credit_card"}

Tips

  1. Remember to keep your APIToolkit project key (at-project-key) secure and not expose it in public repositories or logs.