Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial smoke test. #223

Merged
merged 1 commit into from
May 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
24 changes: 18 additions & 6 deletions kafka_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down Expand Up @@ -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(`<html>
<head><title>Kafka Exporter</title></head>
<body>
<h1>Kafka Exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
<p><a href='` + metricsPath + `'>Metrics</a></p>
</body>
</html>`))
})
Expand All @@ -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))
}
72 changes: 72 additions & 0 deletions simple_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
8 changes: 8 additions & 0 deletions vendor/github.com/prometheus/common/model/labels.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down