Go

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. Middleware Agent: See the Installation Instructions for more details.
  2. Go Version 1.17 or above: Check your Go version with go version

Install

Step 1: Install Go APM Package

Run the following command in your terminal.

Step 2: Import Tracker

Add the following lines to the entry point of your application, and as the first import.

Add the following snippet to your main function. The accessToken is your account key, which will be pre-filled if you're logged into the documentation. It may also be found on the Installation page in the platform.

If using a Golang web framework, additional code may be required, see Framework-Specific Configuration.

With Host
Serverless

Host Based Configuration

If you are deploying the host using Kubernetes or Docker you will need to set the environment variable MW_AGENT_SERVICE.

This variable can be ignored when the agent is running on the same host as the application.

Docker
Kubernetes

Add MW_AGENT_SERVICE environment variable to your application:

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

Identify the namespace where the Infra Agent is running:

Then add the following environment variable to your application deployment YAML file:

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.

gin/gonic

Middleware APM requires Gin v1.1.18 or higher.

Install the Middleware APM Gin package.

Add the Middleware APM Gin package to the existing import statement from Step 2.

Initialize the Middleware APM wherever the Gin package is initialized, typically in the main function.

gorilla/mux

Middleware APM requires MUX v1.8.0

Install the Middleware APM MUX package.

Add the Middleware APM MUX package to the existing import statement from Step 2.

Initialize the Middleware APM wherever the MUX package is initialized, typically in the main function.

go-chi

Middleware APM requires go-chi/chi v1.5.4 or higher.

Install the Middleware APM go-chi package.

Add the Middleware APM go-chi package to the existing import statement from Step 2.

Initialize the Middleware APM wherever the go-chi package is initialized, typically in the main function.

go-chi/v5

Middleware APM requires go-chi/chi/v5 v5.0.8 or higher.

Install the Middleware APM go-chi package.

Add the Middleware APM go-chi package to the existing import statement from Step 2.

Initialize the Middleware APM wherever the go-chi package is initialized, typically in the main function.

astaxie-beego

Middleware APM requires astaxie/beego v1.12.3 or higher.

Install the Middleware APM astaxie-beego package.

Add the Middleware APM astaxie-beego package to the existing import statement from Step 2.

Initialize the Middleware APM wherever the astaxie-beego package is initialized, typically in the main function.

beego/v2

Middleware APM requires beego/v2 v2.0.7 or higher.

Install the Middleware APM astaxie-beego package.

Add the Middleware APM astaxie-beego package to the existing import statement from Step 2.

Initialize the Middleware APM wherever the astaxie-beego package is initialized, typically in the main function.

database/sql

Middleware APM requires go-sql-driver/mysql v1.7.1 or higher.

Install the Middleware APM SQL package.

Add the Middleware APM SQL package to the existing import statement from Step 2.

Initialize the Middleware APM wherever the SQL package is initialized, typically in the main function.

go-pg/pg

Middleware APM requires go-pg/pg v10.11.1 or higher.

Install the Middleware APM PG package.

Add the Middleware APM PG package to the existing import statement from Step 2.

Initialize the Middleware APM wherever the PG package is initialized, typically in the main function.

jinzhu/gorm

Middleware APM requires jinzhu/gorm v1.9.16 or higher.

Install the Middleware APM SQL package.

Add the Middleware APM SQL package to the existing import statement from Step 2.

Initialize the Middleware APM inside the main function.

gorm.io/gorm

Middleware APM requires gorm.io/gorm v1.25.1 or higher.

Install the Middleware APM GORM package.

To instrument GORM, you need to install the plugin

Then use db.WithContext(ctx) to propagate the active span.

mongo

Middleware APM requires go.mongodb.org/mongo-driver v1.12.1 or higher.

Install the Middleware APM Mongo package.

Add the Middleware APM Mongo package to the existing import statement from Step 2.

Initialize the Middleware APM inside the main function.

gRPC

Middleware APM requires google.golang.org/grpc v1.53.0 or higher.

Install the Middleware APM gRPC package.

Add the Middleware APM grpc package to the existing import statement from Step 2.

Initialize the Middleware APM wherever the grpc server is initialized, typically in the main function.

Initialize the Middleware APM wherever the grpc client is initialized, typically in the main function.

net/http

Install the Middleware APM net/http package.

Add the Middleware APM http package to the existing import statement from Step 2.

The code snippet below initializes Middleware Go APM package and shows how to use it with net/http.

Sending Custom Data

Custom Logs

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

logrus

slog

zap

Record Error

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

Get Current Span

To get the current span, you’ll need to pull it out of a context.Context you have a handle on:

Custom Spans

To create a span with a tracer, you'll need a context.Context instance, usually derived from a request object. This context may already carry a parent span from an existing instrumentation.

Custom Events

Custom Attributes

Attributes are keys and values that are applied as metadata to your spans and are useful for aggregating, filtering, and grouping traces. Attributes can be added at span creation, or at any other time during the lifecycle of a span before it has completed.

Custom Resource Attributes

Resource attributes are key-value pairs that describe specific characteristics of a service or system component, like its name, version, or environment. These attributes help in tracking and analyzing the behavior of the resource in logs, metrics, or traces.

Continuous Profiling

Application Profiling is auto-configured upon completing Step 2.

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.

Telemetry Options

The Pause* options allow you to disable specific telemetry data (metrics, traces, logs) and profiling. Use these to reduce overhead by pausing unnecessary data collection.

PauseMetrics

If you want to disable metrics, you can use PauseMetrics, or you can set the environment variable MW_APM_COLLECT_METRICS to false.

PauseDefaultMetrics

If you want to disable only default runtime metrics.

PauseTraces

If you want to disable traces, you can use PauseTraces, or you can set the environment variable MW_APM_COLLECT_TRACES to false.

PauseLogs

If you want to disable logs, you can use PauseLogs, or you can set the environment variable MW_APM_COLLECT_LOGS to false.

PauseProfiling

If you want to disable Profiling, you can use PauseProfiling, or you can set the environment variable MW_APM_COLLECT_PROFILING to false.

Debug Mode

The debug option enables detailed logging of metrics, traces, and logs by printing structured telemetry data directly to the standard output. This mode is ideal for local development and troubleshooting, providing real-time insights into the application's execution and performance.

Debug Log File

When DebugLogFile is enabled, structured telemetry data (metrics, traces, logs) is saved to files instead of being printed to the standard output. This allows for easier storage, analysis, and sharing of debug information without cluttering the console during development.

Need assistance or want to learn more about Middleware? Contact our support team in Slack.