Skip to main content

Manual instrumentation guide (0.7.0 or later)

To trace libraries that whatap-go-inst does not support, or to trace specific sections yourself, use the golang-apm library. If automatic instrumentation is enough, you do not need this guide.

In 0.7.0 or later, do not add github.com/whatap/go-api. If go-api is present, automatic monitoring is not applied.

Add golang-apm​

The library is included in the installation file on the build server. Add it by running the following commands in the project directory. On ARM64, replace amd64 with arm64 in the first line.

B=/opt/whatap-golang-apm-0.7.0-linux-amd64
GOPROXY="file://$B/goproxy,$(go env GOPROXY)" GONOSUMDB=github.com/whatap/golang-apm \
go get github.com/whatap/golang-apm@v0.7.0
Note

This command adds golang-apm to go.mod. If the go version is lower than 1.26, it is raised to 1.26. Your code then follows Go 1.26 behavior, so review the changes and test. From then on, this project always requires Go 1.26 or later to build.

Caution

Import golang-apm only in a separate file that contains only tracing functions. The build tool (whatap-go-inst) does not add monitoring code to files that import golang-apm. If you import it in the file containing the main function, the agent does not start even though the build succeeds and shows instrumented build complete. Also do not import it in files that create routers or connect to databases.

Create tracing functions​

First, create the tracing functions in a separate file. Change the package name to match your project.

// whatap_trace.go
package main

import (
"context"

"github.com/whatap/golang-apm/method"
"github.com/whatap/golang-apm/trace"
)

// Trace work unrelated to web requests (batch, message processing, etc.) as a new transaction
func traceJob(name string, fn func(ctx context.Context)) {
ctx, _ := trace.Start(context.Background(), name)
defer trace.End(ctx, nil)
fn(ctx)
}

// Trace a section of code that handles a web request
func traceSection(ctx context.Context, name string, fn func()) {
m, _ := method.Start(ctx, name)
defer method.End(m, nil)
fn()
}

Use tracing functions​

In other files, call these functions instead of importing golang-apm. Pass the request context to the ctx of traceSection (for example, r.Context(), or c.Request.Context() in Gin).

traceJob("Custom Transaction", func(ctx context.Context) {
// Business logic such as batch jobs
})

traceSection(c.Request.Context(), "section", func() {
// Section to trace in a web request
})
Caution

Do not use traceJob (trace.Start, trace.End) inside automatically traced web requests. No new transaction is created, and trace.End ends the request transaction early, so the response time is recorded shorter than it actually is.

After adding the code, still build with whatap-go-inst go build. Check that the file containing func main() is in the ✅ list of the build output. trace.Init and trace.Shutdown are added to the main function automatically, so do not write them yourself.