Skip to content

Commit 83e265e

Browse files
committed
Add exporting metrics
1 parent 4bc1cc4 commit 83e265e

File tree

10 files changed

+33
-117
lines changed

10 files changed

+33
-117
lines changed

cmd/tunnel/main.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,17 @@ func initServices(runtime *runtime.TunnelRuntime) error {
231231
tunnelAPI := httpapi.NewTunnelHandlers(runtime, sessionManager, adminJWT, jwtAuthorizer, dataStorage, keyStore, ipv4am, statService)
232232

233233
xHttpAddr := runtime.Settings.HTTP.ListenAddr
234-
xhttpOpts := []xhttp.Option{xhttp.WithLogger()}
235-
if runtime.Settings.HTTP.Prometheus {
236-
xhttpOpts = append([]xhttp.Option{xhttp.WithMetrics()}, xhttpOpts...)
234+
xhttpOpts := []xhttp.Option{}
235+
236+
if runtime.Settings.MetricsListenAddr != "" {
237+
xMetricsServer := xhttp.NewMetrics()
238+
if err := xMetricsServer.Run(runtime.Settings.MetricsListenAddr); err != nil {
239+
return err
240+
}
241+
runtime.Services.RegisterService("httpMetricsServer", xMetricsServer)
237242
}
243+
244+
xhttpOpts = append([]xhttp.Option{xhttp.WithLogger()}, xhttpOpts...)
238245
if runtime.Settings.HTTP.CORS {
239246
xhttpOpts = append([]xhttp.Option{xhttp.WithCORS()}, xhttpOpts...)
240247
}
@@ -314,6 +321,7 @@ func initServices(runtime *runtime.TunnelRuntime) error {
314321
}
315322

316323
// Startup HTTP API
324+
zap.L().Debug("Starting http server", zap.Any("xHttpServer", xHttpServer))
317325
if err := xHttpServer.Run(xHttpAddr); err != nil {
318326
return err
319327
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ require (
1616
github.com/stretchr/testify v1.8.4
1717
github.com/vishvananda/netlink v1.1.0
1818
github.com/vpnhouse/api v0.0.0-20251030084646-ea0cd033b859
19-
github.com/vpnhouse/common-lib-go v0.0.0-20251030084639-d20c1eb3a84d
19+
github.com/vpnhouse/common-lib-go v0.0.0-20251121083201-a01e94e1e6a7
2020
github.com/vpnhouse/iprose-go v0.2.5-0.20251030113512-912f550a8bff
2121
go.uber.org/multierr v1.10.0
2222
go.uber.org/zap v1.25.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -830,8 +830,8 @@ github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df h1:OviZH7qLw/7Zo
830830
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df/go.mod h1:JP3t17pCcGlemwknint6hfoeCVQrEMVwxRLRjXpq+BU=
831831
github.com/vpnhouse/api v0.0.0-20251030084646-ea0cd033b859 h1:854OuZ3xW9Oz82F3Hm5zX0tjKhG/FSyl7JBD1+633lU=
832832
github.com/vpnhouse/api v0.0.0-20251030084646-ea0cd033b859/go.mod h1:qeAZBOFAiz7FiTG49UremjHQExCUPui2tTdn1NMDn1s=
833-
github.com/vpnhouse/common-lib-go v0.0.0-20251030084639-d20c1eb3a84d h1:e70ApR8c2IqzvU0C60VXQU0iCEPdLliEcMHKhdxE4VY=
834-
github.com/vpnhouse/common-lib-go v0.0.0-20251030084639-d20c1eb3a84d/go.mod h1:krAKChmKHC9ECivvOmgr1wTDo7aJuwawEHIt8qM9WYQ=
833+
github.com/vpnhouse/common-lib-go v0.0.0-20251121083201-a01e94e1e6a7 h1:anrGIfIZ6VCy6Nns8KKubIzrJ8OAK8A8guV3iSLOOy4=
834+
github.com/vpnhouse/common-lib-go v0.0.0-20251121083201-a01e94e1e6a7/go.mod h1:krAKChmKHC9ECivvOmgr1wTDo7aJuwawEHIt8qM9WYQ=
835835
github.com/vpnhouse/iprose-go v0.2.5-0.20251030113512-912f550a8bff h1:ma2ntR8IT4UndEPk3oQQzA5FH7OgWILNiVTcVkmoJfY=
836836
github.com/vpnhouse/iprose-go v0.2.5-0.20251030113512-912f550a8bff/go.mod h1:JqeiA8pXPX3Aj2vRdiYTlQ0dzKDwufeFnrByqnZFCm0=
837837
github.com/vultr/govultr/v2 v2.7.1/go.mod h1:BvOhVe6/ZpjwcoL6/unkdQshmbS9VGbowI4QT+3DGVU=

internal/manager/internal.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ func (manager *Manager) restorePeers() {
5353
}
5454

5555
_ = manager.wireguard.SetPeer(peer)
56-
allPeersGauge.Inc()
5756
}
5857
}
5958

@@ -66,8 +65,6 @@ func (manager *Manager) unsetPeer(peer *types.PeerInfo) error {
6665

6766
err = manager.ip4am.Unset(*peer.Ipv4)
6867
errs = multierr.Append(errs, err)
69-
70-
allPeersGauge.Dec()
7168
return errs
7269
}
7370

@@ -132,7 +129,6 @@ func (manager *Manager) setPeer(peer *types.PeerInfo) error {
132129
return err
133130
}
134131

135-
allPeersGauge.Inc()
136132
return nil
137133
}
138134

@@ -253,19 +249,9 @@ func (manager *Manager) findPeerByIdentifiers(identifiers *types.PeerIdentifiers
253249
}
254250

255251
func (manager *Manager) sync() {
256-
manager.syncLinkStats()
257252
manager.syncPeerStats()
258253
}
259254

260-
func (manager *Manager) syncLinkStats() {
261-
linkStats, err := manager.wireguard.GetLinkStatistic()
262-
if err == nil {
263-
// non-nil error will be logged
264-
// by the common.Error inside the method.
265-
updatePrometheusFromLinkStats(linkStats)
266-
}
267-
}
268-
269255
func (manager *Manager) background() {
270256
syncPeerTicker := time.NewTicker(manager.runtime.Settings.GetUpdateStatisticsInterval().Value())
271257
zap.L().Debug("Start update peer stats", zap.Stringer("interval", manager.runtime.Settings.GetUpdateStatisticsInterval()))

internal/manager/prom.go

Lines changed: 0 additions & 84 deletions
This file was deleted.

internal/manager/stats.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,6 @@ func (manager *Manager) syncPeerStats() {
9292
zap.L().Error("failed to unset expired peer", zap.Error(err))
9393
}
9494
}
95-
96-
peersWithHandshakesGauge.Set(float64(numPeersWithHadshakes))
9795
}
9896

9997
func (manager *Manager) peerCountry(peer *types.PeerInfo, wgPeer *wgtypes.Peer) string {

internal/proxy/instance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func (instance *Instance) Shutdown() error {
144144
}
145145

146146
func (instance *Instance) Running() bool {
147-
return instance.terminated.Load()
147+
return !instance.terminated.Load()
148148
}
149149

150150
func (instance *Instance) isMyRequest(r *http.Request) bool {

internal/settings/static.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ type Config struct {
109109
IPRose iprose.Config `yaml:"iprose,omitempty"`
110110
Statistics StatisticsConfig `yaml:"statistics,omitempty"`
111111
CDN CDNConfig `yaml:"cdn,omitempty"`
112+
MetricsListenAddr string `yaml:"metrics_listen_addr"`
112113

113114
// path to the config file, or default path in case of safe defaults.
114115
// Used to override config via the admin API.
@@ -190,8 +191,6 @@ type HttpConfig struct {
190191
ListenAddr string `yaml:"listen_addr" valid:"listen_addr,required"`
191192
// CORS enables corresponding middleware for the local development
192193
CORS bool `yaml:"cors"`
193-
// Enable prometheus metrics on "/metrics" path
194-
Prometheus bool `yaml:"prometheus"`
195194
}
196195

197196
type AdminAPIConfig struct {
@@ -413,6 +412,7 @@ func safeDefaults(rootDir string) *Config {
413412
PeerStatistics: defaultPeerStatisticConfig(),
414413
IPRose: iprose.DefaultConfig,
415414
Statistics: defaultStatisticsConfig(),
415+
MetricsListenAddr: "127.0.0.1:9100",
416416
}
417417
}
418418

internal/stats/internal.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,20 @@ func (s *Service) export() {
7070
record.total.upstream += deltaUp
7171
record.total.downstream += deltaDown
7272

73+
extra := record.extraCb()
7374
record.export = Stats{
74-
ExtraStats: record.extraCb(),
75+
ExtraStats: extra,
7576
UpstreamBytes: int64(record.total.upstream),
7677
DownstreamBytes: int64(record.total.downstream),
7778
UpstreamSpeed: int64(speed(deltaUp, now, record.total.at)),
7879
DownstreamSpeed: int64(speed(deltaDown, now, record.total.at)),
7980
}
8081

82+
record.prometheus.peers.Set(float64(extra.PeersTotal))
83+
record.prometheus.active.Set(float64(extra.PeersActive))
84+
record.prometheus.upstreamBytes.Add(float64(deltaUp))
85+
record.prometheus.downstreamBytes.Add(float64(deltaDown))
86+
8187
record.total.at = now
8288
}
8389
}

internal/stats/service.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ type totalStats struct {
2323
}
2424

2525
type protoRecord struct {
26-
export Stats
27-
pending pendingStats
28-
total totalStats
29-
extraCb ExtraStatsCb
30-
service *xstats.Service
26+
export Stats
27+
pending pendingStats
28+
total totalStats
29+
extraCb ExtraStatsCb
30+
service *xstats.Service
31+
prometheus *prometheusStats
3132
}
3233

3334
type Service struct {
@@ -88,8 +89,9 @@ func (s *Service) Register(proto string, extraCb ExtraStatsCb) (*xstats.Service,
8889
}
8990

9091
record = &protoRecord{
91-
service: service,
92-
extraCb: extraCb,
92+
service: service,
93+
extraCb: extraCb,
94+
prometheus: newPrometheusStats(proto),
9395
}
9496
s.records[proto] = record
9597
s.loadMetrics(proto)

0 commit comments

Comments
 (0)