mirror of
https://github.com/strukturag/nextcloud-spreed-signaling
synced 2024-06-29 02:40:03 +02:00
Add throttled requests to metrics.
This commit is contained in:
parent
39f4b2eb11
commit
e862392872
|
@ -49,3 +49,5 @@ The following metrics are available:
|
||||||
| `signaling_grpc_client_calls_total` | Counter | 1.0.0 | The total number of GRPC client calls | `method` |
|
| `signaling_grpc_client_calls_total` | Counter | 1.0.0 | The total number of GRPC client calls | `method` |
|
||||||
| `signaling_grpc_server_calls_total` | Counter | 1.0.0 | The total number of GRPC server calls | `method` |
|
| `signaling_grpc_server_calls_total` | Counter | 1.0.0 | The total number of GRPC server calls | `method` |
|
||||||
| `signaling_http_client_pool_connections` | Gauge | 1.2.4 | The current number of HTTP client connections per host | `host` |
|
| `signaling_http_client_pool_connections` | Gauge | 1.2.4 | The current number of HTTP client connections per host | `host` |
|
||||||
|
| `signaling_throttle_delayed_total` | Counter | 1.2.5 | The total number of delayed requests | `action`, `delay` |
|
||||||
|
| `signaling_throttle_bruteforce_total` | Counter | 1.2.5 | The total number of rejected bruteforce requests | `action` |
|
||||||
|
|
|
@ -25,6 +25,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"log"
|
"log"
|
||||||
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -54,6 +55,10 @@ var (
|
||||||
ErrBruteforceDetected = errors.New("bruteforce detected")
|
ErrBruteforceDetected = errors.New("bruteforce detected")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
RegisterThrottleStats()
|
||||||
|
}
|
||||||
|
|
||||||
type ThrottleFunc func(ctx context.Context)
|
type ThrottleFunc func(ctx context.Context)
|
||||||
|
|
||||||
type Throttler interface {
|
type Throttler interface {
|
||||||
|
@ -248,6 +253,7 @@ func (t *memoryThrottler) CheckBruteforce(ctx context.Context, client string, ac
|
||||||
delta := now.Sub(entries[l-maxBruteforceAttempts].ts)
|
delta := now.Sub(entries[l-maxBruteforceAttempts].ts)
|
||||||
if delta <= maxBruteforceDurationThreshold {
|
if delta <= maxBruteforceDurationThreshold {
|
||||||
log.Printf("Detected bruteforce attempt on \"%s\" from %s", action, client)
|
log.Printf("Detected bruteforce attempt on \"%s\" from %s", action, client)
|
||||||
|
statsThrottleBruteforceTotal.WithLabelValues(action).Inc()
|
||||||
return doThrottle, ErrBruteforceDetected
|
return doThrottle, ErrBruteforceDetected
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -271,6 +277,7 @@ func (t *memoryThrottler) throttle(ctx context.Context, client string, action st
|
||||||
count := t.addEntry(client, action, entry)
|
count := t.addEntry(client, action, entry)
|
||||||
delay := t.getDelay(count - 1)
|
delay := t.getDelay(count - 1)
|
||||||
log.Printf("Failed attempt on \"%s\" from %s, throttling by %s", action, client, delay)
|
log.Printf("Failed attempt on \"%s\" from %s, throttling by %s", action, client, delay)
|
||||||
|
statsThrottleDelayedTotal.WithLabelValues(action, strconv.FormatInt(delay.Milliseconds(), 10)).Inc()
|
||||||
t.doDelay(ctx, delay)
|
t.doDelay(ctx, delay)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
51
throttle_stats_prometheus.go
Normal file
51
throttle_stats_prometheus.go
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
/**
|
||||||
|
* Standalone signaling server for the Nextcloud Spreed app.
|
||||||
|
* Copyright (C) 2024 struktur AG
|
||||||
|
*
|
||||||
|
* @author Joachim Bauch <bauch@struktur.de>
|
||||||
|
*
|
||||||
|
* @license GNU AGPL version 3 or any later version
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
package signaling
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
statsThrottleDelayedTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
|
||||||
|
Namespace: "signaling",
|
||||||
|
Subsystem: "throttle",
|
||||||
|
Name: "delayed_total",
|
||||||
|
Help: "The total number of delayed requests",
|
||||||
|
}, []string{"action", "delay"})
|
||||||
|
|
||||||
|
statsThrottleBruteforceTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
|
||||||
|
Namespace: "signaling",
|
||||||
|
Subsystem: "throttle",
|
||||||
|
Name: "bruteforce_total",
|
||||||
|
Help: "The total number of rejected bruteforce requests",
|
||||||
|
}, []string{"action"})
|
||||||
|
|
||||||
|
throttleStats = []prometheus.Collector{
|
||||||
|
statsThrottleDelayedTotal,
|
||||||
|
statsThrottleBruteforceTotal,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func RegisterThrottleStats() {
|
||||||
|
registerAll(throttleStats...)
|
||||||
|
}
|
Loading…
Reference in a new issue