From 8ce04e4f28c39921f80672e961dc8eee9d420831 Mon Sep 17 00:00:00 2001 From: Ales Justin Date: Wed, 19 May 2021 23:56:17 +0200 Subject: [PATCH] Initial smoke test. --- go.mod | 2 +- go.sum | 2 + kafka_exporter.go | 24 +++++-- simple_test.go | 72 +++++++++++++++++++ .../prometheus/common/model/labels.go | 8 +++ vendor/modules.txt | 2 +- 6 files changed, 102 insertions(+), 8 deletions(-) create mode 100644 simple_test.go diff --git a/go.mod b/go.mod index 03de8da4..fc3b2e3d 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,7 @@ require ( github.com/pkg/errors v0.9.1 github.com/pkg/profile v1.2.1 // indirect github.com/prometheus/client_golang v1.10.0 - github.com/prometheus/common v0.24.0 + github.com/prometheus/common v0.25.0 github.com/prometheus/procfs v0.6.0 // indirect github.com/prometheus/promu v0.12.0 // indirect github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 diff --git a/go.sum b/go.sum index f9cbdd25..58096dbe 100644 --- a/go.sum +++ b/go.sum @@ -382,6 +382,8 @@ github.com/prometheus/common v0.20.0 h1:pfeDeUdQcIxOMutNjCejsEFp7qeP+/iltHSSmLpE github.com/prometheus/common v0.20.0/go.mod h1:U+gB1OBLb1lF3O42bTCL+FK18tX9Oar16Clt/msog/s= github.com/prometheus/common v0.24.0 h1:aIycr3wRFxPUq8XlLQlGQ9aNXV3dFi5y62pe/SB262k= github.com/prometheus/common v0.24.0/go.mod h1:H6QK/N6XVT42whUeIdI3dp36w49c+/iMDk7UAI2qm7Q= +github.com/prometheus/common v0.25.0 h1:IjJYZJCI8HZYtqA3xYwGyDzSCy1r4CA2GRh+4vdOmtE= +github.com/prometheus/common v0.25.0/go.mod h1:H6QK/N6XVT42whUeIdI3dp36w49c+/iMDk7UAI2qm7Q= github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d h1:GoAlyOgbOEIFdaDqxJVlbOQ1DtGmZWs/Qau0hIlk+WQ= github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= github.com/prometheus/procfs v0.0.0-20190117184657-bf6a532e95b1/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= diff --git a/kafka_exporter.go b/kafka_exporter.go index ee46d804..bd5c3dea 100644 --- a/kafka_exporter.go +++ b/kafka_exporter.go @@ -547,6 +547,18 @@ func main() { } } + setup(*listenAddress, *metricsPath, *topicFilter, *groupFilter, *logSarama, opts, labels) +} + +func setup( + listenAddress string, + metricsPath string, + topicFilter string, + groupFilter string, + logSarama bool, + opts kafkaOpts, + labels map[string]string, +) { clusterBrokers = prometheus.NewDesc( prometheus.BuildFQName(namespace, "", "brokers"), "Number of Brokers in the Kafka Cluster.", @@ -634,24 +646,24 @@ func main() { []string{"consumergroup"}, labels, ) - if *logSarama { + if logSarama { sarama.Logger = log.New(os.Stdout, "[sarama] ", log.LstdFlags) } - exporter, err := NewExporter(opts, *topicFilter, *groupFilter) + exporter, err := NewExporter(opts, topicFilter, groupFilter) if err != nil { plog.Fatalln(err) } defer exporter.client.Close() prometheus.MustRegister(exporter) - http.Handle(*metricsPath, promhttp.Handler()) + http.Handle(metricsPath, promhttp.Handler()) http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte(` Kafka Exporter

Kafka Exporter

-

Metrics

+

Metrics

`)) }) @@ -660,6 +672,6 @@ func main() { w.Write([]byte("ok")) }) - plog.Infoln("Listening on", *listenAddress) - plog.Fatal(http.ListenAndServe(*listenAddress, nil)) + plog.Infoln("Listening on", listenAddress) + plog.Fatal(http.ListenAndServe(listenAddress, nil)) } diff --git a/simple_test.go b/simple_test.go new file mode 100644 index 00000000..e3f94183 --- /dev/null +++ b/simple_test.go @@ -0,0 +1,72 @@ +package main + +import ( + "errors" + "github.com/Shopify/sarama" + "io/ioutil" + "log" + "net/http" + "testing" + "time" +) + +var bootstrap_servers = []string{"localhost:9092"} + +func TestSmoke(t *testing.T) { + log.Print("testing " + t.Name()) + + if !assumeKafka(t) { + t.Skip("Kafka is not running ... skipping the test") + return + } + + go runServer() + + execute(func(resp *http.Response) { + log.Println(resp.Status) + + defer resp.Body.Close() + bytes, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Fatalln(err) + } else { + log.Println(string(bytes)) + } + }) +} + +func assumeKafka(t *testing.T) bool { + client, err := sarama.NewClient(bootstrap_servers, nil) + if err != nil { + return false + } + defer client.Close() + _, err = client.Topics() + if err != nil { + return false + } + return true +} + +func execute(handler func(response *http.Response)) { + var e = errors.New("dummy") + for e != nil { + resp, err := http.Get("http://localhost:9304/metrics") + if err != nil { + time.Sleep(time.Millisecond * 100) + } + e = err + if resp != nil { + handler(resp) + } + } +} + +func runServer() { + opts := kafkaOpts{} + opts.uri = bootstrap_servers + opts.uriZookeeper = []string{"localhost:2181"} + opts.kafkaVersion = sarama.V1_0_0_0.String() + opts.metadataRefreshInterval = "30s" + setup("localhost:9304", "/metrics", ".*", ".*", false, opts, nil) +} diff --git a/vendor/github.com/prometheus/common/model/labels.go b/vendor/github.com/prometheus/common/model/labels.go index 41051a01..ef895633 100644 --- a/vendor/github.com/prometheus/common/model/labels.go +++ b/vendor/github.com/prometheus/common/model/labels.go @@ -45,6 +45,14 @@ const ( // scrape a target. MetricsPathLabel = "__metrics_path__" + // ScrapeIntervalLabel is the name of the label that holds the scrape interval + // used to scrape a target. + ScrapeIntervalLabel = "__scrape_interval__" + + // ScrapeTimeoutLabel is the name of the label that holds the scrape + // timeout used to scrape a target. + ScrapeTimeoutLabel = "__scrape_timeout__" + // ReservedLabelPrefix is a prefix which is not legal in user-supplied // label names. ReservedLabelPrefix = "__" diff --git a/vendor/modules.txt b/vendor/modules.txt index cf942018..61cd957f 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -105,7 +105,7 @@ github.com/prometheus/client_golang/prometheus/internal github.com/prometheus/client_golang/prometheus/promhttp # github.com/prometheus/client_model v0.2.0 github.com/prometheus/client_model/go -# github.com/prometheus/common v0.24.0 +# github.com/prometheus/common v0.25.0 ## explicit github.com/prometheus/common/expfmt github.com/prometheus/common/internal/bitbucket.org/ww/goautoneg