diff --git a/docs/prometheus-metrics.md b/docs/prometheus-metrics.md index 352bd65..8e54db5 100644 --- a/docs/prometheus-metrics.md +++ b/docs/prometheus-metrics.md @@ -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` | diff --git a/http_client_pool.go b/http_client_pool.go index 5de25cf..65c19dc 100644 --- a/http_client_pool.go +++ b/http_client_pool.go @@ -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. diff --git a/http_client_pool_stats_prometheus.go b/http_client_pool_stats_prometheus.go new file mode 100644 index 0000000..72d7b2c --- /dev/null +++ b/http_client_pool_stats_prometheus.go @@ -0,0 +1,43 @@ +/** + * Standalone signaling server for the Nextcloud Spreed app. + * Copyright (C) 2024 struktur AG + * + * @author Joachim Bauch + * + * @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 . + */ +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...) +}