90 lines
3.1 KiB
Go
90 lines
3.1 KiB
Go
package metrics
|
||
|
||
import (
|
||
"net/http"
|
||
"runtime"
|
||
|
||
"github.com/prometheus/client_golang/prometheus"
|
||
"github.com/prometheus/client_golang/prometheus/collectors"
|
||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||
)
|
||
|
||
//// Пользовательские метрики
|
||
//var (
|
||
// rtspConnectSuccess = prometheus.NewCounter(prometheus.CounterOpts{
|
||
// Name: "rtsp_connect_success_total",
|
||
// Help: "Количество успешных подключений к RTSP источнику.",
|
||
// })
|
||
// rtspConnectErrors = prometheus.NewCounter(prometheus.CounterOpts{
|
||
// Name: "rtsp_connect_errors_total",
|
||
// Help: "Количество ошибок подключения к RTSP источнику.",
|
||
// })
|
||
// rtspBytesRead = prometheus.NewCounter(prometheus.CounterOpts{
|
||
// Name: "rtsp_bytes_read_total",
|
||
// Help: "Общее количество байт, прочитанных из RTSP-потоков.",
|
||
// })
|
||
// funcDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
|
||
// Name: "function_execution_seconds",
|
||
// Help: "Время выполнения функций приложения.",
|
||
// Buckets: prometheus.DefBuckets,
|
||
// }, []string{"function"})
|
||
//)
|
||
|
||
func init() {
|
||
// Регистрируем стандартные метрики Go
|
||
prometheus.MustRegister(collectors.NewGoCollector())
|
||
prometheus.MustRegister(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}))
|
||
|
||
//// Регистрируем пользовательские метрики
|
||
//prometheus.MustRegister(rtspConnectSuccess)
|
||
//prometheus.MustRegister(rtspConnectErrors)
|
||
//prometheus.MustRegister(rtspBytesRead)
|
||
//prometheus.MustRegister(funcDuration)
|
||
}
|
||
|
||
//func processStream() {
|
||
// timer := prometheus.NewTimer(funcDuration.WithLabelValues("processStream"))
|
||
// defer timer.ObserveDuration()
|
||
//
|
||
// // Пример: имитация подключения к RTSP
|
||
// if err := connectToRTSP(); err != nil {
|
||
// rtspConnectErrors.Inc()
|
||
// return
|
||
// }
|
||
// rtspConnectSuccess.Inc()
|
||
//
|
||
// // Пример: чтение данных
|
||
// n := readRTSPData()
|
||
// rtspBytesRead.Add(float64(n))
|
||
//}
|
||
|
||
//func connectToRTSP() error {
|
||
// // Логика подключения (пример)
|
||
// return nil // или вернуть ошибку
|
||
//}
|
||
|
||
//func readRTSPData() int {
|
||
// // Логика чтения данных (пример)
|
||
// // Для демонстрации можно вернуть случайное значение
|
||
// return 1024
|
||
//}
|
||
|
||
func metricsHandler(w http.ResponseWriter, r *http.Request) {
|
||
w.Header().Set("Content-Type", "text/plain")
|
||
// Дополнительно можно вывести информацию о количестве горутин
|
||
w.Write([]byte("Goroutines: "))
|
||
w.Write([]byte(string(runtime.NumGoroutine())))
|
||
}
|
||
|
||
func Metrics() {
|
||
// Эндпоинт для метрик Prometheus
|
||
http.Handle("/metrics", promhttp.Handler())
|
||
// Остальные эндпоинты вашего приложения
|
||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||
//processStream()
|
||
w.Write([]byte("Processing stream..."))
|
||
})
|
||
|
||
http.ListenAndServe(":9100", nil)
|
||
}
|