Skip to content

Commit

Permalink
feat: Add healthcheck (prometheus-community#15)
Browse files Browse the repository at this point in the history
Signed-off-by: grzesuav <grzesuav@gmail.com>
  • Loading branch information
grzesuav authored and jt-kloudfuse committed Mar 23, 2021
1 parent d7981b3 commit 7df882f
Show file tree
Hide file tree
Showing 9 changed files with 676 additions and 162 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ avalanche --help
docker run quay.io/freshtracks.io/avalanche --help
```

## Endpoints

Two endpoints are available :
* `/metrics` - metrics endpoint
* `/health` - healthcheck endpoint

## build and run go binary
```bash
go get github.com/open-fresh/avalanche/cmd/...
Expand Down
11 changes: 11 additions & 0 deletions avalanche.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
metricPrefix: "comp1"
minSamples: 10
maxSamples: 10
LableCount: 1
labelCount: 2
valueInterval: 2
metricInterval: 4

cardinalityMap:
51: 2
49: 15
120 changes: 21 additions & 99 deletions cmd/avalanche.go
Original file line number Diff line number Diff line change
@@ -1,118 +1,40 @@
package main

import (
"flag"
"fmt"
"log"
"math/rand"
"strconv"
"sync"
"time"
"os"

"github.com/open-fresh/avalanche/metrics"
"github.com/open-fresh/avalanche/pkg/download"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)

var (
metricCount = kingpin.Flag("metric-count", "Number of metrics to serve.").Default("500").Int()
labelCount = kingpin.Flag("label-count", "Number of labels per-metric.").Default("10").Int()
seriesCount = kingpin.Flag("series-count", "Number of series per-metric.").Default("10").Int()
metricLength = kingpin.Flag("metricname-length", "Modify length of metric names.").Default("5").Int()
labelLength = kingpin.Flag("labelname-length", "Modify length of label names.").Default("5").Int()
constLabels = kingpin.Flag("const-label", "Constant label to add to every metric. Format is labelName=labelValue. Flag can be specified multiple times.").Strings()
valueInterval = kingpin.Flag("value-interval", "Change series values every {interval} seconds.").Default("30").Int()
labelInterval = kingpin.Flag("series-interval", "Change series_id label values every {interval} seconds.").Default("60").Int()
metricInterval = kingpin.Flag("metric-interval", "Change __name__ label values every {interval} seconds.").Default("120").Int()
port = kingpin.Flag("port", "Port to serve at").Default("9001").Int()
remoteURL = kingpin.Flag("remote-url", "URL to send samples via remote_write API.").URL()
remotePprofURLs = kingpin.Flag("remote-pprof-urls", "a list of urls to download pprofs during the remote write: --remote-pprof-urls=http://127.0.0.1:10902/debug/pprof/heap --remote-pprof-urls=http://127.0.0.1:10902/debug/pprof/profile").URLList()
remotePprofInterval = kingpin.Flag("remote-pprof-interval", "how often to download pprof profiles.When not provided it will download a profile once before the end of the test.").Duration()
remoteBatchSize = kingpin.Flag("remote-batch-size", "how many samples to send with each remote_write API request.").Default("2000").Int()
remoteRequestCount = kingpin.Flag("remote-requests-count", "how many requests to send in total to the remote_write API.").Default("100").Int()
remoteReqsInterval = kingpin.Flag("remote-write-interval", "delay between each remote write request.").Default("100ms").Duration()
remoteTenant = kingpin.Flag("remote-tenant", "Tenant ID to include in remote_write send").Default("0").String()
log "github.com/sirupsen/logrus"
)

func main() {
kingpin.Version("0.3")
log.SetFlags(log.Ltime | log.Lshortfile) // Show file name and line in logs.
kingpin.CommandLine.Help = "avalanche - metrics test server"
kingpin.Parse()
var cfgFile string
flag.StringVar(&cfgFile, "config", "", "Path to config file.")
flag.Parse()

stop := make(chan struct{})
defer close(stop)
updateNotify, err := metrics.RunMetrics(*metricCount, *labelCount, *seriesCount, *metricLength, *labelLength, *valueInterval, *labelInterval, *metricInterval, *constLabels, stop)
if err != nil {
log.Fatal(err)
if cfgFile == "" {
log.Fatal("Config is a required argument")
os.Exit(-1)
}

if *remoteURL != nil {
if (**remoteURL).Host == "" || (**remoteURL).Scheme == "" {
log.Fatal("remote host and scheme can't be empty")
}
if *remoteBatchSize <= 0 {
log.Fatal("remote send batch size should be more than zero")
}

config := metrics.ConfigWrite{
URL: **remoteURL,
RequestInterval: *remoteReqsInterval,
BatchSize: *remoteBatchSize,
RequestCount: *remoteRequestCount,
UpdateNotify: updateNotify,
Tenant: *remoteTenant,
}

// Collect Pprof during the write only if not collecting within a regular interval.
if *remotePprofInterval == 0 {
config.PprofURLs = *remotePprofURLs
}
cfg, err := metrics.LoadConfigurationFromFile(cfgFile)
if err != nil {
log.Fatal("Failed to parse config file")
os.Exit(-1)
}

var (
wg sync.WaitGroup
done = make(chan struct{})
)
if *remotePprofInterval > 0 {
if len(*remotePprofURLs) == 0 {
log.Fatal("remote profiling interval specified wihout any remote pprof urls")
}
rand.Seed(time.Now().UnixNano())
suffix := rand.Intn(1000)
go func() {
ticker := time.NewTicker(*remotePprofInterval)
var dur time.Duration
loop:
for {
select {
case <-ticker.C:
select {
case <-done: // Prevents a panic when calling wg.Add(1) after calling wg.Wait().
break loop
default:
}
dur += *remotePprofInterval
wg.Add(1)
download.URLs(*remotePprofURLs, strconv.Itoa(suffix)+"-"+dur.String())
wg.Done()
}
}
}()
stop := make(chan struct{})
defer close(stop)

}
// First cut: just send the metrics once then exit
err := metrics.SendRemoteWrite(config)
if err != nil {
log.Fatal(err)
}
if *remotePprofInterval > 0 {
done <- struct{}{}
wg.Wait()
}
return
err = metrics.RunMetrics(cfg, stop)
if err != nil {
log.Fatal(err)
}

fmt.Printf("Serving ur metrics at localhost:%v/metrics\n", *port)
err = metrics.ServeMetrics(*port)
fmt.Printf("Serving ur metrics at localhost:%v/metrics\n", cfg.Port)
err = metrics.ServeMetrics(cfg.Port)
if err != nil {
log.Fatal(err)
}
Expand Down
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@ go 1.14

require (
github.com/alecthomas/units v0.0.0-20201120081800-1786d5ef83d4 // indirect
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6 // indirect
github.com/coreos/go-etcd v2.0.0+incompatible // indirect
github.com/gogo/protobuf v1.3.1
github.com/golang/snappy v0.0.2
github.com/nelkinda/health-go v0.0.1
github.com/prometheus/client_golang v1.9.0
github.com/prometheus/client_model v0.2.0
github.com/prometheus/common v0.15.0
github.com/prometheus/prometheus v1.8.2-0.20201119181812-c8f810083d3f
github.com/sirupsen/logrus v1.6.0 // indirect
github.com/spf13/viper v1.7.1 // indirect
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8 // indirect
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77 // indirect
golang.org/x/sys v0.0.0-20201223074533-0d417f636930 // indirect
gopkg.in/alecthomas/kingpin.v2 v2.2.6
)
Loading

0 comments on commit 7df882f

Please sign in to comment.