Prerequisites

  • To monitor APM data on dashboard, Middleware Host agent needs to be installed.
  • You can refer this demo project to refer use cases of APM.

Step 1 : Install Golang package

Run this in your terminal

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

Step 2 : Import Tracker

Add these line at the very start of your project

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

Collect Golang specific metrics

Call track method in your main function

go track.Track(
    track.WithConfigTag("service", {APM-SERVICE-NAME}),
    track.WithConfigTag("projectName", {APM-PROJECT-NAME}),
    track.WithConfigTag("accessToken", "{ACCOUNT_KEY}"),
)

Running this method with go routine is important ! This will start collecting the application traces & profiling data.

Add custom logs

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

....

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

Distributed Tracing

You may need to add a framework specific middleware, to watch traces.

FrameworkReference
gin/gonicGIN Demo
gorilla/muxMUX Demo
go-chigo-chi Demo
go-chi/v5go-chi/v5 Demo
astaxie-beegoastaxie-beego Demo
beego/v2beego/v2 Demo
database/sqlSQL Demo
go-pg/pgPG Demo
GORM 1GORM 1 Demo
GORM 2GORM 2 Demo
go-pg/mongoMongo Demo
gRPCgRPC Demo
go-redis/redis/v9go-redis/redis/v9 Demo
go-redis/redis/v8go-redis/redis/v8 Demo

Note for APM inside Kubernetes

If you are using APM in a Kubernetes cluster make sure to follow these 2 steps:

Step 1 : Find your Middleware Service namespace

For older setup, your “mw-service” can be inside mw-agent-ns-{FIRST-5-LETTERS-OF-API-KEY} namespace For newer setup, we simplified the namespace name to mw-agent-ns

Step 2 : Set this ENV variable in your application deployment YAML

MW_AGENT_SERVICE=mw-service.NAMESPACE.svc.cluster.local
Please replace “NAMESPACE” with the correct value that you found from Step 1.

Error Handling :

If you want to record exception in traces then you can use track.RecordError(ctx,error) method.


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