Add throttled requests to metrics.

This commit is contained in:
Joachim Bauch 2024-05-14 13:40:19 +02:00
parent 39f4b2eb11
commit e862392872
No known key found for this signature in database
GPG key ID: 77C1D22D53E15F02
3 changed files with 60 additions and 0 deletions

View file

@ -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_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_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` |

View file

@ -25,6 +25,7 @@ import (
"context"
"errors"
"log"
"strconv"
"sync"
"time"
)
@ -54,6 +55,10 @@ var (
ErrBruteforceDetected = errors.New("bruteforce detected")
)
func init() {
RegisterThrottleStats()
}
type ThrottleFunc func(ctx context.Context)
type Throttler interface {
@ -248,6 +253,7 @@ func (t *memoryThrottler) CheckBruteforce(ctx context.Context, client string, ac
delta := now.Sub(entries[l-maxBruteforceAttempts].ts)
if delta <= maxBruteforceDurationThreshold {
log.Printf("Detected bruteforce attempt on \"%s\" from %s", action, client)
statsThrottleBruteforceTotal.WithLabelValues(action).Inc()
return doThrottle, ErrBruteforceDetected
}
}
@ -271,6 +277,7 @@ func (t *memoryThrottler) throttle(ctx context.Context, client string, action st
count := t.addEntry(client, action, entry)
delay := t.getDelay(count - 1)
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)
}

View 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...)
}