mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-14 14:11:23 -04:00
feat: add option to expose the stats by using metrics
This commit is contained in:
parent
af73ab7d1f
commit
d932cb2744
3 changed files with 124 additions and 2 deletions
76
proxy/lib/metrics.go
Normal file
76
proxy/lib/metrics.go
Normal file
|
@ -0,0 +1,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",
|
||||
}),
|
||||
totalOutBoundTraffic: prometheus.NewCounter(prometheus.CounterOpts{
|
||||
Namespace: metricNamespace,
|
||||
Name: "traffic_outbound_bytes_total",
|
||||
Help: "The total out bound traffic by the snowflake proxy ",
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
// 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()
|
||||
}
|
29
proxy/lib/pt_event_metrics.go
Normal file
29
proxy/lib/pt_event_metrics.go
Normal file
|
@ -0,0 +1,29 @@
|
|||
package snowflake_proxy
|
||||
|
||||
import (
|
||||
"gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake/v2/common/event"
|
||||
)
|
||||
|
||||
type EventCollector interface {
|
||||
TrackInBoundTraffic(value int64)
|
||||
TrackOutBoundTraffic(value int64)
|
||||
TrackNewConnection()
|
||||
}
|
||||
|
||||
type EventMetrics struct {
|
||||
collector EventCollector
|
||||
}
|
||||
|
||||
func NewEventMetrics(collector EventCollector) *EventMetrics {
|
||||
return &EventMetrics{collector: collector}
|
||||
}
|
||||
|
||||
func (em *EventMetrics) OnNewSnowflakeEvent(e event.SnowflakeEvent) {
|
||||
switch e.(type) {
|
||||
case event.EventOnProxyConnectionOver:
|
||||
e := e.(event.EventOnProxyConnectionOver)
|
||||
em.collector.TrackInBoundTraffic(e.InboundTraffic)
|
||||
em.collector.TrackOutBoundTraffic(e.OutboundTraffic)
|
||||
em.collector.TrackNewConnection()
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue