Automatic Database Call Tracing in Go Applications

Some of the most common root causes of performance bottlenecks in web applications are databases. This is true for any language, and Golang is no exception.

To trace database call performance in Go, legacy tracing mechanisms should be added around each query or call, which will decrease the readability of the code. This mechanism is also limited in identifying where exactly the time is spent in database clients. For example, there are cases when it is not the database response time, but the DNS response is slow.

Automatic tracing with StackImpact

StackImpact’s production profiler allows develops to trace database calls automatically using Go’s builtin tracing, without adding any implicit timing functions. All SQL clients using database/sql abstraction are currently supported, as well as most popular MongoDB and Redis driver packages.

Regular and anomaly-triggered tracing samples collected by the StackImpact agent are reported to the Dashboard, where they can be analyzed in a historically comparable form. This is important for detecting performance regression due to, for example, database size growth over time, query complexity, configuration changes after releases, etc.

Here is how the information is presented in the Dashboard, for MongoDB calls, for example.

The source code for the example application is available in the agent’s GitHub repository at https://github.com/stackimpact/stackimpact-go.

Setting up the agent

Sign up for a free account, get the agent with go get github.com/stackimpact/stackimpact-go, import stackimpact and add the following code to your application:

agent := stackimpact.NewAgent();
agent.Start(stackimpact.Options{
	AgentKey: "agent key here",
	AppName: "MyGoApp",
})

See documentation for detailed setup instructions.

Follow us on Twitter @stackimpact for updates on performance profiling and monitoring of production Golang apps.