/
gf_metrics.go
73 lines (55 loc) · 1.92 KB
/
gf_metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
GloFlow application and media management/publishing platform
Copyright (C) 2021 Ivan Trajkovic
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package gf_core
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
//-------------------------------------------------
type GFmetrics struct {
// ERRORS_COUNTER - number of GF errors that were thrown
ErrorsCounter prometheus.Counter
}
//-------------------------------------------------
// INIT
func MetricsInit(p_metrics_endpoint_str string, // "/metrics"
pPortInt int) *GFmetrics {
// ERRORS_COUNTER
errorsCounter := prometheus.NewCounter(prometheus.CounterOpts{
Name: "gf_core__errors_num",
Help: "number of gf_errors thrown in the system",
})
prometheus.MustRegister(errorsCounter)
// START_SERVER
go func() {
metrics_router := mux.NewRouter()
metrics_router.Handle(p_metrics_endpoint_str, promhttp.Handler())
metricsServer := http.Server{
Handler: metrics_router,
Addr: fmt.Sprintf(":%d", pPortInt),
}
// ADD!! - check for returned error here,
// and report this in some way to the user.
metricsServer.ListenAndServe()
}()
metrics := &GFmetrics{
ErrorsCounter: errorsCounter,
}
return metrics
}