TracesMetricsApp LogsCustom LogsProfiling
✖️

This guide walks you through setting up Application Performance Monitoring (APM) on a Go application. These instructions can also be found on the Installation page in your Middleware Account. View example code here.

Prerequisites

1

Infra Agent

Infrastructure Agent (Infra Agent). To install the Infra Agent, see our installation guide.

2

Go Version

Go version 1.17 or above. Check your Go version with the following command:

Shell
go version

Install

Step 1: Install Go APM Package

Run the following command in your terminal.

Go
go get github.com/middleware-labs/golang-apm

Step 2: Import Tracker

Add the following lines to the beginning of your application code base.

Go
import (
    track "github.com/middleware-labs/golang-apm/tracker"
)

Add the following snippet to your main function. The access token is your account key, which can be found on the Installation page. If using a Golang web framework, additional code may be required, see Step 3.

Go
go track.Track(
    track.WithConfigTag("service", {APM-SERVICE-NAME}),
    track.WithConfigTag("accessToken", "<xxxxxxxxxx>"),
)

Step 3: Framework-Specific Configuration

If any of the following Go web frameworks are used in your application, additional configuration is required. View additional configuration steps per framework below.

Step 4: Container Variables

Applications running in a container require an additional environment variable. If your application is not running in a container, move to Step 5.

Docker

Add the following environment variable to your application.

Shell
MW_AGENT_SERVICE=DOCKER_BRIDGE_GATEWAY_ADDRESS
The DOCKER_BRIDGE_GATEWAY_ADDRESS is the IP address of the gateway between the Docker host and bridge network. This is 172.17.0.1 by default. Learn more about Docker bridge networking here

Kubernetes

Run the following command to find the mw-service in Kubernetes:

Shell
kubectl get service --all-namespaces | grep mw-service

Add the following environment variable to your application deployment YAML file:

Shell
MW_AGENT_SERVICE=mw-service.mw-agent-ns.svc.cluster.local

Step 5: Capture Application Data

Traces

Distributed tracing is automatically enabled upon completion of Step 2.

Custom Logs

To ingest custom logs into Middleware, utilize the following functions inside your logging method based on desired log severity levels.

Go
"github.com/middleware-labs/golang-apm/logger"

....

logger.Error("Error")
logger.Info("Info")
logger.Warn("Warn")

Profiling

Application Profiling is auto-configured upon completing Step 2.

Stack Traces

Use the track.RecordError(ctx,error) method to record a stack trace when an error occurs. See an example of this method below.

Go
app.get("/hello", (req, res) => {
    ctx := req.Context()
    try {
        throw ("error");
    } catch (error) {
        track.RecordError(ctx, error)
    }
})

Continuous Profiling

Continuous profiling captures real-time performance insights from your application to enable rapid identification of resource allocation, bottlenecks, and more. Navigate to the Continuous Profiling section to learn more about using Continuous Profiling with the Go APM.

Need assistance or want to learn more about Middleware? Contact us at support[at]middleware.io.