Last active
June 29, 2023 22:11
-
-
Save linuxoid69/265b1736c7b7a31995084af468851336 to your computer and use it in GitHub Desktop.
simple prometheus exporter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"net/http" | |
"github.com/prometheus/client_golang/prometheus" | |
"github.com/prometheus/client_golang/prometheus/promhttp" | |
) | |
func main() { | |
// Создание нового регистратора метрик Prometheus | |
reg := prometheus.NewRegistry() | |
// Определение и регистрация метрики | |
counter := prometheus.NewCounter(prometheus.CounterOpts{ | |
Name: "my_custom_counter", | |
Help: "This is a custom counter metric.", | |
}) | |
reg.MustRegister(counter) | |
// Увеличение значения счетчика при каждом запросе | |
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | |
counter.Inc() | |
fmt.Fprint(w, "Hello, Prometheus!") | |
}) | |
// Обработка метрик Prometheus через HTTP | |
http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{})) | |
// Запуск веб-сервера на порту 8080 | |
if err := http.ListenAndServe(":8080", nil); err != nil { | |
panic(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment