Ops AI Golang APM

Instrument your Go service with the Middleware Golang SDK (≥ 1.7.0) and (if you use Gin) the Gin middleware (≥ 1.0.0) so Ops AI can analyze runtime issues with full code context.

What you’ll Set Up#

  • SDK install (Go modules)
  • Tracker initialization at app start (top of main)
  • (If using Gin) Add the Gin middleware to trace requests
  • Exception code capture (function names/bodies, stacks, file ranges)
  • VCS metadata (auto or env vars) so Ops AI can map incidents to the right commit and suggest fixes/PRs

APM Setup#

1. Install the Middleware Packages (Shell)#

Install the core Go APM SDK and (optionally) the Gin integration. Using @latest satisfies the minimum versions.

go get github.com/middleware-labs/golang-apm@latest
go get github.com/middleware-labs/golang-apm-gin@latest

2. Initialize the tracker (Go)#

Add the tracker import near the top of your entry file, then initialize before wiring framework middleware. (Your access token is the Middleware account key.)

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

Initialize in main (base pattern):
config, _ := track.Track(
    track.WithConfigTag("service", "<apm-service-name>"),
    track.WithConfigTag("accessToken", "<MW_API_KEY>"),
)

3. Gin framework wiring (Go)#

If you use Gin, install its package, import it, and attach the Middleware APM Gin middleware using the config returned by track.Track(...).

import (
    "github.com/gin-gonic/gin"
    track "github.com/middleware-labs/golang-apm/tracker"
    mwgin "github.com/middleware-labs/golang-apm-gin/gin"
)

func main() {
    r := gin.Default()

    config, _ := track.Track(
        track.WithConfigTag("service", "<apm-service-name>"),
        track.WithConfigTag("accessToken", "<MW_API_KEY>"),
    )

    r.Use(mwgin.Middleware(config))
    // ... your routes ...
    r.Run(":8090")
}

Exception Code Capturing (what gets recorded)#

The Go SDK automatically captures:

  • Exception types & messages
  • Stack traces with line numbers
  • Function names and bodies
  • File paths and line ranges
  • Library file detection (files under /go/src, /go/pkg/mod, vendor, third_party)

This is what enables Ops AI to show precise code context alongside an incident. No extra config required.

VCS Metadata#

If you need to override the automatic detection or if your project is not a git repository, you can set these environment variables:

MW_VCS_COMMIT_SHA=$(git rev-parse HEAD)
MW_VCS_REPOSITORY_URL=$(git config --get remote.origin.url)

This lets Middleware know exactly which version of your code is running, so it can suggest the correct fix and even create a solution PR for you.

Need assistance or want to learn more about Middleware? Contact our support team at [email protected] or join our Slack channel.