Add metrics for current number of HTTP client connections.

This commit is contained in:
Joachim Bauch 2024-02-27 09:10:32 +01:00
parent 3ea60cfe31
commit 62b54a85ed
No known key found for this signature in database
GPG key ID: 77C1D22D53E15F02
3 changed files with 58 additions and 3 deletions

View file

@ -48,3 +48,4 @@ The following metrics are available:
| `signaling_grpc_clients` | Gauge | 1.0.0 | The current number of GRPC clients | |
| `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` |

View file

@ -29,10 +29,18 @@ import (
"net/http"
"net/url"
"sync"
"github.com/prometheus/client_golang/prometheus"
)
func init() {
RegisterHttpClientPoolStats()
}
type Pool struct {
pool chan *http.Client
currentConnections prometheus.Gauge
}
func (p *Pool) get(ctx context.Context) (client *http.Client, err error) {
@ -40,21 +48,24 @@ func (p *Pool) get(ctx context.Context) (client *http.Client, err error) {
case <-ctx.Done():
return nil, ctx.Err()
case client := <-p.pool:
p.currentConnections.Inc()
return client, nil
}
}
func (p *Pool) Put(c *http.Client) {
p.currentConnections.Dec()
p.pool <- c
}
func newPool(constructor func() *http.Client, size int) (*Pool, error) {
func newPool(host string, constructor func() *http.Client, size int) (*Pool, error) {
if size <= 0 {
return nil, fmt.Errorf("can't create empty pool")
}
p := &Pool{
pool: make(chan *http.Client, size),
pool: make(chan *http.Client, size),
currentConnections: connectionsPerHostCurrent.WithLabelValues(host),
}
for size > 0 {
c := constructor()
@ -103,7 +114,7 @@ func (p *HttpClientPool) getPool(url *url.URL) (*Pool, error) {
return pool, nil
}
pool, err := newPool(func() *http.Client {
pool, err := newPool(url.Host, func() *http.Client {
return &http.Client{
Transport: p.transport,
// Only send body in redirect if going to same scheme / host.

View file

@ -0,0 +1,43 @@
/**
* 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 (
connectionsPerHostCurrent = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "signaling",
Subsystem: "http_client_pool",
Name: "connections",
Help: "The current number of HTTP client connections per host",
}, []string{"host"})
httpClientPoolStats = []prometheus.Collector{
connectionsPerHostCurrent,
}
)
func RegisterHttpClientPoolStats() {
registerAll(httpClientPoolStats...)
}