API Governance | May 2, 2025 | 5 min read
We’ve updated the Treblle Go SDK to support Treblle V3, with a full rewrite focused on performance, ease of use, and better integration with Go tools and frameworks. This guide walks you through the changes, setup, and new features.
We’re releasing a new version of the Treblle Go SDK, rewritten to support Treblle V3 and to better fit the needs of Go developers.
The SDK is designed for easy integration with both the Go standard library and popular frameworks, such as Gorilla Mux. It uses Go 1.21.
It provides middleware, automatic data masking, async support, and straightforward configuration.
Version 2 of the Treblle Go SDK introduces breaking changes compared to previous versions. If you are upgrading from v1, you will need to update your configuration, middleware usage, and possibly some integration points.
Please review the new setup and example code before upgrading.
You can configure the SDK using both an SDK token and an API key. Additional options are available for debug mode, masking extra fields, and enabling async processing.
treblle.Configure(treblle.Configuration{
SDK_TOKEN: "your-sdk-token",
API_KEY: "your-api-key",
Debug: true,
AdditionalFieldsToMask: []string{"bank_account", "ssn", "credit_card"},
AsyncProcessingEnabled: true,
})
treblle.HandleFunc
for route patterns and wrap with treblle.Middleware
.treblle.Middleware
to your router with r.Use(treblle.Middleware)
.The SDK normalizes route patterns for consistent analytics, regardless of the router.
Sensitive fields (passwords, API keys, credit card numbers, etc.) are masked automatically in requests and responses. You can add custom fields to the mask list at configuration.
Monitoring and data transmission to Treblle run asynchronously by default. You can control concurrency and timeouts. There is also a graceful shutdown mechanism to ensure all data is sent before the application exits.
The SDK collects errors from your handlers, including panics, and reports them to Treblle. Batch error collection is supported, and errors are included in the analytics data.
A CLI tool is available for inspecting the SDK configuration and debugging. It supports environment variables and shows masked API keys and tokens.
treblle-go -debug
You can specify environments where Treblle should not track requests (e.g., dev or test) using configuration or environment variables.
You can associate requests with specific customers and trace IDs by setting custom headers before Treblle processes the request. This is useful for user tracking, correlation, and debugging across distributed systems.
Here’s how you can do it with a custom middleware:
func addTreblleHeadersMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Example: Extract user ID from your authentication/session logic
userID := r.Header.Get("X-User-ID")
if userID != "" {
// Set Treblle user ID header
r.Header.Set("treblle-user-id", userID)
// Set Treblle trace ID header (from request or generate one)
traceID := r.Header.Get("X-Trace-ID")
if traceID != "" {
r.Header.Set("treblle-tag-id", traceID)
} else {
r.Header.Set("treblle-tag-id", "trace-" + strconv.Itoa(int(time.Now().UnixNano())))
}
}
next.ServeHTTP(w, r)
})
}
Apply your custom middleware before the Treblle middleware:
api.Use(addTreblleHeadersMiddleware)
api.Use(treblle.Middleware)
Now, any request with X-User-ID
and optionally X-Trace-ID
headers will be tracked in Treblle with customer and trace information.
The Treblle Go SDK is designed to work seamlessly with Go’s concurrency model.
If you want to understand Concurrency in Go, check out our latest blog on the same.
All monitoring and data transmission to Treblle are handled asynchronously, so API handlers are never blocked or slowed down by network calls or processing overhead.
golang.org/x/sync/semaphore
) to control concurrency.sync.WaitGroup
and context timeouts, ensuring clean shutdowns and no resource leaks.Because all Treblle operations are offloaded to background goroutines, the SDK adds virtually no overhead to your request/response cycle. Your API endpoints return responses as fast as they would without Treblle, and monitoring is handled independently.
You can adjust concurrency settings in the configuration:
treblle.Configure(treblle.Configuration{
// ...
AsyncProcessingEnabled: true,
MaxConcurrentProcessing: 20, // Increase or decrease as needed
})
By default, async processing is enabled and tuned for typical production workloads.
The repository includes practical examples for both the standard library and Gorilla Mux:
You can install the SDK using
go get github.com/treblle/treblle-go
Configure it, add the middleware, and you’re ready to use Treblle V3 features in your Go API.
For more information, see the README.
💡
If you have feedback or want to contribute, please visit the GitHub repository and do drop a star if you enjoy using the SDK.
Inconsistent API endpoints slow development, confuse teams, and frustrate users. This guide breaks down the principles and best practices for designing clean, predictable, and scalable API paths that improve developer experience and reduce errors.
In this blog, we explore how Go’s concurrency model helps us do more with less. Through a fun kitchen party analogy, you’ll learn about goroutines, channels, waitgroups, semaphores, and how to use them together to build efficient, concurrent programs.
Tired of tweaking AI prompts just to get decent results? I was too—so Rahul built AI Prompt Enhancer, a free tool that turns rough instructions into clear, structured prompts. Here’s how it works, why he built it, and how tools like API Insights and Treblle helped him along the way.