Skip to content

Commit

Permalink
Merge branch 'export_cluster-wide_node_stats' of https://github.com/d…
Browse files Browse the repository at this point in the history
…an-cleinmark/elasticsearch_exporter into dan-cleinmark-export_cluster-wide_node_stats
  • Loading branch information
ewr committed Jun 17, 2015
2 parents c13e371 + eacd599 commit 5d9b679
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ make
./elasticsearch_exporter --help
```

* __`es.server`:__ Address (host and port) of the Elasticsearch node we should
* __`es.uri`:__ Address (host and port) of the Elasticsearch node we should
connect to. This could be a local node (`localhost:8500`, for instance), or
the address of a remote Elasticsearch server.
* __`es.cluster_nodes`:__ If true, query stats for all nodes in the cluster,
* __`es.all`:__ If true, query stats for all nodes in the cluster,
rather than just the node we connect to.
* __`es.timeout`:__ Timeout for trying to get stats from Elasticsearch. (ex: 20s)
* __`web.listen-address`:__ Address to listen on for web interface and telemetry.
* __`web.telemetry-path`:__ Path under which to expose metrics.

__NOTE:__ We support pulling stats for all nodes at once, but in production
this is unlikely to be the way you actually want to run the system. It is much
better to run an exporter on each Elasticsearch node to remove a single point
of failure and improve the connection between operation and reporting.
22 changes: 16 additions & 6 deletions elasticsearch_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,13 @@ type Exporter struct {
counters map[string]*prometheus.CounterVec
counterVecs map[string]*prometheus.CounterVec

allNodes bool

client *http.Client
}

// NewExporter returns an initialized Exporter.
func NewExporter(uri string, timeout time.Duration) *Exporter {
func NewExporter(uri string, timeout time.Duration, allNodes bool) *Exporter {
counters := make(map[string]*prometheus.CounterVec, len(counterMetrics))
counterVecs := make(map[string]*prometheus.CounterVec, len(counterVecMetrics))
gauges := make(map[string]*prometheus.GaugeVec, len(gaugeMetrics))
Expand Down Expand Up @@ -167,6 +169,8 @@ func NewExporter(uri string, timeout time.Duration) *Exporter {
gauges: gauges,
gaugeVecs: gaugeVecs,

allNodes: allNodes,

client: &http.Client{
Transport: &http.Transport{
Dial: func(netw, addr string) (net.Conn, error) {
Expand Down Expand Up @@ -237,9 +241,9 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
return
}

// We only expose metrics for the local node, not the whole cluster.
if l := len(allStats.Nodes); l != 1 {
log.Println("Unexpected number of nodes returned:", l)
// If we aren't polling all nodes, make sure we only got one response.
if !e.allNodes && len(allStats.Nodes) != 1 {
log.Println("Unexpected number of nodes returned.")
}

for _, stats := range allStats.Nodes {
Expand Down Expand Up @@ -325,12 +329,18 @@ func main() {
metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
esURI = flag.String("es.uri", "http://localhost:9200", "HTTP API address of an Elasticsearch node.")
esTimeout = flag.Duration("es.timeout", 5*time.Second, "Timeout for trying to get stats from Elasticsearch.")
esAllNodes = flag.Bool("es.all", false, "Export stats for all nodes in the cluster.")
)
flag.Parse()

*esURI = *esURI + "/_nodes/_local/stats"

exporter := NewExporter(*esURI, *esTimeout)
if *esAllNodes {
*esURI = *esURI + "/_nodes/stats"
} else {
*esURI = *esURI + "/_nodes/_local/stats"
}

exporter := NewExporter(*esURI, *esTimeout, *esAllNodes)
prometheus.MustRegister(exporter)

log.Println("Starting Server:", *listenAddress)
Expand Down

0 comments on commit 5d9b679

Please sign in to comment.