Skip to content

Commit

Permalink
Collect key transactions metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
diogonicoleti committed Sep 5, 2018
1 parent 6981d41 commit 2dd12c7
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ type newRelicCollector struct {
instanceSummaryErrorRate *prometheus.Desc
instanceSummaryResponseTime *prometheus.Desc
instanceSummaryThroughput *prometheus.Desc
keyTransactionApdexScore *prometheus.Desc
keyTransactionApdexTarget *prometheus.Desc
keyTransactionErrorRate *prometheus.Desc
keyTransactionResponseTime *prometheus.Desc
keyTransactionThroughput *prometheus.Desc
}

// NewNewRelicCollector returns a prometheus collector which exports
Expand Down Expand Up @@ -56,6 +61,11 @@ func NewNewRelicCollector(apiKey string, config config.Config) prometheus.Collec
instanceSummaryErrorRate: newInstanceSummaryDesc("error_rate"),
instanceSummaryResponseTime: newInstanceSummaryDesc("response_time"),
instanceSummaryThroughput: newInstanceSummaryDesc("throughput"),
keyTransactionApdexScore: newKeyTransactionDesc("apdex_score"),
keyTransactionApdexTarget: newKeyTransactionDesc("apdex_target"),
keyTransactionErrorRate: newKeyTransactionDesc("error_rate"),
keyTransactionResponseTime: newKeyTransactionDesc("response_time"),
keyTransactionThroughput: newKeyTransactionDesc("throughput"),
}
}

Expand All @@ -77,6 +87,15 @@ func newInstanceSummaryDesc(name string) *prometheus.Desc {
)
}

func newKeyTransactionDesc(name string) *prometheus.Desc {
return prometheus.NewDesc(
prometheus.BuildFQName(namespace, "key_transaction", name),
"Key transaction last 10 minutes average for "+strings.Replace(name, "_", " ", -1),
[]string{"transaction"},
nil,
)
}

// Describe describes all the metrics exported by the NewRelic exporter.
// It implements prometheus.Collector.
func (c *newRelicCollector) Describe(ch chan<- *prometheus.Desc) {
Expand All @@ -90,6 +109,11 @@ func (c *newRelicCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.instanceSummaryErrorRate
ch <- c.instanceSummaryResponseTime
ch <- c.instanceSummaryThroughput
ch <- c.keyTransactionApdexScore
ch <- c.keyTransactionApdexTarget
ch <- c.keyTransactionErrorRate
ch <- c.keyTransactionResponseTime
ch <- c.keyTransactionThroughput
}

// Collect fetches the metrics data from the NewRelic application and
Expand All @@ -108,6 +132,8 @@ func (c *newRelicCollector) Collect(ch chan<- prometheus.Metric) {
// all goroutines below. Maybe consuming the simplest API endpoint
ch <- prometheus.MustNewConstMetric(c.up, prometheus.GaugeValue, 1)

c.collectKeyTransactions(ch)

for _, app := range c.config.Applications {
go func(app config.Application) {
defer wg.Done()
Expand Down Expand Up @@ -159,3 +185,30 @@ func (c *newRelicCollector) collectInstanceSummary(ch chan<- prometheus.Metric,
}
}
}

func (c *newRelicCollector) collectKeyTransactions(ch chan<- prometheus.Metric) {
log.Infof("Collecting metrics from key transactions")
keyTransactions, err := c.client.ListKeyTransactions()
if err != nil {
log.Errorf("Failed to get key transactions: %v", err)
return
}

for _, transaction := range keyTransactions {
if transaction.Reporting {
summary := transaction.ApplicationSummary
ch <- prometheus.MustNewConstMetric(c.keyTransactionApdexScore,
prometheus.GaugeValue, summary.ApdexScore, transaction.TransactionName)
ch <- prometheus.MustNewConstMetric(c.keyTransactionApdexTarget,
prometheus.GaugeValue, summary.ApdexTarget, transaction.TransactionName)
ch <- prometheus.MustNewConstMetric(c.keyTransactionErrorRate,
prometheus.GaugeValue, summary.ErrorRate, transaction.TransactionName)
ch <- prometheus.MustNewConstMetric(c.keyTransactionResponseTime,
prometheus.GaugeValue, summary.ResponseTime, transaction.TransactionName)
ch <- prometheus.MustNewConstMetric(c.keyTransactionThroughput,
prometheus.GaugeValue, summary.Throughput, transaction.TransactionName)
} else {
log.Warnf("Ignoring key transaction '%s' because it is not reporting.", transaction.TransactionName)
}
}
}

0 comments on commit 2dd12c7

Please sign in to comment.