Skip to content

Commit

Permalink
Add metrics from /metrics/snapshot
Browse files Browse the repository at this point in the history
This needs to be reworked once we figure out what every metric exactly
means. Right now it just blindly exposes them which isn't best practice.
  • Loading branch information
discordianfish committed Oct 23, 2015
1 parent 301a387 commit 9a2c143
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 27 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ leader and one exporter for each slave with `-slave` pointing to it. In
a default mesos / DCOS setup, you should be able to run the mesos-exporter
like this:

- Master: `mesos-exporter -master http://leader.mesos:5050/state.json`
- Slave: `mesos-exporter -slave http://localhost:5051/monitor/statistics.json`
- Master: `mesos-exporter -master http://leader.mesos:5050`
- Slave: `mesos-exporter -slave http://localhost:5051`
14 changes: 2 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"flag"
"log"
"net/http"
"net/url"
"os"
"time"

Expand All @@ -25,22 +24,13 @@ func main() {

switch {
case *masterURL != "":
url, err := url.Parse(*masterURL)
if err != nil {
log.Fatal(err)
}

c := newMasterCollector(url, *timeout)
c := newMasterCollector(*masterURL, *timeout)
if err := prometheus.Register(c); err != nil {
log.Fatal(err)
}
log.Printf("Exposing master metrics on %s", *addr)
case *slaveURL != "":
url, err := url.Parse(*slaveURL)
if err != nil {
log.Fatal(err)
}
c := newSlaveCollector(url, *timeout)
c := newSlaveCollector(*slaveURL, *timeout)
if err := prometheus.Register(c); err != nil {
log.Fatal(err)
}
Expand Down
17 changes: 12 additions & 5 deletions master.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"log"
"net/http"
"net/url"
"strconv"
"time"

Expand Down Expand Up @@ -63,16 +62,16 @@ type (

masterCollector struct {
*http.Client
*url.URL
url string
metrics map[prometheus.Collector]func(*state, prometheus.Collector)
}
)

func newMasterCollector(url *url.URL, timeout time.Duration) *masterCollector {
func newMasterCollector(url string, timeout time.Duration) *masterCollector {
labels := []string{"slave"}
return &masterCollector{
Client: &http.Client{Timeout: timeout},
URL: url,
url: url,
metrics: map[prometheus.Collector]func(*state, prometheus.Collector){
prometheus.NewGaugeVec(prometheus.GaugeOpts{
Help: "Total slave CPUs (fractional)",
Expand Down Expand Up @@ -228,7 +227,7 @@ func newMasterCollector(url *url.URL, timeout time.Duration) *masterCollector {
}

func (c *masterCollector) Collect(ch chan<- prometheus.Metric) {
res, err := c.Do(&http.Request{Method: "GET", URL: c.URL})
res, err := c.Get(c.url + "/state.json")
if err != nil {
log.Print(err)
return
Expand All @@ -245,6 +244,14 @@ func (c *masterCollector) Collect(ch chan<- prometheus.Metric) {
set(&s, c)
c.Collect(ch)
}

res, err = c.Get(c.url + "/metrics/snapshot")
if err != nil {
log.Print(err)
return
}
defer res.Body.Close()
snapshotCollect(ch, res.Body)
}

func (c *masterCollector) Describe(ch chan<- *prometheus.Desc) {
Expand Down
21 changes: 13 additions & 8 deletions slave.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"log"
"net/http"
"net/url"
"time"

"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -40,8 +39,7 @@ type (

slaveCollector struct {
*http.Client
*url.URL
errors *prometheus.Counter
url string
metrics map[*prometheus.Desc]metric
}

Expand All @@ -51,11 +49,12 @@ type (
}
)

func newSlaveCollector(url *url.URL, timeout time.Duration) *slaveCollector {
func newSlaveCollector(url string, timeout time.Duration) *slaveCollector {
labels := []string{"id", "framework_id", "source"}

return &slaveCollector{
Client: &http.Client{Timeout: timeout},
URL: url,
url: url,
metrics: map[*prometheus.Desc]metric{
// CPU
prometheus.NewDesc(
Expand Down Expand Up @@ -139,18 +138,16 @@ func newSlaveCollector(url *url.URL, timeout time.Duration) *slaveCollector {
}

func (c *slaveCollector) Collect(ch chan<- prometheus.Metric) {
res, err := http.Get(c.URL.String())
res, err := http.Get(c.url + "/monitor/statistics.json")
if err != nil {
log.Print(err)
// c.errors.Inc()
return
}
defer res.Body.Close()

stats := []executor{}
if err := json.NewDecoder(res.Body).Decode(&stats); err != nil {
log.Print(err)
// c.errors.Inc()
return
}

Expand All @@ -159,6 +156,14 @@ func (c *slaveCollector) Collect(ch chan<- prometheus.Metric) {
ch <- prometheus.MustNewConstMetric(desc, m.valueType, m.get(exec.Statistics), exec.ID, exec.FrameworkID, exec.Source)
}
}

res, err = http.Get(c.url + "/metrics/snapshot")
if err != nil {
log.Print(err)
return
}
defer res.Body.Close()
snapshotCollect(ch, res.Body)
}

func (c *slaveCollector) Describe(ch chan<- *prometheus.Desc) {
Expand Down

0 comments on commit 9a2c143

Please # to comment.