Skip to content

Commit

Permalink
Merge pull request #32 from opstree/duplicate-fix
Browse files Browse the repository at this point in the history
[BugFix][Change] Fixed duplicate metrics 500 issue
  • Loading branch information
iamabhishek-dubey authored Jun 22, 2020
2 parents bb355bb + 7f4157a commit 635df9d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
### v0.7
##### June 22, 2020

#### :beetle: Bug Fixes

- Fixed JSON parser failure issue
- Fixed duplicate task metric's server 500 issue

### v0.6
##### June 11, 2020

Expand Down
5 changes: 4 additions & 1 deletion collector/druid.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import (
)

var (
druid = kingpin.Flag("druid.uri", "URL of druid router or coordinator, EnvVar - DRUID_URL").Default("http://druid.opstreelabs.in").OverrideDefaultFromEnvar("DRUID_URL").Short('d').String()
druid = kingpin.Flag(
"druid.uri",
"URL of druid router or coordinator, EnvVar - DRUID_URL",
).Default("http://druid.opstreelabs.in").OverrideDefaultFromEnvar("DRUID_URL").Short('d').String()
)

// GetDruidHealthMetrics returns the set of metrics for druid
Expand Down
35 changes: 29 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,18 @@ import (
)

var (
port = kingpin.Flag("port", "Port to listen druid exporter, EnvVar - DRUID_EXPORTER_PORT. (Default - 8080)").Default("8080").OverrideDefaultFromEnvar("DRUID_EXPORTER_PORT").Short('p').String()
logLevel = kingpin.Flag("log.level", "Log level for druid exporter, EnvVar - LOG_LEVEL. (Default: info)").Default("info").OverrideDefaultFromEnvar("LOG_LEVEL").Short('l').String()
logFormat = kingpin.Flag("log.format", "Log format for druid exporter, text or json, EnvVar - LOG_FORMAT. (Default: text)").Default("text").OverrideDefaultFromEnvar("LOG_FORMAT").Short('f').String()
port = kingpin.Flag(
"port",
"Port to listen druid exporter, EnvVar - DRUID_EXPORTER_PORT. (Default - 8080)",
).Default("8080").OverrideDefaultFromEnvar("DRUID_EXPORTER_PORT").Short('p').String()
logLevel = kingpin.Flag(
"log.level",
"Log level for druid exporter, EnvVar - LOG_LEVEL. (Default: info)",
).Default("info").OverrideDefaultFromEnvar("LOG_LEVEL").Short('l').String()
logFormat = kingpin.Flag(
"log.format",
"Log format for druid exporter, text or json, EnvVar - LOG_FORMAT. (Default: text)",
).Default("text").OverrideDefaultFromEnvar("LOG_FORMAT").Short('f').String()
druidEmittedData = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "druid_emitted_metrics",
Expand All @@ -24,8 +33,6 @@ var (
)

func init() {
getDruidAPIdata := collector.Collector()
prometheus.MustRegister(getDruidAPIdata)
prometheus.MustRegister(druidEmittedData)
}

Expand All @@ -47,8 +54,10 @@ func main() {
})
}
router := mux.NewRouter()
getDruidAPIdata := collector.Collector()
handlerFunc := newHandler(*getDruidAPIdata)
router.Handle("/druid", listener.DruidHTTPEndpoint(druidEmittedData))
router.Handle("/metrics", promhttp.Handler())
router.Handle("/metrics", promhttp.InstrumentMetricHandler(prometheus.DefaultRegisterer, handlerFunc))
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>Druid Exporter</title></head>
Expand All @@ -63,3 +72,17 @@ func main() {
logrus.Infof("Druid emitter endpoint - http://0.0.0.0:%v/druid", *port)
http.ListenAndServe("0.0.0.0:"+*port, router)
}

func newHandler(metrics collector.MetricCollector) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
registry := prometheus.NewRegistry()
getDruidAPIdata := collector.Collector()
registry.MustRegister(getDruidAPIdata)
gatherers := prometheus.Gatherers{
prometheus.DefaultGatherer,
registry,
}
h := promhttp.HandlerFor(gatherers, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
}
}

0 comments on commit 635df9d

Please sign in to comment.