Golang: Instant live visualization of your Go application

Okky Muhamad Budiman
2 min readNov 20, 2020

Ketika kita telah selesai mendeploy suatu aplikasi backend, selain testing untuk mengetahui apakah sesuai dengan yang kita harapkan, ada atau tidaknya bug, kita juga perlu memonitoring apps kita (GC, MemStats, etc).

Monotoring tersebut bisa kita lihat pada dashboard dimana kita mendeploy suatu apps, contoh di AWS.

Ada cara simple dan praktis untuk memonitor statistic apps kita tanpa login pada vendor server kita, disini saya menggunakan arl/statsviz untuk instance live statistic.

gambaran statistic heap dan objects menggunakan statsviz.

arl/statsviz sendiri support di beberapa framework golang dan router packages seperti :

  • Echo
  • Gin-Gonic
  • Gorilla Mux

How its Work

Mengapa arl/statsviz menurut saya cukup simple ? karena :

  • Mengcompose 2 http handlers.
  • By default ada endpoint debug/statsviz . dia akan serve html/js untuk kebutuhan user interface statistic yang akan kita lihat.
  • Menggunakan websocket protokol, periodically dia memanggil runtime.ReadMemStats.
  • Stat disimpen didalam circular buffer dimana by default dia akan tracking 60 datapoint.
  • Hanya dengan membuka endpoint debug/statsviz/ kita sudah bisa melihat statistik dari apps kita.

Fitur dari arl/statsviz

Adapun fitur-fitur yang dapat kita lihat adalah :

  • Heap
  • MSpans/MCaches
  • Size Class Heat Map
  • Objects
  • Gouroutines
  • GC/GPU fraction

Let’s Start

  1. Pertama buatlah simple program API golang, disini saya menggunakan mux sebagai routes API packages.
mux := http.NewServeMux() 
mux.Handle("/", r)
http.ListenAndServe(":8080", mux)

2. Implement statsviz pada router.

go work()// Create a Gorilla router and register statsviz handlers.
r := mux.NewRouter()
r.Use(mux.CORSMethodMiddleware(r))

r.Methods(GET).Path("/debug/statsviz/ws").Name("GET /debug/statsviz/ws").HandlerFunc(statsviz.Ws)
r.Methods(GET).PathPrefix("/debug/statsviz/").Name("GET /debug/statsviz/").Handler(statsviz.Index)

3. Implement work() supaya bisa kustomisasi untuk time periodically nya.

func work() {
// Generate some allocations
m := map[string][]byte{}

for {
b := make([]byte, 512+rand.Intn(16*1024))
m[strconv.Itoa(len(m)%(10*100))] = b

if len(m)%(10*100) == 0 {
m = make(map[string][]byte)
}

time.Sleep(10 * time.Millisecond)
}
}

Full dari file main.go

package main

import (
"net/http"

"github.com/gorilla/mux"

"github.com/arl/statsviz"
example "github.com/arl/statsviz/_example"
)

func main() {
// Force the GC to work to make the plots "move".
go
example.Work()

// Create a Gorilla router and register statsviz handlers.
r := mux.NewRouter()
r.Methods("GET").Path("/debug/statsviz/ws").Name("GET /debug/statsviz/ws").HandlerFunc(statsviz.Ws)
r.Methods("GET").PathPrefix("/debug/statsviz/").Name("GET /debug/statsviz/").Handler(statsviz.Index)

mux := http.NewServeMux()
mux.Handle("/", r)
http.ListenAndServe(":8080", mux)
}

5. Run aplikasi anda dan buka localhost:port/debug/statsviz/ pada browser maka statistik dari apps kita harusnya sudah bisa dilihat.

Simple dan praktis bukan ? silahkan dicoba !

References :

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Okky Muhamad Budiman
Okky Muhamad Budiman

Written by Okky Muhamad Budiman

Tech Enthusiast, Punk Rock Software Engineer, Hustler Harder

No responses yet

Write a response