File: connectioncounter.go

package info (click to toggle)
gitlab-shell 14.35.0%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,652 kB
  • sloc: ruby: 1,129; makefile: 583; sql: 391; sh: 384
file content (30 lines) | stat: -rw-r--r-- 684 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package starter

import (
	"net"

	"github.com/prometheus/client_golang/prometheus"
)

// wrap returns a listener which increments a prometheus counter on each
// accepted connection. Use cType to specify the connection type, this is
// a prometheus label.
func wrap(cType string, l net.Listener, counter *prometheus.CounterVec) net.Listener {
	return &countingListener{
		cType:    cType,
		Listener: l,
		counter:  counter,
	}
}

type countingListener struct {
	net.Listener
	cType   string
	counter *prometheus.CounterVec
}

func (cl *countingListener) Accept() (net.Conn, error) {
	conn, err := cl.Listener.Accept()
	cl.counter.WithLabelValues(cl.cType).Inc()
	return conn, err
}