Skip to content

Commit

Permalink
getting custom metrics on prometheus
Browse files Browse the repository at this point in the history
  • Loading branch information
henrod committed Nov 3, 2018
1 parent 9c95da7 commit ccc1f0e
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ benchmark-test-grpc: ensure-e2e-deps-grpc ensure-testing-bin

unit-test-coverage: kill-testing-deps
@echo "===============RUNNING UNIT TESTS==============="
@go test $(TESTABLE_PACKAGES) -coverprofile coverprofile.out
@go test $(TESTABLE_PACKAGES) -coverprofile coverprofile.out -v

test: kill-testing-deps test-coverage
@make rm-test-temp-files
Expand Down
20 changes: 9 additions & 11 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,21 +142,19 @@ func Configure(

func configureMetrics(serverType string) {
app.metricsReporters = make([]metrics.Reporter, 0)

constTags := app.config.GetStringMapString("pitaya.metrics.constTags")

if app.config.GetBool("pitaya.metrics.prometheus.enabled") {
port := app.config.GetInt("pitaya.metrics.prometheus.port")
game := app.config.GetString("pitaya.game")
logger.Log.Infof(
"prometheus is enabled, configuring the metrics reporter on port %d", port,
)

additionalTags := app.config.GetStringMapString("pitaya.metrics.additionalTags")
prometheus := metrics.GetPrometheusReporter(serverType, game, port,
constTags, additionalTags)
AddMetricsReporter(prometheus)
logger.Log.Infof("prometheus is enabled, configuring reporter on port %d", port)
prometheus, err := metrics.GetPrometheusReporter(serverType, app.config, constTags)
if err != nil {
logger.Log.Errorf("failed to start prometheus metrics reporter, skipping %v", err)
} else {
AddMetricsReporter(prometheus)
}
} else {
logger.Log.Info("prometheus is disabled, the metrics reporter will not be enabled")
logger.Log.Info("prometheus is disabled, reporter will not be enabled")
}

if app.config.GetBool("pitaya.metrics.statsd.enabled") {
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func (c *Config) fillDefaultValues() {
"pitaya.metrics.constTags": map[string]string{},
"pitaya.metrics.additionalTags": map[string]string{},
"pitaya.metrics.periodicMetrics.period": "15s",
"pitaya.metrics.custom": map[string]interface{}{},
"pitaya.defaultpipelines.structvalidation.enabled": false,
"pitaya.worker.redis.url": "localhost:6379",
"pitaya.worker.redis.pool": "10",
Expand Down
55 changes: 55 additions & 0 deletions metrics/models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package metrics

import (
"encoding/json"

"github.com/topfreegames/pitaya/config"
)

// Summary ...
type Summary struct {
Subsystem string
Name string
Help string
Objectives map[float64]float64
Labels []string
}

// Gauge ...
type Gauge struct {
Subsystem string
Name string
Help string
Labels []string
}

// Counter ...
type Counter struct {
Subsystem string
Name string
Help string
Labels []string
}

// CustomMetricsSpec ...
type CustomMetricsSpec struct {
Summaries []*Summary
Gauges []*Gauge
Counters []*Counter
}

// NewCustomMetricsSpec ...
func NewCustomMetricsSpec(config *config.Config) (*CustomMetricsSpec, error) {
bts, err := json.Marshal(config.Get("pitaya.metrics.custom"))
if err != nil {
return nil, err
}

var spec CustomMetricsSpec
err = json.Unmarshal(bts, &spec)
if err != nil {
return nil, err
}

return &spec, nil
}
73 changes: 67 additions & 6 deletions metrics/prometheus_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"sync"

"github.com/prometheus/client_golang/prometheus"
"github.com/topfreegames/pitaya/config"
"github.com/topfreegames/pitaya/constants"
)

Expand All @@ -45,9 +46,57 @@ type PrometheusReporter struct {
additionalLabels map[string]string
}

func (p *PrometheusReporter) registerCustomMetrics(
constLabels map[string]string,
spec *CustomMetricsSpec,
) {
for _, summary := range spec.Summaries {
p.summaryReportersMap[summary.Name] = prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Namespace: "pitaya",
Subsystem: summary.Subsystem,
Name: summary.Name,
Help: summary.Help,
Objectives: summary.Objectives,
ConstLabels: constLabels,
},
summary.Labels,
)
}

for _, gauge := range spec.Gauges {
p.gaugeReportersMap[gauge.Name] = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "pitaya",
Subsystem: gauge.Subsystem,
Name: gauge.Name,
Help: gauge.Help,
ConstLabels: constLabels,
},
gauge.Labels,
)
}

for _, counter := range spec.Counters {
p.countReportersMap[counter.Name] = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "pitaya",
Subsystem: counter.Subsystem,
Name: counter.Name,
Help: counter.Help,
ConstLabels: constLabels,
},
counter.Labels,
)
}
}

func (p *PrometheusReporter) registerMetrics(
constLabels, additionalLabels map[string]string,
spec *CustomMetricsSpec,
) {
p.registerCustomMetrics(constLabels, spec)

constLabels["game"] = p.game
constLabels["serverType"] = p.serverType

Expand Down Expand Up @@ -212,10 +261,21 @@ func (p *PrometheusReporter) registerMetrics(

// GetPrometheusReporter gets the prometheus reporter singleton
func GetPrometheusReporter(
serverType, game string,
port int,
constLabels, additionalLabels map[string]string,
) *PrometheusReporter {
serverType string,
config *config.Config,
constLabels map[string]string,
) (*PrometheusReporter, error) {
var (
port = config.GetInt("pitaya.metrics.prometheus.port")
game = config.GetString("pitaya.game")
additionalLabels = config.GetStringMapString("pitaya.metrics.additionalTags")
)

spec, err := NewCustomMetricsSpec(config)
if err != nil {
return nil, err
}

once.Do(func() {
prometheusReporter = &PrometheusReporter{
serverType: serverType,
Expand All @@ -224,13 +284,14 @@ func GetPrometheusReporter(
summaryReportersMap: make(map[string]*prometheus.SummaryVec),
gaugeReportersMap: make(map[string]*prometheus.GaugeVec),
}
prometheusReporter.registerMetrics(constLabels, additionalLabels)
prometheusReporter.registerMetrics(constLabels, additionalLabels, spec)
http.Handle("/metrics", prometheus.Handler())
go (func() {
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
})()
})
return prometheusReporter

return prometheusReporter, nil
}

// ReportSummary reports a summary metric
Expand Down

0 comments on commit ccc1f0e

Please sign in to comment.