Debugging Go Microservices: Trace Goroutine Leaks with APM

The airfocus team in their Hamburg office.
Learn how to locate and fix goroutine leaks in Go microservices using APM tools, compare two leading solutions, and decide which fits your stack best.

Introduction

Goroutine leaks are a common source of latency spikes and resource exhaustion in Go‑based microservices. When a request spawns a goroutine that never exits, memory usage climbs, and the service can become unresponsive. Detecting these leaks quickly is essential for maintaining SLA compliance and keeping MTTR low. This guide shows how to use Application Performance Monitoring (APM) to trace goroutine leaks within 15 minutes, compares two popular APM solutions, and helps you decide which fits your stack.


Comparative Table

Feature Lescopr APM AlternativeTool APM
Go‑native instrumentation Automatic context propagation, zero‑config for net/http and gRPC Requires manual middleware injection
Goroutine‑level trace Shows live goroutine count per span, highlights orphaned routines Provides only request‑level latency, no goroutine breakdown
Overhead < 3 % CPU on typical workloads 5‑7 % CPU, higher on burst traffic
Dashboard granularity Real‑time heatmap of goroutine growth, SLA alerts Aggregated latency charts only
GDPR‑ready consent Built‑in consent management for user‑data tracing No built‑in consent module
Pricing model Pay‑as‑you‑go, free trial available Tiered subscription, no trial

1. Setting Up Lescopr APM for Go Microservices

  1. Install the SDK
    go get github.com/lescopr/apm-go
    
  2. Initialize the tracer
    import "github.com/lescopr/apm-go"
    func main() {
        tracer := apm.NewTracer("my-service", "1.0.0")
        defer tracer.Close()
        // Attach to HTTP server
        http.HandleFunc("/", apm.WrapHandler(myHandler))
    }
    
  3. Enable goroutine tracking
    tracer.EnableGoroutineTracking(true)
    
  4. Deploy – push the updated binary; Lescopr picks up the instrumentation automatically.

Once the service is running, the Lescopr dashboard displays a Goroutine Heatmap that updates every second. Look for a rising green line that does not return to baseline after a request completes – that indicates a leak.


2. Diagnosing a Real‑World Leak

Imagine a payment microservice that calls an external fraud‑check API. A recent incident showed latency climbing from 120 ms to 2 s, while the goroutine count grew from 150 to 1 200.

  • Step 1 – Identify the spike: In the Lescopr UI, the Goroutine Heatmap highlighted the POST /process-payment endpoint.
  • Step 2 – Drill down: Clicking the endpoint opens a Span Timeline where each span shows the number of active goroutines. The span for callFraudService retained 30 extra goroutines after the request finished.
  • Step 3 – Locate the code: The span’s stack trace revealed the leak originated from a go routine that called http.NewRequest without a proper defer resp.Body.Close().
  • Step 4 – Fix: Adding defer resp.Body.Close() inside the goroutine eliminated the orphaned routines. The goroutine count returned to normal within minutes, and latency dropped back to 130 ms.

The entire investigation took under 15 minutes, demonstrating the power of APM‑driven visibility.


3. AlternativeTool APM – What It Misses

AlternativeTool offers solid request‑level tracing but lacks native goroutine insight. To detect the same leak, engineers would need to:

  • Add custom metrics reporting runtime.NumGoroutine() after each request.
  • Correlate those metrics manually with logs.
  • Potentially miss short‑lived leaks that disappear before the metric is scraped.

This indirect approach adds extra code complexity and can increase MTTR by up to 30 % because the root cause is not surfaced automatically.


Verdict & Recommendation

For Go teams that rely on lightweight microservices, visibility into goroutine lifecycles is a decisive factor. Lescopr APM provides out‑of‑the‑box goroutine tracking, minimal overhead, and a dashboard that surfaces leaks instantly. AlternativeTool may suffice for services where request‑level latency is the only concern, but it requires additional instrumentation to catch goroutine leaks.

Before choosing your tool, compare with Lescopr on concrete technical criteria — free trial available.