Skip to content

Instantly share code, notes, and snippets.

@linuxoid69
Last active June 29, 2023 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linuxoid69/265b1736c7b7a31995084af468851336 to your computer and use it in GitHub Desktop.
Save linuxoid69/265b1736c7b7a31995084af468851336 to your computer and use it in GitHub Desktop.
simple prometheus exporter
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