Skip to content

Commit

Permalink
[#2]: feat(metrics): add more http metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
rustatian authored Jan 26, 2022
2 parents 265ec1e + 10b2f88 commit 933462b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
24 changes: 24 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Reason for This PR

`[Author TODO: add issue # or explain reasoning.]`

## Description of Changes

`[Author TODO: add description of changes.]`

## License Acceptance

By submitting this pull request, I confirm that my contribution is made under
the terms of the MIT license.

## PR Checklist

`[Author TODO: Meet these criteria.]`
`[Reviewer TODO: Verify that these criteria are met. Request changes if not]`

- [ ] All commits in this PR are signed (`git commit -s`).
- [ ] The reason for this PR is clearly provided (issue no. or explanation).
- [ ] The description of changes is clear and encompassing.
- [ ] Any required documentation changes (code and docs) are included in this PR.
- [ ] Any user-facing changes are mentioned in `CHANGELOG.md`.
- [ ] All added/changed functionality is tested.
27 changes: 21 additions & 6 deletions metrics.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package http

import (
"strconv"

"github.com/prometheus/client_golang/prometheus"
"github.com/roadrunner-server/api/v2/plugins/informer"
)
Expand All @@ -10,20 +12,29 @@ func (p *Plugin) MetricsCollector() []prometheus.Collector {
}

type statsExporter struct {
desc *prometheus.Desc
workers informer.Informer
totalMemoryDesc *prometheus.Desc
stateDesc *prometheus.Desc
workerMemoryDesc *prometheus.Desc
totalWorkersDesc *prometheus.Desc
workers informer.Informer
}

func newWorkersExporter(stats informer.Informer) *statsExporter {
return &statsExporter{
desc: prometheus.NewDesc("rr_http_workers_memory_bytes", "Memory usage by HTTP workers.", nil, nil),
workers: stats,
totalWorkersDesc: prometheus.NewDesc("rr_http_total_workers", "Total number of workers used by the HTTP plugin", nil, nil),
totalMemoryDesc: prometheus.NewDesc("rr_http_workers_memory_bytes", "Memory usage by HTTP workers.", nil, nil),
stateDesc: prometheus.NewDesc("rr_http_worker_state", "Worker current state", []string{"state", "pid"}, nil),
workerMemoryDesc: prometheus.NewDesc("rr_http_worker_memory_bytes", "Worker current memory usage", []string{"pid"}, nil),
workers: stats,
}
}

func (s *statsExporter) Describe(d chan<- *prometheus.Desc) {
// send description
d <- s.desc
d <- s.totalWorkersDesc
d <- s.totalMemoryDesc
d <- s.stateDesc
d <- s.workerMemoryDesc
}

func (s *statsExporter) Collect(ch chan<- prometheus.Metric) {
Expand All @@ -36,8 +47,12 @@ func (s *statsExporter) Collect(ch chan<- prometheus.Metric) {
// collect the memory
for i := 0; i < len(workers); i++ {
cum += workers[i].MemoryUsage

ch <- prometheus.MustNewConstMetric(s.stateDesc, prometheus.GaugeValue, 0, workers[i].Status, strconv.Itoa(workers[i].Pid))
ch <- prometheus.MustNewConstMetric(s.workerMemoryDesc, prometheus.GaugeValue, float64(workers[i].MemoryUsage), strconv.Itoa(workers[i].Pid))
}

// send the values to the prometheus
ch <- prometheus.MustNewConstMetric(s.desc, prometheus.GaugeValue, float64(cum))
ch <- prometheus.MustNewConstMetric(s.totalWorkersDesc, prometheus.GaugeValue, float64(len(workers)))
ch <- prometheus.MustNewConstMetric(s.totalMemoryDesc, prometheus.GaugeValue, float64(cum))
}

0 comments on commit 933462b

Please sign in to comment.