Rework stats

This commit is contained in:
9seconds
2021-03-27 22:04:30 +03:00
parent 5eca6ecb05
commit bc2bd4510a
19 changed files with 273 additions and 232 deletions
+80 -117
View File
@@ -4,8 +4,6 @@ import (
"context"
"net"
"net/http"
"strconv"
"time"
"github.com/9seconds/mtg/v2/events"
"github.com/9seconds/mtg/v2/mtglib"
@@ -19,89 +17,61 @@ type prometheusProcessor struct {
}
func (p prometheusProcessor) EventStart(evt mtglib.EventStart) {
sInfo := &streamInfo{
createdAt: evt.CreatedAt,
clientIP: evt.RemoteIP,
}
p.streams[evt.StreamID()] = sInfo
info := acquireStreamInfo()
info.SetStartTime(evt.CreatedAt)
info.SetClientIP(evt.RemoteIP)
p.streams[evt.StreamID()] = info
p.factory.metricClientConnections.WithLabelValues(sInfo.GetClientIPType()).Inc()
p.factory.metricClientConnections.
WithLabelValues(info.V(TagIPFamily)).
Inc()
}
func (p prometheusProcessor) EventConnectedToDC(evt mtglib.EventConnectedToDC) {
sInfo, ok := p.streams[evt.StreamID()]
info, ok := p.streams[evt.StreamID()]
if !ok {
return
}
sInfo.remoteIP = evt.RemoteIP
sInfo.dc = evt.DC
info.SetTelegramIP(evt.RemoteIP)
info.SetDC(evt.DC)
p.factory.metricTelegramConnections.WithLabelValues(
sInfo.GetRemoteIPType(),
sInfo.remoteIP.String(),
strconv.Itoa(sInfo.dc)).Inc()
p.factory.metricTelegramConnections.
WithLabelValues(info.V(TagTelegramIP), info.V(TagDC)).
Inc()
}
func (p prometheusProcessor) EventTraffic(evt mtglib.EventTraffic) {
sInfo, ok := p.streams[evt.StreamID()]
func (p prometheusProcessor) EventTelegramTraffic(evt mtglib.EventTelegramTraffic) {
info, ok := p.streams[evt.StreamID()]
if !ok {
return
}
labels := []string{
sInfo.GetRemoteIPType(),
sInfo.remoteIP.String(),
strconv.Itoa(sInfo.dc),
}
if evt.IsRead {
sInfo.bytesRecvFromTelegram += evt.Traffic
labels = append(labels, TagDirectionClient)
} else {
sInfo.bytesSentToTelegram += evt.Traffic
labels = append(labels, TagDirectionTelegram)
}
p.factory.metricTraffic.WithLabelValues(labels...).Add(float64(evt.Traffic))
p.factory.metricTelegramTraffic.
WithLabelValues(info.V(TagTelegramIP), info.V(TagDC), getDirection(evt.IsRead)).
Add(float64(evt.Traffic))
}
func (p prometheusProcessor) EventFinish(evt mtglib.EventFinish) {
sInfo, ok := p.streams[evt.StreamID()]
info, ok := p.streams[evt.StreamID()]
if !ok {
return
}
defer delete(p.streams, evt.StreamID())
defer func() {
delete(p.streams, evt.StreamID())
releaseStreamInfo(info)
}()
duration := evt.CreatedAt.Sub(sInfo.createdAt)
p.factory.metricClientConnections.
WithLabelValues(info.V(TagIPFamily)).
Dec()
p.factory.metricClientConnections.WithLabelValues(sInfo.GetClientIPType()).Dec()
p.factory.metricSessionDuration.Observe(float64(duration) / float64(time.Second))
if sInfo.remoteIP == nil {
return
if info.V(TagTelegramIP) != "" {
p.factory.metricTelegramConnections.
WithLabelValues(info.V(TagTelegramIP), info.V(TagDC)).
Dec()
}
labels := []string{
sInfo.GetRemoteIPType(),
sInfo.remoteIP.String(),
strconv.Itoa(sInfo.dc),
}
p.factory.metricTelegramConnections.WithLabelValues(labels...).Dec()
labels = append(labels, TagDirectionClient)
p.factory.metricSessionTraffic.
WithLabelValues(labels...).
Observe(float64(sInfo.bytesRecvFromTelegram))
labels[3] = TagDirectionTelegram
p.factory.metricSessionTraffic.
WithLabelValues(labels...).
Observe(float64(sInfo.bytesSentToTelegram))
}
func (p prometheusProcessor) EventConcurrencyLimited(_ mtglib.EventConcurrencyLimited) {
@@ -109,11 +79,7 @@ func (p prometheusProcessor) EventConcurrencyLimited(_ mtglib.EventConcurrencyLi
}
func (p prometheusProcessor) EventIPBlocklisted(evt mtglib.EventIPBlocklisted) {
if evt.RemoteIP.To4() == nil {
p.factory.metricIPBlocklisted.WithLabelValues(TagIPTypeIPv6).Inc()
} else {
p.factory.metricIPBlocklisted.WithLabelValues(TagIPTypeIPv4).Inc()
}
p.factory.metricIPBlocklisted.Inc()
}
func (p prometheusProcessor) Shutdown() {
@@ -123,13 +89,17 @@ func (p prometheusProcessor) Shutdown() {
type PrometheusFactory struct {
httpServer *http.Server
metricClientConnections *prometheus.GaugeVec
metricTelegramConnections *prometheus.GaugeVec
metricTraffic *prometheus.CounterVec
metricIPBlocklisted *prometheus.CounterVec
metricSessionTraffic *prometheus.HistogramVec
metricConcurrencyLimited prometheus.Counter
metricSessionDuration prometheus.Histogram
metricClientConnections *prometheus.GaugeVec
metricTelegramConnections *prometheus.GaugeVec
metricDomainDisguisingConnections *prometheus.GaugeVec
metricTelegramTraffic *prometheus.CounterVec
metricDomainDisguisingTraffic *prometheus.CounterVec
metricDomainDisguising prometheus.Counter
metricConcurrencyLimited prometheus.Counter
metricIPBlocklisted prometheus.Counter
metricReplayAttacks prometheus.Counter
}
func (p *PrometheusFactory) Make() events.Observer {
@@ -164,70 +134,63 @@ func NewPrometheus(metricPrefix, httpPath string) *PrometheusFactory { // nolint
metricClientConnections: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: metricPrefix,
Name: MetricClientConnections,
Help: "A number of connections under active processing.",
}, []string{TagIPType}),
Help: "A number of actively processing client connections.",
}, []string{TagIPFamily}),
metricTelegramConnections: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: metricPrefix,
Name: MetricTelegramConnections,
Help: "A number of connections to Telegram servers.",
}, []string{TagIPType, TagTelegramIP, TagDC}),
metricSessionDuration: prometheus.NewHistogram(prometheus.HistogramOpts{
}, []string{TagTelegramIP, TagDC}),
metricDomainDisguisingConnections: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: metricPrefix,
Name: MetricSessionDuration,
Help: "Session duration.",
Buckets: []float64{ // per 30 seconds
30,
60,
90,
120,
150,
180,
210,
240,
270,
300,
},
Name: MetricDomainDisguisingConnections,
Help: "A number of connections which talk with disguising domain.",
}, []string{TagIPFamily}),
metricTelegramTraffic: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: metricPrefix,
Name: MetricTelegramTraffic,
Help: "Traffic which is generated talking with Telegram servers.",
}, []string{TagTelegramIP, TagDC, TagDirection}),
metricDomainDisguisingTraffic: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: metricPrefix,
Name: MetricDomainDisguisingTraffic,
Help: "Traffic which is generated talking with disguising domain.",
}, []string{TagDirection}),
metricDomainDisguising: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: metricPrefix,
Name: MetricDomainDisguising,
Help: "A number of routings to disguising domain.",
}),
metricSessionTraffic: prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: metricPrefix,
Name: MetricSessionTraffic,
Help: "A traffic size which flew via proxy within a single session.",
Buckets: []float64{ // per 1mb
1 * 1024 * 1024,
2 * 1024 * 1024,
3 * 1024 * 1024,
4 * 1024 * 1024,
5 * 1024 * 1024,
6 * 1024 * 1024,
7 * 1024 * 1024,
8 * 1024 * 1024,
9 * 1024 * 1024,
},
}, []string{TagIPType, TagTelegramIP, TagDC, TagDirection}),
metricTraffic: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: metricPrefix,
Name: MetricTraffic,
Help: "Traffic which is sent through this proxy.",
}, []string{TagIPType, TagTelegramIP, TagDC, TagDirection}),
metricConcurrencyLimited: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: metricPrefix,
Name: MetricConcurrencyLimited,
Help: "A number of sessions that were rejected by concurrency limiter.",
}),
metricIPBlocklisted: prometheus.NewCounterVec(prometheus.CounterOpts{
metricIPBlocklisted: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: metricPrefix,
Name: MetricIPBlocklisted,
Help: "A number of rejected sessions due to ip blocklisting",
}, []string{TagIPType}),
Help: "A number of rejected sessions due to ip blocklisting.",
}),
metricReplayAttacks: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: metricPrefix,
Name: MetricReplayAttacks,
Help: "A number of detected replay attacks.",
}),
}
registry.MustRegister(factory.metricClientConnections)
registry.MustRegister(factory.metricTelegramConnections)
registry.MustRegister(factory.metricTraffic)
registry.MustRegister(factory.metricSessionTraffic)
registry.MustRegister(factory.metricSessionDuration)
registry.MustRegister(factory.metricDomainDisguisingConnections)
registry.MustRegister(factory.metricTelegramTraffic)
registry.MustRegister(factory.metricDomainDisguisingTraffic)
registry.MustRegister(factory.metricDomainDisguising)
registry.MustRegister(factory.metricConcurrencyLimited)
registry.MustRegister(factory.metricIPBlocklisted)
registry.MustRegister(factory.metricReplayAttacks)
return factory
}