-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexporter.go
172 lines (154 loc) · 5.76 KB
/
exporter.go
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"errors"
"strings"
"github.com/prometheus/client_golang/prometheus"
v2stats "github.com/v2fly/v2ray-core/v4/app/stats/command"
)
const (
namespace = "v2ray"
)
type Exporter struct {
// v2c V2Ray Client
v2c *Client
inboundTagTrafficUplink *prometheus.Desc
inboundTagTrafficDownlink *prometheus.Desc
userTrafficUplink *prometheus.Desc
userTrafficDownlink *prometheus.Desc
numGoroutine *prometheus.Desc
numGC *prometheus.Desc
alloc *prometheus.Desc
totalAlloc *prometheus.Desc
sys *prometheus.Desc
mallocs *prometheus.Desc
frees *prometheus.Desc
liveObjects *prometheus.Desc
pauseTotalNs *prometheus.Desc
uptime *prometheus.Desc
}
type SingleF64Stat struct {
Name string
Value float64
Type prometheus.ValueType
Key string
Tag string
}
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
ch <- e.inboundTagTrafficUplink
ch <- e.inboundTagTrafficDownlink
ch <- e.userTrafficUplink
ch <- e.userTrafficDownlink
}
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
usageStats, err := e.v2c.Stats("")
if err != nil {
logger.Warnln("get usage stats from v2ray", "error", err)
} else {
if err := e.parseUsageStats(ch, usageStats); err != nil {
logger.Warnln("parse usage stats from v2ray", "error", err)
}
}
sysStats, err := e.v2c.SysStats()
if err != nil {
logger.Warnln("get sys sysStats from v2ray", "error", err)
return
}
logger.Debugln("collected sys stats", "data", sysStats)
ch <- prometheus.MustNewConstMetric(e.numGoroutine, prometheus.GaugeValue, float64(sysStats.NumGoroutine))
ch <- prometheus.MustNewConstMetric(e.numGC, prometheus.CounterValue, float64(sysStats.NumGC))
ch <- prometheus.MustNewConstMetric(e.alloc, prometheus.GaugeValue, float64(sysStats.Alloc))
ch <- prometheus.MustNewConstMetric(e.totalAlloc, prometheus.GaugeValue, float64(sysStats.TotalAlloc))
ch <- prometheus.MustNewConstMetric(e.sys, prometheus.GaugeValue, float64(sysStats.Sys))
ch <- prometheus.MustNewConstMetric(e.mallocs, prometheus.GaugeValue, float64(sysStats.Mallocs))
ch <- prometheus.MustNewConstMetric(e.frees, prometheus.GaugeValue, float64(sysStats.Frees))
ch <- prometheus.MustNewConstMetric(e.liveObjects, prometheus.GaugeValue, float64(sysStats.LiveObjects))
ch <- prometheus.MustNewConstMetric(e.pauseTotalNs, prometheus.CounterValue, float64(sysStats.PauseTotalNs))
ch <- prometheus.MustNewConstMetric(e.uptime, prometheus.CounterValue, float64(sysStats.Uptime))
}
func (e *Exporter) parseStatsItem(s *v2stats.Stat) (*SingleF64Stat, error) {
// "user>>>[email protected]>>>traffic>>>downlink"
// "inbound>>>api>>>traffic>>>uplink"
// "inbound>>>vmess-ws-in>>>traffic>>>downlink"
d := strings.Split(s.Name, ">>>")
if len(d) != 4 {
return nil, errors.New("invalid stats [length]")
}
tag := d[1]
link := d[3]
if d[0] == "inbound" {
return &SingleF64Stat{
Name: "inbound_tag_traffic_" + link,
Value: float64(s.Value),
Type: prometheus.CounterValue,
Key: "tag",
Tag: tag,
}, nil
}
if d[0] == "user" {
return &SingleF64Stat{
Name: "user_traffic_" + link,
Value: float64(s.Value),
Type: prometheus.CounterValue,
Key: "user",
Tag: tag,
}, nil
}
return nil, errors.New("invalid stats [type]")
}
func (e *Exporter) parseUsageStats(ch chan<- prometheus.Metric, stats []*v2stats.Stat) error {
itemsMetrics := map[string]*prometheus.Desc{
"inbound_tag_traffic_uplink": e.inboundTagTrafficUplink,
"inbound_tag_traffic_downlink": e.inboundTagTrafficDownlink,
"user_traffic_uplink": e.userTrafficUplink,
"user_traffic_downlink": e.userTrafficDownlink,
}
for m, d := range itemsMetrics {
for _, t := range stats {
single, err := e.parseStatsItem(t)
if err != nil {
logger.Warnln("parse usage stats", "error", err)
break
}
if single.Name == m {
ch <- prometheus.MustNewConstMetric(d, single.Type, single.Value, single.Tag)
logger.Debugln("collected usage stats", "data", single)
}
}
}
return nil
}
func NewExporter(c *Client) *Exporter {
return &Exporter{
v2c: c,
inboundTagTrafficUplink: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "inbound_tag_traffic_uplink"),
"System inbound uplink traffic group by tag.",
[]string{"tag"}, nil,
),
inboundTagTrafficDownlink: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "inbound_tag_traffic_downlink"),
"System inbound downlink traffic group by tag.",
[]string{"tag"}, nil,
),
userTrafficUplink: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "user_traffic_uplink"),
"User uplink traffic.",
[]string{"user"}, nil,
),
userTrafficDownlink: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "user_traffic_downlink"),
"User downlink.",
[]string{"user"}, nil,
),
numGoroutine: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "num_goroutine"), "", nil, nil),
numGC: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "num_gc"), "", nil, nil),
alloc: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "alloc"), "", nil, nil),
totalAlloc: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "total_alloc"), "", nil, nil),
sys: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "sys"), "", nil, nil),
mallocs: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "mallocs"), "", nil, nil),
frees: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "frees"), "", nil, nil),
liveObjects: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "live_objects"), "", nil, nil),
pauseTotalNs: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "pause_total_ns"), "", nil, nil),
uptime: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "uptime"), "", nil, nil),
}
}