mirror of
https://github.com/strukturag/nextcloud-spreed-signaling
synced 2024-06-14 11:45:35 +02:00
Add metrics for current number of HTTP client connections.
This commit is contained in:
parent
3ea60cfe31
commit
62b54a85ed
|
@ -48,3 +48,4 @@ The following metrics are available:
|
||||||
| `signaling_grpc_clients` | Gauge | 1.0.0 | The current number of GRPC clients | |
|
| `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_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` |
|
||||||
|
|
|
@ -29,10 +29,18 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
RegisterHttpClientPoolStats()
|
||||||
|
}
|
||||||
|
|
||||||
type Pool struct {
|
type Pool struct {
|
||||||
pool chan *http.Client
|
pool chan *http.Client
|
||||||
|
|
||||||
|
currentConnections prometheus.Gauge
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Pool) get(ctx context.Context) (client *http.Client, err error) {
|
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():
|
case <-ctx.Done():
|
||||||
return nil, ctx.Err()
|
return nil, ctx.Err()
|
||||||
case client := <-p.pool:
|
case client := <-p.pool:
|
||||||
|
p.currentConnections.Inc()
|
||||||
return client, nil
|
return client, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Pool) Put(c *http.Client) {
|
func (p *Pool) Put(c *http.Client) {
|
||||||
|
p.currentConnections.Dec()
|
||||||
p.pool <- c
|
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 {
|
if size <= 0 {
|
||||||
return nil, fmt.Errorf("can't create empty pool")
|
return nil, fmt.Errorf("can't create empty pool")
|
||||||
}
|
}
|
||||||
|
|
||||||
p := &Pool{
|
p := &Pool{
|
||||||
pool: make(chan *http.Client, size),
|
pool: make(chan *http.Client, size),
|
||||||
|
currentConnections: connectionsPerHostCurrent.WithLabelValues(host),
|
||||||
}
|
}
|
||||||
for size > 0 {
|
for size > 0 {
|
||||||
c := constructor()
|
c := constructor()
|
||||||
|
@ -103,7 +114,7 @@ func (p *HttpClientPool) getPool(url *url.URL) (*Pool, error) {
|
||||||
return pool, nil
|
return pool, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
pool, err := newPool(func() *http.Client {
|
pool, err := newPool(url.Host, func() *http.Client {
|
||||||
return &http.Client{
|
return &http.Client{
|
||||||
Transport: p.transport,
|
Transport: p.transport,
|
||||||
// Only send body in redirect if going to same scheme / host.
|
// Only send body in redirect if going to same scheme / host.
|
||||||
|
|
43
http_client_pool_stats_prometheus.go
Normal file
43
http_client_pool_stats_prometheus.go
Normal 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...)
|
||||||
|
}
|
Loading…
Reference in a new issue