Skip to content

Commit

Permalink
Direct instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasmalkmus committed Aug 7, 2019
1 parent da9732c commit 5e521cf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 42 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ Help on flags:
Docker images are now available on [DockerHub]!

```bash
docker run -p9386:9386/tcp lukasmalkmus/tankerkoenig-exporter:v0.6.0 \
--api.key="YOUR_API_TOKEN" \
docker run -p9386:9386/tcp -e TANKERKOENIG_API_KEY="YOUR_API_TOKEN" lukasmalkmus/tankerkoenig-exporter:v0.7.0 \
--api.stations="9646eb5e-b7ae-4205-bdbd-0a64abc46c20,7566fb7a-b7cc-5214-bcad-0a53abd46d14"
```

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.6.0
0.7.0
66 changes: 27 additions & 39 deletions tankerkoenig_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ const (
)

var (
apiKey = kingpin.Flag("api.key", "Personal API key used to authenticate against the tankerkoenig API").String()
apiStations = kingpin.Flag("api.stations", "ID of a station. Flag can be reused multiple times.").Short('s').Strings()
webListenAddress = kingpin.Flag("web.listen-address", "Address on which to expose metrics and web interface").Default(":9386").String()
webMetricsPath = kingpin.Flag("web.telemetry-path", "Path under which to expose metrics").Default("/metrics").String()
Expand Down Expand Up @@ -69,8 +68,8 @@ type Exporter struct {
totalScrapes, failedScrapes prometheus.Counter

// Tankerkoenig metrics.
price *prometheus.GaugeVec
open *prometheus.GaugeVec
priceDesc *prometheus.Desc
openDesc *prometheus.Desc
}

// New returns a new, initialized Tankerkoenig Exporter.
Expand Down Expand Up @@ -115,18 +114,18 @@ func New(apiKey string, apiStations []string) (*Exporter, error) {
Name: "scrape_failures_total",
Help: "Total amount of scrape failures.",
}),
price: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "station",
Name: "price_euro",
Help: "Gas prices in EURO (€).",
}, []string{"station_id", "station_name", "product"}),
open: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "station",
Name: "open",
Help: "Status of the station. 1 for OPEN, 0 for CLOSED.",
}, []string{"station_id", "station_name"}),
priceDesc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "station", "price_euro"),
"Gas prices in EURO (€).",
[]string{"station_id", "station_name", "product"},
nil,
),
openDesc: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "station", "open"),
"Status of the station. 1 for OPEN, 0 for CLOSED.",
[]string{"station_id", "station_name"},
nil,
),
}

// Retrieve initial station details to validate integrity of user provided
Expand All @@ -149,8 +148,8 @@ func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
e.scrapeDuration.Describe(ch)
e.failedScrapes.Describe(ch)
e.totalScrapes.Describe(ch)
e.price.Describe(ch)
e.open.Describe(ch)
ch <- e.priceDesc
ch <- e.openDesc
}

// Collect the stats from the Tankerkoenig API.
Expand All @@ -160,11 +159,8 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
e.mutex.Lock()
defer e.mutex.Unlock()

// Reset metrics.
e.reset()

// Scrape metrics from Tankerkoenig API.
if err := e.scrape(); err != nil {
if err := e.scrape(ch); err != nil {
log.Error(err)
}

Expand All @@ -173,18 +169,10 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
e.scrapeDuration.Collect(ch)
e.failedScrapes.Collect(ch)
e.totalScrapes.Collect(ch)
e.price.Collect(ch)
e.open.Collect(ch)
}

// reset resets the vector metrics.
func (e *Exporter) reset() {
e.open.Reset()
e.price.Reset()
}

// scrape performs the API call and meassures its duration.
func (e *Exporter) scrape() error {
func (e *Exporter) scrape(ch chan<- prometheus.Metric) error {
// Meassure scrape duration.
defer func(begun time.Time) {
e.scrapeDuration.Set(time.Since(begun).Seconds())
Expand Down Expand Up @@ -214,20 +202,20 @@ func (e *Exporter) scrape() error {
if stat := p.Status; stat == "no prices" {
continue
} else if stat == "open" {
e.open.WithLabelValues(id, name).Set(1.0)
ch <- prometheus.MustNewConstMetric(e.openDesc, prometheus.GaugeValue, 1.0, id, name)
} else {
e.open.WithLabelValues(id, name).Set(0.0)
ch <- prometheus.MustNewConstMetric(e.openDesc, prometheus.GaugeValue, 0.0, id, name)
}

// Station prices.
if f, ok := p.Diesel.(float64); ok {
e.price.WithLabelValues(id, name, "diesel").Set(f)
if v, ok := p.Diesel.(float64); ok {
ch <- prometheus.MustNewConstMetric(e.priceDesc, prometheus.GaugeValue, v, id, name, "diesel")
}
if f, ok := p.E5.(float64); ok {
e.price.WithLabelValues(id, name, "e5").Set(f)
if v, ok := p.E5.(float64); ok {
ch <- prometheus.MustNewConstMetric(e.priceDesc, prometheus.GaugeValue, v, id, name, "e5")
}
if f, ok := p.E10.(float64); ok {
e.price.WithLabelValues(id, name, "e10").Set(f)
if v, ok := p.E10.(float64); ok {
ch <- prometheus.MustNewConstMetric(e.priceDesc, prometheus.GaugeValue, v, id, name, "e10")
}
}

Expand All @@ -248,7 +236,7 @@ func main() {
log.Info("Build context", version.BuildContext())

// Create a new Tankerkoenig exporter. Exit if an error is returned.
exporter, err := New(*apiKey, *apiStations)
exporter, err := New(os.Getenv("TANKERKOENIG_API_KEY"), *apiStations)
if err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit 5e521cf

Please sign in to comment.