summaryrefslogtreecommitdiff
path: root/service/metrics/config.go
diff options
context:
space:
mode:
authorWolfy-J <[email protected]>2019-06-26 16:18:03 +0300
committerWolfy-J <[email protected]>2019-06-26 16:18:03 +0300
commit73ce97e6fccfc59ab759787891d2cc2f8d3f88a5 (patch)
treebe50996a5f24aa3b2d57bf70cdcbf4109d518554 /service/metrics/config.go
parent603239d5e39c5722e295c73eda552fe28f8378fd (diff)
application specific metrics
Diffstat (limited to 'service/metrics/config.go')
-rw-r--r--service/metrics/config.go104
1 files changed, 98 insertions, 6 deletions
diff --git a/service/metrics/config.go b/service/metrics/config.go
index 799ba2d2..b93d8f50 100644
--- a/service/metrics/config.go
+++ b/service/metrics/config.go
@@ -1,23 +1,115 @@
package metrics
-import "github.com/spiral/roadrunner/service"
+import (
+ "fmt"
+ "github.com/prometheus/client_golang/prometheus"
+ "github.com/spiral/roadrunner/service"
+)
type Config struct {
// Address to listen
Address string
- // Metrics define application specific metrics.
- Metrics map[string]Metric
+ // Collect define application specific metrics.
+ Collect map[string]Metric
}
// Metric describes single application specific metric.
type Metric struct {
- Type string
- Description string
- Labels []string
+ // Namespace of the metric.
+ Namespace string
+
+ // Subsystem of the metric.
+ Subsystem string
+
+ // Collector type (histogram, gauge, counter, summary).
+ Type string
+
+ // Help of collector.
+ Help string
+
+ // Labels for vectorized metrics.
+ Labels []string
+
+ // Buckets for histogram metric.
+ Buckets []float64
}
// Hydrate configuration.
func (c *Config) Hydrate(cfg service.Config) error {
return cfg.Unmarshal(c)
}
+
+// register application specific metrics.
+func (c *Config) registerMetrics() error {
+ if c.Collect == nil {
+ return nil
+ }
+
+ for name, m := range c.Collect {
+ var collector prometheus.Collector
+ switch m.Type {
+ case "histogram":
+ opts := prometheus.HistogramOpts{
+ Name: name,
+ Namespace: m.Namespace,
+ Subsystem: m.Subsystem,
+ Help: m.Help,
+ Buckets: m.Buckets,
+ }
+
+ if len(m.Labels) != 0 {
+ collector = prometheus.NewHistogramVec(opts, m.Labels)
+ } else {
+ collector = prometheus.NewHistogram(opts)
+ }
+ case "gauge":
+ opts := prometheus.GaugeOpts{
+ Name: name,
+ Namespace: m.Namespace,
+ Subsystem: m.Subsystem,
+ Help: m.Help,
+ }
+
+ if len(m.Labels) != 0 {
+ collector = prometheus.NewGaugeVec(opts, m.Labels)
+ } else {
+ collector = prometheus.NewGauge(opts)
+ }
+ case "counter":
+ opts := prometheus.CounterOpts{
+ Name: name,
+ Namespace: m.Namespace,
+ Subsystem: m.Subsystem,
+ Help: m.Help,
+ }
+
+ if len(m.Labels) != 0 {
+ collector = prometheus.NewCounterVec(opts, m.Labels)
+ } else {
+ collector = prometheus.NewCounter(opts)
+ }
+ case "summary":
+ opts := prometheus.SummaryOpts{
+ Name: name,
+ Namespace: m.Namespace,
+ Subsystem: m.Subsystem,
+ Help: m.Help,
+ }
+
+ if len(m.Labels) != 0 {
+ collector = prometheus.NewSummaryVec(opts, m.Labels)
+ } else {
+ collector = prometheus.NewSummary(opts)
+ }
+ default:
+ return fmt.Errorf("invalid metric type %s", m.Type)
+ }
+
+ if err := prometheus.Register(collector); err != nil {
+ return err
+ }
+ }
+
+ return nil
+}