воскресенье, 9 августа 2020 г.

Пакет expvar в Golang

Пакет expvar предоставляет стандартизированный интерфейс для общедоступных переменных, таких как счетчики операций на серверах. Он предоставляет эти переменные через HTTP в /debug/vars в формате JSON.

Операции по установке или изменению этих общедоступных переменных являются атомарными.

В дополнение к добавлению обработчика HTTP этот пакет регистрирует следующие переменные:

cmdline   os.Args
memstats  runtime.Memstats

Иногда пакет импортируется только из-за побочного эффекта регистрации его обработчика HTTP и указанных выше переменных. Чтобы использовать его таким образом, свяжите этот пакет со своей программой:

import _ "expvar"

Примеры использования expvar

package main

import (
    _ "expvar"
    "io"
    "log"
    "net/http"
)

func main() {
    helloHandler := func(w http.ResponseWriter, req *http.Request) {
        io.WriteString(w, "Hello, world!\n")
    }

    http.HandleFunc("/hello", helloHandler)

    log.Fatal(http.ListenAndServe(":8080", nil))
}

При запуске этой программы запускается веб-обработчик и при обращении к http://localhost:8080/hello получаем вывод:

Hello, world!

При обращении к http://localhost:8080/debug/vars получаем примерно следующий вывод:

{
"cmdline": ["C:\\Users\\MyUSER\\AppData\\Local\\Temp\\___go_build_example_go.exe"],
"memstats": {"Alloc":205560,"TotalAlloc":205560,"Sys":6705288,"Lookups":0,"Mallocs":934,"Frees":18,"HeapAlloc":205560,"HeapSys":4063232,"HeapIdle":3162112,"HeapInuse":901120,"HeapReleased":3129344,"HeapObjects":916,"StackInuse":131072,"StackSys":131072,"MSpanInuse":27880,"MSpanSys":32768,"MCacheInuse":6816,"MCacheSys":16384,"BuckHashSys":3675,"GCSys":1407368,"OtherSys":1050789,"NextGC":4473924,"LastGC":0,"PauseTotalNs":0,"PauseNs":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"PauseEnd":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"NumGC":0,"NumForcedGC":0,"GCCPUFraction":0,"EnableGC":true,"DebugGC":false,"BySize":[{"Size":0,"Mallocs":0,"Frees":0},{"Size":8,"Mallocs":15,"Frees":0},{"Size":16,"Mallocs":263,"Frees":0},{"Size":32,"Mallocs":113,"Frees":0},{"Size":48,"Mallocs":275,"Frees":0},{"Size":64,"Mallocs":43,"Frees":0},{"Size":80,"Mallocs":17,"Frees":0},{"Size":96,"Mallocs":12,"Frees":0},{"Size":112,"Mallocs":4,"Frees":0},{"Size":128,"Mallocs":6,"Frees":0},{"Size":144,"Mallocs":1,"Frees":0},{"Size":160,"Mallocs":20,"Frees":0},{"Size":176,"Mallocs":4,"Frees":0},{"Size":192,"Mallocs":0,"Frees":0},{"Size":208,"Mallocs":28,"Frees":0},{"Size":224,"Mallocs":3,"Frees":0},{"Size":240,"Mallocs":4,"Frees":0},{"Size":256,"Mallocs":8,"Frees":0},{"Size":288,"Mallocs":5,"Frees":0},{"Size":320,"Mallocs":3,"Frees":0},{"Size":352,"Mallocs":9,"Frees":0},{"Size":384,"Mallocs":13,"Frees":0},{"Size":416,"Mallocs":6,"Frees":0},{"Size":448,"Mallocs":0,"Frees":0},{"Size":480,"Mallocs":1,"Frees":0},{"Size":512,"Mallocs":6,"Frees":0},{"Size":576,"Mallocs":3,"Frees":0},{"Size":640,"Mallocs":9,"Frees":0},{"Size":704,"Mallocs":2,"Frees":0},{"Size":768,"Mallocs":1,"Frees":0},{"Size":896,"Mallocs":11,"Frees":0},{"Size":1024,"Mallocs":2,"Frees":0},{"Size":1152,"Mallocs":3,"Frees":0},{"Size":1280,"Mallocs":2,"Frees":0},{"Size":1408,"Mallocs":1,"Frees":0},{"Size":1536,"Mallocs":0,"Frees":0},{"Size":1792,"Mallocs":4,"Frees":0},{"Size":2048,"Mallocs":1,"Frees":0},{"Size":2304,"Mallocs":2,"Frees":0},{"Size":2688,"Mallocs":2,"Frees":0},{"Size":3072,"Mallocs":0,"Frees":0},{"Size":3200,"Mallocs":0,"Frees":0},{"Size":3456,"Mallocs":0,"Frees":0},{"Size":4096,"Mallocs":6,"Frees":0},{"Size":4864,"Mallocs":0,"Frees":0},{"Size":5376,"Mallocs":1,"Frees":0},{"Size":6144,"Mallocs":1,"Frees":0},{"Size":6528,"Mallocs":0,"Frees":0},{"Size":6784,"Mallocs":0,"Frees":0},{"Size":6912,"Mallocs":0,"Frees":0},{"Size":8192,"Mallocs":1,"Frees":0},{"Size":9472,"Mallocs":0,"Frees":0},{"Size":9728,"Mallocs":0,"Frees":0},{"Size":10240,"Mallocs":4,"Frees":0},{"Size":10880,"Mallocs":0,"Frees":0},{"Size":12288,"Mallocs":0,"Frees":0},{"Size":13568,"Mallocs":0,"Frees":0},{"Size":14336,"Mallocs":1,"Frees":0},{"Size":16384,"Mallocs":0,"Frees":0},{"Size":18432,"Mallocs":0,"Frees":0},{"Size":19072,"Mallocs":0,"Frees":0}]}
}

Если в вашей программе присутствует ServeMux тогда вам следует зарегистрировать обработчик в нем (в данном примере также сменен путь по которому доступен вывод переменных):

package main

import (
    "expvar"
    "io"
    "log"
    "net/http"
)

func main() {
    mux := http.NewServeMux()

    helloHandler := func(w http.ResponseWriter, req *http.Request) {
        io.WriteString(w, "Hello, world!\n")
    }

    mux.HandleFunc("/hello", helloHandler)
    mux.Handle("/stats", expvar.Handler())

    log.Fatal(http.ListenAndServe(":8080", mux))
}


Читайте также:


Комментариев нет:

Отправить комментарий