Skip to content

Commit

Permalink
Collect application summary data
Browse files Browse the repository at this point in the history
  • Loading branch information
diogonicoleti committed Jan 19, 2018
1 parent e7bbad3 commit 6f23d82
Showing 1 changed file with 44 additions and 3 deletions.
47 changes: 44 additions & 3 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ type newRelicCollector struct {

up *prometheus.Desc
scrapeDuration *prometheus.Desc
appSummaryApdexScore *prometheus.Desc
appSummaryErrorRate *prometheus.Desc
appSummaryResponseTime *prometheus.Desc
appSummaryThroughput *prometheus.Desc
instanceSummaryApdexScore *prometheus.Desc
instanceSummaryErrorRate *prometheus.Desc
instanceSummaryResponseTime *prometheus.Desc
Expand All @@ -44,17 +48,30 @@ func NewNewRelicCollector(apiKey string, config config.Config) prometheus.Collec
nil,
nil,
),
appSummaryApdexScore: newAppSummaryDesc("apdex_score"),
appSummaryErrorRate: newAppSummaryDesc("error_rate"),
appSummaryResponseTime: newAppSummaryDesc("response_time"),
appSummaryThroughput: newAppSummaryDesc("throughput"),
instanceSummaryApdexScore: newInstanceSummaryDesc("apdex_score"),
instanceSummaryErrorRate: newInstanceSummaryDesc("error_rate"),
instanceSummaryResponseTime: newInstanceSummaryDesc("response_time"),
instanceSummaryThroughput: newInstanceSummaryDesc("throughput"),
}
}

func newAppSummaryDesc(name string) *prometheus.Desc {
return prometheus.NewDesc(
prometheus.BuildFQName(namespace, "app_summary", name),
"Application rolling three-to-four-minute average for "+strings.Replace(name, "_", " ", -1),
[]string{"app"},
nil,
)
}

func newInstanceSummaryDesc(name string) *prometheus.Desc {
return prometheus.NewDesc(
prometheus.BuildFQName(namespace, "instance_summary", name),
"Instance rolling three-to-four-minute average for "+strings.Replace(name, "_", " ", -1),
"Application instance rolling three-to-four-minute average for "+strings.Replace(name, "_", " ", -1),
[]string{"app", "instance"},
nil,
)
Expand All @@ -65,6 +82,10 @@ func newInstanceSummaryDesc(name string) *prometheus.Desc {
func (c *newRelicCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.up
ch <- c.scrapeDuration
ch <- c.appSummaryApdexScore
ch <- c.appSummaryErrorRate
ch <- c.appSummaryResponseTime
ch <- c.appSummaryThroughput
ch <- c.instanceSummaryApdexScore
ch <- c.instanceSummaryErrorRate
ch <- c.instanceSummaryResponseTime
Expand All @@ -81,20 +102,40 @@ func (c *newRelicCollector) Collect(ch chan<- prometheus.Metric) {
start := time.Now()
for _, app := range c.config.Applications {
log.Infof("Collecting metrics from application: %s", app.Name)
application, err := c.client.ShowApplication(app.ID)
if err != nil {
ch <- prometheus.MustNewConstMetric(c.up, prometheus.GaugeValue, 0)
log.Errorf("Failed to get application: %v", err)
return
}
c.collectApplicationSummary(ch, app.Name, application)

instances, err := c.client.ListInstances(app.ID)
if err != nil {
ch <- prometheus.MustNewConstMetric(c.up, prometheus.GaugeValue, 0)
log.Errorf("Failed to get application instances: %v", err)
return
}

c.collectInstanceSummary(ch, app.Name, instances)
}

ch <- prometheus.MustNewConstMetric(c.up, prometheus.GaugeValue, 1)
ch <- prometheus.MustNewConstMetric(c.scrapeDuration, prometheus.GaugeValue, time.Since(start).Seconds())
}

func (c *newRelicCollector) collectApplicationSummary(ch chan<- prometheus.Metric,
appName string, application newrelic.Application) {
if application.ApplicationSummary.InstanceCount > 0 {
summary := application.ApplicationSummary
ch <- prometheus.MustNewConstMetric(c.appSummaryApdexScore, prometheus.GaugeValue, summary.ApdexScore, appName)
ch <- prometheus.MustNewConstMetric(c.appSummaryErrorRate, prometheus.GaugeValue, summary.ErrorRate, appName)
ch <- prometheus.MustNewConstMetric(c.appSummaryResponseTime, prometheus.GaugeValue, summary.ResponseTime, appName)
ch <- prometheus.MustNewConstMetric(c.appSummaryThroughput, prometheus.GaugeValue, summary.Throughput, appName)
} else {
log.Warnf("Ignoring application %s because its InstanceCount is 0.", appName)
}
}

func (c *newRelicCollector) collectInstanceSummary(ch chan<- prometheus.Metric,
appName string, instances []newrelic.ApplicationInstance) {
for _, instance := range instances {
Expand All @@ -105,7 +146,7 @@ func (c *newRelicCollector) collectInstanceSummary(ch chan<- prometheus.Metric,
ch <- prometheus.MustNewConstMetric(c.instanceSummaryResponseTime, prometheus.GaugeValue, summary.ResponseTime, appName, instance.Host)
ch <- prometheus.MustNewConstMetric(c.instanceSummaryThroughput, prometheus.GaugeValue, summary.Throughput, appName, instance.Host)
} else {
log.Warnf("Ignoring instance %s because its InstanceCount is 0.", instance.Host)
log.Warnf("Ignoring application instance %s because its InstanceCount is 0.", instance.Host)
}
}
}

0 comments on commit 6f23d82

Please sign in to comment.