Skip to content

Commit

Permalink
Merge pull request #13 from imryao/master
Browse files Browse the repository at this point in the history
Reuse grpc connections to improve performance
  • Loading branch information
wi1dcard authored Dec 23, 2021
2 parents ca76157 + ee28dfb commit 62d9e88
Show file tree
Hide file tree
Showing 6 changed files with 568 additions and 82 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/dist
/dist
.idea
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ dist: bionic
language: go

go:
- "1.15"
- "1.17"
services:
- docker
addons:
Expand Down
37 changes: 20 additions & 17 deletions exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"sync"
"time"

"v2ray.com/core/app/stats/command"
"github.com/v2fly/v2ray-core/v4/app/stats/command"

"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
Expand All @@ -21,9 +21,10 @@ type Exporter struct {
registry *prometheus.Registry
totalScrapes prometheus.Counter
metricDescriptions map[string]*prometheus.Desc
conn *grpc.ClientConn
}

func NewExporter(endpoint string, scrapeTimeout time.Duration) *Exporter {
func NewExporter(endpoint string, scrapeTimeout time.Duration) (*Exporter, error) {
e := Exporter{
endpoint: endpoint,
scrapeTimeout: scrapeTimeout,
Expand All @@ -46,14 +47,25 @@ func NewExporter(endpoint string, scrapeTimeout time.Duration) *Exporter {
"scrape_duration_seconds": {txt: "Scrape duration in seconds"},
"uptime_seconds": {txt: "V2Ray uptime in seconds"},
"traffic_uplink_bytes_total": {txt: "Number of transmitted bytes", lbls: []string{"dimension", "target"}},
"traffic_downlink_bytes_total": {txt: "Number of receieved bytes", lbls: []string{"dimension", "target"}},
"traffic_downlink_bytes_total": {txt: "Number of received bytes", lbls: []string{"dimension", "target"}},
} {
e.metricDescriptions[k] = e.newMetricDescr(k, desc.txt, desc.lbls)
}

e.registry.MustRegister(&e)

return &e
ctx, cancel := context.WithTimeout(context.Background(), scrapeTimeout)
defer cancel()

conn, err := grpc.DialContext(ctx, endpoint, grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
logrus.Fatal(fmt.Errorf("failed to dial: %w, timeout: %v", err, e.scrapeTimeout))
return nil, err
}

e.conn = conn

return &e, nil
}

func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
Expand Down Expand Up @@ -84,22 +96,13 @@ func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
}

func (e *Exporter) scrapeV2Ray(ch chan<- prometheus.Metric) error {
ctx, cancel := context.WithTimeout(context.Background(), e.scrapeTimeout)
defer cancel()

conn, err := grpc.DialContext(ctx, e.endpoint, grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
return fmt.Errorf("failed to dial: %w, timeout: %v", err, e.scrapeTimeout)
}
defer conn.Close()

client := command.NewStatsServiceClient(conn)
client := command.NewStatsServiceClient(e.conn)

if err := e.scrapeV2RaySysMetrics(ctx, ch, client); err != nil {
if err := e.scrapeV2RaySysMetrics(context.Background(), ch, client); err != nil {
return err
}

if err := e.scrapeV2RayMetrics(ctx, ch, client); err != nil {
if err := e.scrapeV2RayMetrics(context.Background(), ch, client); err != nil {
return err
}

Expand Down Expand Up @@ -147,7 +150,7 @@ func (e *Exporter) scrapeV2RaySysMetrics(ctx context.Context, ch chan<- promethe
// See: https://prometheus.io/docs/instrumenting/writing_exporters/#drop-less-useful-statistics

// These metrics below are not directly exposed by Go collector.
// Therefore we only add the "memstats_" prefix without changing their original names.
// Therefore, we only add the "memstats_" prefix without changing their original names.
e.registerConstMetricGauge(ch, "memstats_num_gc", float64(resp.GetNumGC()))
e.registerConstMetricGauge(ch, "memstats_pause_total_ns", float64(resp.GetPauseTotalNs()))

Expand Down
27 changes: 20 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
module github.com/wi1dcard/v2ray-exporter

go 1.15
go 1.17

require (
github.com/jessevdk/go-flags v1.4.0
github.com/prometheus/client_golang v1.6.0
github.com/sirupsen/logrus v1.6.0
golang.org/x/text v0.3.2 // indirect
google.golang.org/grpc v1.29.1
v2ray.com/core v4.19.1+incompatible
github.com/prometheus/client_golang v1.11.0
github.com/sirupsen/logrus v1.8.1
github.com/v2fly/v2ray-core/v4 v4.44.0
google.golang.org/grpc v1.43.0
)

replace v2ray.com/core => github.com/v2ray/v2ray-core v1.24.5-0.20200610141238-f9935d0e93ea
require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/pires/go-proxyproto v0.6.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.26.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
golang.org/x/net v0.0.0-20210903162142-ad29c8ab022f // indirect
golang.org/x/sys v0.0.0-20210903071746-97244b99971b // indirect
golang.org/x/text v0.3.6 // indirect
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect
google.golang.org/protobuf v1.27.1 // indirect
)
Loading

0 comments on commit 62d9e88

Please sign in to comment.