Skip to content

Commit ac86d5a

Browse files
committed
Allow set custom interval
1 parent 1c9245f commit ac86d5a

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

cmd/exporter/flags/flags.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ package flags
22

33
import "flag"
44

5-
func Parse(arguments []string) (namespace, binary, argName string, port int) {
5+
func Parse(arguments []string) (namespace, binary, argName string, port int, interval float64) {
66
flagSet := flag.NewFlagSet("flags", flag.ExitOnError)
77
namespacePtr := flagSet.String("namespace", "mine", "Prometheus metric namespace.")
88
binaryPtr := flagSet.String("binary", "", "Filter which processes to watch by binary name. This is limited to 15 bytes because of the kernel.")
99
nameFlagPtr := flagSet.String("nameflag", "name", "Set Prometheus \"name\"-label value to value of this command line argument of the monitored processes.")
1010
portPtr := flagSet.Int("port", 80, "Port on which to listen to requests to /metrics.")
11+
intervalPtr := flagSet.Float64("interval", 10, "Interval between refreshes of metrics, in seconds. Should not be too large to prevent CPU reading from getting noisy.")
1112
err := flagSet.Parse(arguments)
1213
if err != nil {
1314
panic(err)
1415
}
15-
return *namespacePtr, *binaryPtr, *nameFlagPtr, *portPtr
16+
return *namespacePtr, *binaryPtr, *nameFlagPtr, *portPtr, *intervalPtr
1617
}

cmd/exporter/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,24 @@ import (
1313
)
1414

1515
func main() {
16-
namespace, procBinaryName, nameFlag, port := flags.Parse(os.Args[1:])
16+
namespace, procBinaryName, nameFlag, port, interval := flags.Parse(os.Args[1:])
1717
metricsSet := metrics.NewPrometheusProcessMetricsSet(namespace, procBinaryName, nameFlag)
1818
mu := &sync.Mutex{}
1919
updateMetricsSet(metricsSet, mu)
2020
// ctx, cancelFunc := context.WithCancel(context.Background())
21-
go keepMetricsUpToDate(metricsSet, mu)
21+
go keepMetricsUpToDate(metricsSet, mu, time.Duration(float64(time.Second)*interval))
2222
http.Handle("/metrics", newHttpMetricsRequestHandler(metricsSet, mu))
2323
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
2424
}
2525

26-
func keepMetricsUpToDate(set *metrics.PrometheusProcessMetricsSet, mu *sync.Mutex) {
26+
func keepMetricsUpToDate(set *metrics.PrometheusProcessMetricsSet, mu *sync.Mutex, interval time.Duration) {
2727
defer func() {
2828
if r := recover(); r != nil {
2929
defer func() { go func() { time.Sleep(time.Second); log.Exit(1) }() }()
3030
log.Panicf("Panic in keepMetricsUpToDate(): %v", r)
3131
}
3232
}()
33-
ticker := time.NewTicker(time.Second * 10)
33+
ticker := time.NewTicker(interval)
3434
for {
3535
select {
3636
case <-ticker.C:

0 commit comments

Comments
 (0)