Measuring HTTP Handlers in Go

StackImpact’s profiling agent automatically and continuously profiles Golang applications’ CPU usage, memory allocations, network wait and other hot spots without requiring any special tracing mechanisms. The profiles are regularly reported to the Dashboard for historical performance analysis. The agent only needs to be initialized once when the program starts.

Automatic profiling also covers HTTP request bottlenecks as well as HTTP and database clients. This profiling is based on regular, short-term activation of a built-in trace logger that logs all blocking operations.

To measure arbitrary code segment execution time, the agent’s Segment API can be used. The agent will calculate the 95th percentile of requests over a measurement interval, typically 60 seconds, and report it to the Dashboard. More details here.

For HTTP handler execution time, the following handler wrappers are available. These are provided by the agent and use the Segment API underneath.

pattern, wrappedHandler := agent.MeasureHandler(pattern, handler)
pattern, wrappedHandlerFunc := agent.MeasureHandlerFunc(pattern, handlerFunc)

Here is an example application to demonstrate how these wrappers can be integrated.

package main

import (


func sleepHandler(w http.ResponseWriter, r *http.Request) {
	time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
	fmt.Fprintf(w, "Done")

func main() {
	// Initialize StackImpact agent
	agent := stackimpact.NewAgent();
	  AgentKey: "agent key here",
	  AppName: "Example App",

	// Start server
	http.HandleFunc(agent.MeasureHandlerFunc("/sleep", sleepHandler))
	http.Handle(agent.MeasureHandler("/tmp", http.FileServer(http.Dir("/tmp"))))

	http.ListenAndServe(":9000", nil)

The overhead is around 200ns per request.

After sending requests to the applications for a given amount of time, the following data will be available in the Dashboard.

See documentation for more information on agent configuration and the Segment API.