mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-09-01 16:01:55 +03:00
Add EventIPBlocklisted
This commit is contained in:
+2
-1
@@ -4,9 +4,10 @@ const (
|
||||
MetricActiveConnection = "active_connections"
|
||||
MetricSessionDuration = "session_duration"
|
||||
MetricConcurrencyLimited = "concurrency_limited"
|
||||
MetricIPBlocklisted = "ip_blocklisted"
|
||||
|
||||
TagIPType = "ip_type"
|
||||
|
||||
TagIPTypeIPv4 = "ipv4"
|
||||
TagIPTypeIPv6 = "ipv4"
|
||||
TagIPTypeIPv6 = "ipv6"
|
||||
)
|
||||
|
||||
+16
-1
@@ -41,10 +41,18 @@ func (p prometheusProcessor) EventFinish(evt mtglib.EventFinish) {
|
||||
p.factory.metricSessionDuration.Observe(float64(duration) / float64(time.Second))
|
||||
}
|
||||
|
||||
func (p prometheusProcessor) EventConcurrencyLimited(evt mtglib.EventConcurrencyLimited) {
|
||||
func (p prometheusProcessor) EventConcurrencyLimited(_ mtglib.EventConcurrencyLimited) {
|
||||
p.factory.metricConcurrencyLimited.Inc()
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
||||
func (p prometheusProcessor) Shutdown() {
|
||||
p.streams = make(map[string]*streamInfo)
|
||||
}
|
||||
@@ -53,6 +61,7 @@ type PrometheusFactory struct {
|
||||
httpServer *http.Server
|
||||
|
||||
metricActiveConnections *prometheus.GaugeVec
|
||||
metricIPBlocklisted *prometheus.CounterVec
|
||||
metricConcurrencyLimited prometheus.Counter
|
||||
metricSessionDuration prometheus.Histogram
|
||||
}
|
||||
@@ -113,11 +122,17 @@ func NewPrometheus(metricPrefix, httpPath string) *PrometheusFactory {
|
||||
Name: MetricConcurrencyLimited,
|
||||
Help: "A number of sessions that were rejected by concurrency limiter.",
|
||||
}),
|
||||
metricIPBlocklisted: prometheus.NewCounterVec(prometheus.CounterOpts{
|
||||
Namespace: metricPrefix,
|
||||
Name: MetricIPBlocklisted,
|
||||
Help: "A number of rejected sessions due to ip blocklisting",
|
||||
}, []string{TagIPType}),
|
||||
}
|
||||
|
||||
registry.MustRegister(factory.metricActiveConnections)
|
||||
registry.MustRegister(factory.metricSessionDuration)
|
||||
registry.MustRegister(factory.metricConcurrencyLimited)
|
||||
registry.MustRegister(factory.metricIPBlocklisted)
|
||||
|
||||
return factory
|
||||
}
|
||||
|
||||
@@ -91,6 +91,19 @@ func (suite *PrometheusTestSuite) TestEventConcurrencyLimited() {
|
||||
suite.Contains(data, `mtg_concurrency_limited 1`)
|
||||
}
|
||||
|
||||
func (suite *PrometheusTestSuite) TestEventIPBlocklisted() {
|
||||
suite.prometheus.EventIPBlocklisted(mtglib.EventIPBlocklisted{
|
||||
CreatedAt: time.Now(),
|
||||
RemoteIP: net.ParseIP("2001:db8::68"),
|
||||
})
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
data, err := suite.Get()
|
||||
suite.NoError(err)
|
||||
suite.Contains(data, `mtg_ip_blocklisted{ip_type="ipv6"} 1`)
|
||||
}
|
||||
|
||||
func TestPrometheus(t *testing.T) {
|
||||
t.Parallel()
|
||||
suite.Run(t, &PrometheusTestSuite{})
|
||||
|
||||
@@ -49,6 +49,18 @@ func (s statsdProcessor) EventConcurrencyLimited(_ mtglib.EventConcurrencyLimite
|
||||
s.client.Incr(MetricConcurrencyLimited, 1)
|
||||
}
|
||||
|
||||
func (s statsdProcessor) EventIPBlocklisted(evt mtglib.EventIPBlocklisted) {
|
||||
var tag statsd.Tag
|
||||
|
||||
if evt.RemoteIP.To4() == nil {
|
||||
tag = statsd.StringTag(TagIPType, TagIPTypeIPv6)
|
||||
} else {
|
||||
tag = statsd.StringTag(TagIPType, TagIPTypeIPv4)
|
||||
}
|
||||
|
||||
s.client.Incr(MetricIPBlocklisted, 1, tag)
|
||||
}
|
||||
|
||||
func (s statsdProcessor) Shutdown() {
|
||||
now := time.Now()
|
||||
events := make([]mtglib.EventFinish, 0, len(s.streams))
|
||||
|
||||
@@ -121,6 +121,16 @@ func (suite *StatsdTestSuite) TestEventConcurrencyLimited() {
|
||||
suite.Equal("mtg.concurrency_limited:1|c", suite.statsdServer.String())
|
||||
}
|
||||
|
||||
func (suite *StatsdTestSuite) TestEventIPBlocklisted() {
|
||||
suite.statsd.EventIPBlocklisted(mtglib.EventIPBlocklisted{
|
||||
CreatedAt: time.Now(),
|
||||
RemoteIP: net.ParseIP("10.0.0.10"),
|
||||
})
|
||||
|
||||
time.Sleep(2 * statsd.DefaultFlushInterval)
|
||||
suite.Equal("mtg.ip_blocklisted:1|c|#ip_type:ipv4", suite.statsdServer.String())
|
||||
}
|
||||
|
||||
func TestStatsd(t *testing.T) {
|
||||
t.Parallel()
|
||||
suite.Run(t, &StatsdTestSuite{})
|
||||
|
||||
Reference in New Issue
Block a user