Skip to content

Commit

Permalink
Add up metric
Browse files Browse the repository at this point in the history
  • Loading branch information
diogonicoleti committed Nov 30, 2017
1 parent c8b2d96 commit b7dbe6d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
20 changes: 20 additions & 0 deletions exporter/newrelic_exporter.go → exporter/exporter.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
package exporter

import (
"sync"

"github.com/ContaAzul/newrelic_exporter/newrelic"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
)

const namespace = "newrelic"

// Exporter collects metrics from a NewRelic application
type Exporter struct {
mutex sync.RWMutex
client *newrelic.Client

up *prometheus.Desc
}

// NewExporter returns an initialized exporter.
func NewExporter(apiKey string) *Exporter {
return &Exporter{
client: newrelic.NewClient(apiKey),
up: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "up"),
"NewRelic API is up and accepting requests",
Expand All @@ -32,5 +40,17 @@ func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
// Collect fetches the metrics data from the NewRelic application and
// delivers them as Prometheus metrics. It implements prometheus.Collector.
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
// To protect metrics from concurrent collects.
e.mutex.Lock()
defer e.mutex.Unlock()

//TODO: Use correct application ID to scrape data
_, err := e.client.ListInstances(0)
if err != nil {
log.Errorf("Failed to get application instances: %v", err)
ch <- prometheus.MustNewConstMetric(e.up, prometheus.GaugeValue, 0)
return
}

ch <- prometheus.MustNewConstMetric(e.up, prometheus.GaugeValue, 1)
}
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/ContaAzul/newrelic_exporter/exporter"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/log"
"gopkg.in/alecthomas/kingpin.v2"
)
Expand All @@ -29,7 +30,7 @@ func main() {

prometheus.MustRegister(exporter.NewExporter(*apiKey))

http.Handle("/metrics", prometheus.Handler())
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w,
`
Expand All @@ -45,6 +46,6 @@ func main() {

log.Infof("Server listening on %s", *addr)
if err := http.ListenAndServe(*addr, nil); err != nil {
log.Fatalf("Error starting server: %s", err)
log.Fatalf("Error starting server: %v", err)
}
}

0 comments on commit b7dbe6d

Please sign in to comment.