본문 바로가기
how to monitor software and infra/Application Monitoring

Prometheus Golang SDK 예제로 만들어보기

by 단북 2020. 8. 2.

Prometheus를 최근에 많이 사용하면서, Custom Exporter를 만들어 Custom Metric을 가져오고 싶어서 Client Library 부분을 참고해서 Custom Exporter를 만들어 보았다.

생각보다 쉬웠고, APP 내부 메트릭을 모니터링하는게 너무 신기했다. 10년전에는 끽해야 webalizer 만 사용해서 분석했던걸 이렇게 다양하게 분석할 수 있는 시대가 오다니.. 프로그래밍만 좀 하면 좋은 세상인것 같다.

## prometheus golang package 설치하기

alinos@DESKTOP-A52MBHK:~/go/src/github.com/alinos/alinosprom$ go get github.com/prometheus/client_golang/prometheus
alinos@DESKTOP-A52MBHK:~/go/src/github.com/alinos/alinosprom$ go get github.com/prometheus/client_golang/prometheus/promauto
alinos@DESKTOP-A52MBHK:~/go/src/github.com/alinos/alinosprom$ go get github.com/prometheus/client_golang/prometheus/promhttp
alinos@DESKTOP-A52MBHK:~/go/src/github.com/alinos/alinosprom$ touch main.go
alinos@DESKTOP-A52MBHK:~/go/src/github.com/alinos/alinosprom$ vi main.go
alinos@DESKTOP-A52MBHK:~/go/src/github.com/alinos/alinosprom$ go run main.go

 

## Prometheus 예제 코드 복붙(github.com/prometheus/client_golang/prometheus/promauto/auto.go)

- / 호출 시에 http request code 수를 집계하여 promhttp handler에 hello_requests_total 이름으로 카운팅 해서 보여주는 코드다.

package main

import (
        "fmt"
        "net/http"

        "github.com/prometheus/client_golang/prometheus"
        "github.com/prometheus/client_golang/prometheus/promauto"
        "github.com/prometheus/client_golang/prometheus/promhttp"
)

func main() {
        http.Handle("/", promhttp.InstrumentHandlerCounter(
                promauto.NewCounterVec(
                        prometheus.CounterOpts{
                                Name: "hello_requests_total",
                                Help: "Total number of hello-world requests by HTTP code.",
                        },
                        []string{"code"},
                ),
                http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
                        fmt.Fprint(w, "Hello, world!\nalinos promtest\n")
                }),
        ))
        http.Handle("/metrics", promhttp.Handler())
        http.ListenAndServe(":1985", nil)
}

## 예제 코드 테스트

- curl 로 한번 호출 후 metrics를 확인해보면 "hello_requests_total {code="200"} 1"  200 코드가 1번 호출됐다는 게 카운팅 되었다.
- 정확히 카운팅이 되는지 확인하기 위해 추가로 10번을 더 호출 후 확인해보면 hello_requests_total{code="200"} 11 정확히 10번이 더 추가돼서 카운팅 된 걸 확인할 수 있다.

alinos@DESKTOP-A52MBHK:~/go/src/github.com/alinos/alinosprom$ ls
main.go
alinos@DESKTOP-A52MBHK:~/go/src/github.com/alinos/alinosprom$ go run main.go
alinos@DESKTOP-A52MBHK:~$ curl -s http://localhost:1985
Hello, world!
alinos promtest
alinos@DESKTOP-A52MBHK:~$ curl -s http://localhost:1985/metrics | grep hello
# HELP hello_requests_total Total number of hello-world requests by HTTP code.
# TYPE hello_requests_total counter
hello_requests_total{code="200"} 1
alinos@DESKTOP-A52MBHK:~$ for i in `seq 1 10`; do curl -s http://localhost:1985; done
Hello, world!
alinos promtest
Hello, world!
alinos promtest
Hello, world!
alinos promtest
Hello, world!
alinos promtest
Hello, world!
alinos promtest
Hello, world!
alinos promtest
Hello, world!
alinos promtest
Hello, world!
alinos promtest
Hello, world!
alinos promtest
Hello, world!
alinos promtest
alinos@DESKTOP-A52MBHK:~$ curl -s http://localhost:1985/metrics | grep hello
# HELP hello_requests_total Total number of hello-world requests by HTTP code.
# TYPE hello_requests_total counter
hello_requests_total{code="200"} 11

 

참조

https://prometheus.io/docs/instrumenting/clientlibs/

 

Client libraries | Prometheus

Before you can monitor your services, you need to add instrumentation to their code via one of the Prometheus client libraries. These implement the Prometheus metric types. Choose a Prometheus client library that matches the language in which your applicat

prometheus.io

https://github.com/prometheus/client_golang

 

prometheus/client_golang

Prometheus instrumentation library for Go applications - prometheus/client_golang

github.com