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 74 75 76
|
package snowflake_proxy
import (
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const (
// metricNamespace represent prometheus namespace
metricNamespace = "tor_snowflake_proxy"
)
type Metrics struct {
totalInBoundTraffic prometheus.Counter
totalOutBoundTraffic prometheus.Counter
totalConnections prometheus.Counter
}
func NewMetrics() *Metrics {
return &Metrics{
totalConnections: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: metricNamespace,
Name: "connections_total",
Help: "The total number of connections handled by the snowflake proxy",
}),
totalInBoundTraffic: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: metricNamespace,
Name: "traffic_inbound_bytes_total",
Help: "The total in bound traffic by the snowflake proxy (KB)",
}),
totalOutBoundTraffic: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: metricNamespace,
Name: "traffic_outbound_bytes_total",
Help: "The total out bound traffic by the snowflake proxy (KB)",
}),
}
}
// Start register the metrics server and serve them on the given address
func (m *Metrics) Start(addr string) error {
go func() {
http.Handle("/internal/metrics", promhttp.Handler())
if err := http.ListenAndServe(addr, nil); err != nil {
panic(err)
}
}()
return prometheus.Register(m)
}
func (m *Metrics) Collect(ch chan<- prometheus.Metric) {
m.totalConnections.Collect(ch)
m.totalInBoundTraffic.Collect(ch)
m.totalOutBoundTraffic.Collect(ch)
}
func (m *Metrics) Describe(descs chan<- *prometheus.Desc) {
prometheus.DescribeByCollect(m, descs)
}
// TrackInBoundTraffic counts the received traffic by the snowflake proxy
func (m *Metrics) TrackInBoundTraffic(value int64) {
m.totalInBoundTraffic.Add(float64(value))
}
// TrackOutBoundTraffic counts the transmitted traffic by the snowflake proxy
func (m *Metrics) TrackOutBoundTraffic(value int64) {
m.totalOutBoundTraffic.Add(float64(value))
}
// TrackNewConnection counts the new connections
func (m *Metrics) TrackNewConnection() {
m.totalConnections.Inc()
}
|