Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

report sys metrics #53

Merged
merged 2 commits into from
Sep 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,11 @@ func initSysRemotes() {
)
}

func periodicMetrics() {
period := app.config.GetDuration("pitaya.metrics.periodicMetrics.period")
go metrics.ReportSysMetrics(app.metricsReporters, period)
}

// Start starts the app
func Start() {
if !app.configured {
Expand Down Expand Up @@ -396,6 +401,8 @@ func Start() {
app.metricsReporters,
)

periodicMetrics()

listen()

defer func() {
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func (c *Config) fillDefaultValues() {
"pitaya.metrics.prometheus.port": 9090,
"pitaya.metrics.prometheus.enabled": false,
"pitaya.metrics.tags": map[string]string{},
"pitaya.metrics.periodicMetrics.period": "15s",
"pitaya.defaultpipelines.structvalidation.enabled": false,
}

Expand Down
4 changes: 4 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ Metrics Reporting
- map[string]string{}
- map[string]string
- Tags to be added to reported metrics
* - pitaya.metrics.periodicMetrics.period
- 15s
- string
- Period that system metrics will be reported

Concurrency
===========
Expand Down
6 changes: 6 additions & 0 deletions metrics/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,10 @@ var (
DroppedMessages = "dropped_messages"
// ProcessDelay reports the message processing delay to handle the messages at the handler service
ProcessDelay = "handler_delay_ns"
// Goroutines reports the number of goroutines
Goroutines = "goroutines"
// HeapSize reports the size of heap
HeapSize = "heapsize"
// HeapObjects reports the number of allocated heap objects
HeapObjects = "heapobjects"
)
33 changes: 33 additions & 0 deletions metrics/prometheus_reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,39 @@ func (p *PrometheusReporter) registerMetrics(constLabels map[string]string) {
[]string{},
)

p.gaugeReportersMap[Goroutines] = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "pitaya",
Subsystem: "sys",
Name: Goroutines,
Help: "the current number of goroutines",
ConstLabels: constLabels,
},
[]string{},
)

p.gaugeReportersMap[HeapSize] = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "pitaya",
Subsystem: "sys",
Name: HeapSize,
Help: "the current heap size",
ConstLabels: constLabels,
},
[]string{},
)

p.gaugeReportersMap[HeapObjects] = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "pitaya",
Subsystem: "sys",
Name: HeapObjects,
Help: "the current number of allocated heap objects",
ConstLabels: constLabels,
},
[]string{},
)

toRegister := make([]prometheus.Collector, 0)
for _, c := range p.countReportersMap {
toRegister = append(toRegister, c)
Expand Down
18 changes: 18 additions & 0 deletions metrics/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package metrics

import (
"context"
"runtime"
"time"

"github.com/topfreegames/pitaya/constants"
Expand Down Expand Up @@ -71,3 +72,20 @@ func ReportNumberOfConnectedClients(reporters []Reporter, number int64) {
r.ReportGauge(ConnectedClients, map[string]string{}, float64(number))
}
}

// ReportSysMetrics reports sys metrics
func ReportSysMetrics(reporters []Reporter, period time.Duration) {
for {
for _, r := range reporters {
num := runtime.NumGoroutine()
m := &runtime.MemStats{}
runtime.ReadMemStats(m)

r.ReportGauge(Goroutines, map[string]string{}, float64(num))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error return value of r.ReportGauge is not checked

r.ReportGauge(HeapSize, map[string]string{}, float64(m.Alloc))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error return value of r.ReportGauge is not checked

r.ReportGauge(HeapObjects, map[string]string{}, float64(m.HeapObjects))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error return value of r.ReportGauge is not checked

}

time.Sleep(period)
}
}
2 changes: 1 addition & 1 deletion pitaya-protos
Submodule pitaya-protos updated 1 files
+0 −2 pitaya.proto