Add iplist_size metric

This commit is contained in:
9seconds
2022-03-11 16:20:11 +03:00
parent 4687a7c899
commit 30170b9413
19 changed files with 206 additions and 26 deletions
+16
View File
@@ -89,6 +89,13 @@ const (
// Type: counter
MetricReplayAttacks = "replay_attacks"
// MetricIPListSize defines a metric for the size of the the ip list.
//
// Type: gauge
// Tags:
// ip_list | 'allowlist' or 'blocklist'
MetricIPListSize = "iplist_size"
// TagIPFamily defines a name of the 'ip_family' tag and all values.
TagIPFamily = "ip_family"
@@ -114,4 +121,13 @@ const (
// TagDirectionFromClient defines that traffic is sent from a client to
// Telegram.
TagDirectionFromClient = "from_client"
// TagIPList defines a name of the 'ip_list' and all values.
TagIPList = "ip_list"
// TagIPListAllow defines a value of 'ip_list' of allowlist.
TagIPListAllow = "allowlist"
// TagIPListBlock defines a value of 'ip_list' of blocklist.
TagIPListBlock = "blocklist"
)
+16
View File
@@ -118,6 +118,15 @@ func (p prometheusProcessor) EventReplayAttack(_ mtglib.EventReplayAttack) {
p.factory.metricReplayAttacks.Inc()
}
func (p prometheusProcessor) EventIPListSize(evt mtglib.EventIPListSize) {
tag := TagIPListBlock
if !evt.IsBlockList {
tag = TagIPListAllow
}
p.factory.metricIPListSize.WithLabelValues(tag).Set(float64(evt.Size))
}
func (p prometheusProcessor) Shutdown() {
for k, v := range p.streams {
releaseStreamInfo(v)
@@ -137,6 +146,7 @@ type PrometheusFactory struct {
metricClientConnections *prometheus.GaugeVec
metricTelegramConnections *prometheus.GaugeVec
metricDomainFrontingConnections *prometheus.GaugeVec
metricIPListSize *prometheus.GaugeVec
metricTelegramTraffic *prometheus.CounterVec
metricDomainFrontingTraffic *prometheus.CounterVec
@@ -197,6 +207,11 @@ func NewPrometheus(metricPrefix, httpPath string) *PrometheusFactory { // nolint
Name: MetricDomainFrontingConnections,
Help: "A number of connections which talk to front domain.",
}, []string{TagIPFamily}),
metricIPListSize: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: metricPrefix,
Name: MetricIPListSize,
Help: "A size of the ip list (blocklist or allowlist)",
}, []string{TagIPList}),
metricTelegramTraffic: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: metricPrefix,
@@ -234,6 +249,7 @@ func NewPrometheus(metricPrefix, httpPath string) *PrometheusFactory { // nolint
registry.MustRegister(factory.metricClientConnections)
registry.MustRegister(factory.metricTelegramConnections)
registry.MustRegister(factory.metricDomainFrontingConnections)
registry.MustRegister(factory.metricIPListSize)
registry.MustRegister(factory.metricTelegramTraffic)
registry.MustRegister(factory.metricDomainFrontingTraffic)
+12
View File
@@ -169,6 +169,18 @@ func (suite *PrometheusTestSuite) TestEventReplayAttack() {
suite.Contains(data, `mtg_replay_attacks 1`)
}
func (suite *PrometheusTestSuite) TestEventIPListSize() {
suite.prometheus.EventIPListSize(mtglib.NewEventIPListSize(10, false))
suite.prometheus.EventIPListSize(mtglib.NewEventIPListSize(3, true))
time.Sleep(100 * time.Millisecond)
data, err := suite.Get()
suite.NoError(err)
suite.Contains(data, `mtg_iplist_size{ip_list="allowlist"} 10`)
suite.Contains(data, `mtg_iplist_size{ip_list="blocklist"} 3`)
}
func TestPrometheus(t *testing.T) {
t.Parallel()
suite.Run(t, &PrometheusTestSuite{})
+9
View File
@@ -121,6 +121,15 @@ func (s statsdProcessor) EventReplayAttack(_ mtglib.EventReplayAttack) {
s.client.Incr(MetricReplayAttacks, 1)
}
func (s statsdProcessor) EventIPListSize(evt mtglib.EventIPListSize) {
tag := TagIPListBlock
if !evt.IsBlockList {
tag = TagIPListAllow
}
s.client.Gauge(MetricIPListSize, int64(evt.Size), statsd.StringTag(TagIPList, tag))
}
func (s statsdProcessor) Shutdown() {
events := make([]mtglib.EventFinish, 0, len(s.streams))
+16
View File
@@ -196,6 +196,22 @@ func (suite *StatsdTestSuite) TestEventReplayAttack() {
suite.Equal("mtg.replay_attacks:1|c", suite.statsdServer.String())
}
func (suite *StatsdTestSuite) TestEventIPListSizeAllowlist() {
suite.statsd.EventIPListSize(mtglib.NewEventIPListSize(10, false))
time.Sleep(statsdSleepTime)
suite.Contains(suite.statsdServer.String(), "mtg.iplist_size:10|g")
suite.Contains(suite.statsdServer.String(), "allowlist")
}
func (suite *StatsdTestSuite) TestEventIPListSizeBlocklist() {
suite.statsd.EventIPListSize(mtglib.NewEventIPListSize(10, true))
time.Sleep(statsdSleepTime)
suite.Contains(suite.statsdServer.String(), "mtg.iplist_size:10|g")
suite.Contains(suite.statsdServer.String(), "blocklist")
}
func TestStatsd(t *testing.T) {
t.Parallel()
suite.Run(t, &StatsdTestSuite{})