Skip to content

Commit

Permalink
Add option --scrape-timeout.
Browse files Browse the repository at this point in the history
  • Loading branch information
wi1dcard committed Feb 6, 2020
1 parent bee40ba commit 1462d76
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
12 changes: 7 additions & 5 deletions exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ import (
type Exporter struct {
sync.Mutex
endpoint string
scrapeTimeout time.Duration
registry *prometheus.Registry
totalScrapes prometheus.Counter
metricDescriptions map[string]*prometheus.Desc
}

func NewExporter(endpoint string) *Exporter {
func NewExporter(endpoint string, scrapeTimeout time.Duration) *Exporter {
e := Exporter{
endpoint: endpoint,
registry: prometheus.NewRegistry(),
endpoint: endpoint,
scrapeTimeout: scrapeTimeout,
registry: prometheus.NewRegistry(),

totalScrapes: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "v2ray",
Expand Down Expand Up @@ -82,12 +84,12 @@ func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
}

func (e *Exporter) scrapeV2Ray(ch chan<- prometheus.Metric) error {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
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", err)
return fmt.Errorf("failed to dial: %w, timeout: %v", err, e.scrapeTimeout)
}
defer conn.Close()

Expand Down
13 changes: 8 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ import (
"fmt"
"net/http"
"os"
"time"

flags "github.com/jessevdk/go-flags"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
)

var opts struct {
Listen string `short:"l" long:"listen" description:"Listen address" value-name:"[ADDR]:PORT" default:":9550"`
MetricsPath string `short:"m" long:"metrics-path" description:"Metrics path" value-name:"PATH" default:"/scrape"`
V2RayEndpoint string `short:"e" long:"v2ray-endpoint" description:"V2Ray API endpoint" value-name:"HOST:PORT" default:"127.0.0.1:8080"`
Version bool `long:"version" description:"Show version"`
Listen string `short:"l" long:"listen" description:"Listen address" value-name:"[ADDR]:PORT" default:":9550"`
MetricsPath string `short:"m" long:"metrics-path" description:"Metrics path" value-name:"PATH" default:"/scrape"`
V2RayEndpoint string `short:"e" long:"v2ray-endpoint" description:"V2Ray API endpoint" value-name:"HOST:PORT" default:"127.0.0.1:8080"`
ScrapeTimeoutInSeconds int64 `short:"t" long:"scrape-timeout" description:"The timeout in seconds for every individual scrape" value-name:"N" default:"3"`
Version bool `long:"version" description:"Show version"`
}

var (
Expand Down Expand Up @@ -42,7 +44,8 @@ func main() {
os.Exit(0)
}

exporter = NewExporter(opts.V2RayEndpoint)
scrapeTimeout := time.Duration(opts.ScrapeTimeoutInSeconds) * time.Second
exporter = NewExporter(opts.V2RayEndpoint, scrapeTimeout)

http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/scrape", scrapeHandler)
Expand Down

0 comments on commit 1462d76

Please sign in to comment.