Go
Go APM - Setup & Installation Docs | Middleware
Traces | Metrics | App Logs | Custom Logs | Profiling |
---|---|---|---|---|
✅ | ✅ | ✖️ | ✅ | ✅ |
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
Infra Agent
Infrastructure Agent (Infra Agent). To install the Infra Agent, see our installation guide.
Go Version
Go version 1.17
or above. Check your Go version with the following command:
Install
Step 1: Install Go
APM Package
Run the following command in your terminal.
Step 2: Import Tracker
Add the following lines to the beginning of your application code base.
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.
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.
gin/gonic
gin/gonic
Middleware APM requires Gin version 1.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
gorilla/mux
Middleware APM requires MUX version 1.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
go-chi
Middleware APM requires go-chi/chi version 1.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
go-chi/v5
Middleware APM requires go-chi/chi/v5 version 5.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
astaxie-beego
Middleware APM requires astaxie/beego version 1.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
beego/v2
Middleware APM requires beego/v2 version 2.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
database/sql
Middleware APM requires go-sql-driver/mysql version 1.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
go-pg/pg
Middleware APM requires go-pg/pg version 10.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.
GORM 1
GORM 1
Middleware APM requires jinzhu/gorm version 1.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 2
GORM 2
Middleware APM requires gorm.io/gorm version 1.25.1 or higher.
Install the Middleware APM GORM package.
Add the Middleware APM GORM package to the existing import statement from Step 2.
Initialize the Middleware APM inside the main function.
Then use db.WithContext(ctx) to propagate the active span.
go-pg/mongo
go-pg/mongo
Middleware APM requires go.mongodb.org/mongo-driver version 1.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
gRPC
Middleware APM requires google.golang.org/grpc version 1.53.0 or higher.
Install the Middleware APM gRPC 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.
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.
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:
Add the following environment variable to your application deployment YAML file:
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.
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.
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.