-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add handler metrics to bus and saga (#101)
* add handler metrics to bus and saga + tests * fix build * add 0 to the default buckets to catch fast message handling * PR correction - changed latency to summary(removed bucket configuration), add registration for saga handlers * PR correction - getting logger as a param * PR correction - new line in eof * PR corrections message handler + sync.map + latency as summary * add rejected messages metric
- Loading branch information
1 parent
4ab2be5
commit f617e04
Showing
13 changed files
with
497 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package gbus | ||
|
||
import ( | ||
"reflect" | ||
"runtime" | ||
"strings" | ||
) | ||
|
||
//MessageHandler signature for all command handlers | ||
type MessageHandler func(invocation Invocation, message *BusMessage) error | ||
|
||
func (mg MessageHandler) Name() string { | ||
funName := runtime.FuncForPC(reflect.ValueOf(mg).Pointer()).Name() | ||
splits := strings.Split(funName, ".") | ||
fn := strings.Replace(splits[len(splits)-1], "-fm", "", -1) | ||
return fn | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package metrics | ||
|
||
import ( | ||
"fmt" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_model/go" | ||
"github.com/sirupsen/logrus" | ||
"sync" | ||
) | ||
|
||
var ( | ||
handlerMetricsByHandlerName = &sync.Map{} | ||
) | ||
|
||
const ( | ||
failure = "failure" | ||
success = "success" | ||
handlerResult = "result" | ||
handlers = "handlers" | ||
grabbitPrefix = "grabbit" | ||
) | ||
|
||
type HandlerMetrics struct { | ||
result *prometheus.CounterVec | ||
latency prometheus.Summary | ||
} | ||
|
||
func AddHandlerMetrics(handlerName string) { | ||
handlerMetrics := newHandlerMetrics(handlerName) | ||
_, exists := handlerMetricsByHandlerName.LoadOrStore(handlerName, handlerMetrics) | ||
|
||
if !exists { | ||
prometheus.MustRegister(handlerMetrics.latency, handlerMetrics.result) | ||
} | ||
} | ||
|
||
func RunHandlerWithMetric(handleMessage func() error, handlerName string, logger logrus.FieldLogger) error { | ||
handlerMetrics := GetHandlerMetrics(handlerName) | ||
defer func() { | ||
if p := recover(); p != nil { | ||
if handlerMetrics != nil { | ||
handlerMetrics.result.WithLabelValues(failure).Inc() | ||
} | ||
|
||
panic(p) | ||
} | ||
}() | ||
|
||
if handlerMetrics == nil { | ||
logger.WithField("handler", handlerName).Warn("Running with metrics - couldn't find metrics for the given handler") | ||
return handleMessage() | ||
} | ||
|
||
err := trackTime(handleMessage, handlerMetrics.latency) | ||
|
||
if err != nil { | ||
handlerMetrics.result.WithLabelValues(failure).Inc() | ||
} else { | ||
handlerMetrics.result.WithLabelValues(success).Inc() | ||
} | ||
|
||
return err | ||
} | ||
|
||
func GetHandlerMetrics(handlerName string) *HandlerMetrics { | ||
entry, ok := handlerMetricsByHandlerName.Load(handlerName) | ||
if ok { | ||
return entry.(*HandlerMetrics) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func newHandlerMetrics(handlerName string) *HandlerMetrics { | ||
return &HandlerMetrics{ | ||
result: prometheus.NewCounterVec( | ||
prometheus.CounterOpts{ | ||
Namespace: grabbitPrefix, | ||
Subsystem: handlers, | ||
Name: fmt.Sprintf("%s_result", handlerName), | ||
Help: fmt.Sprintf("The %s's result", handlerName), | ||
}, | ||
[]string{handlerResult}), | ||
latency: prometheus.NewSummary( | ||
prometheus.SummaryOpts{ | ||
Namespace: grabbitPrefix, | ||
Subsystem: handlers, | ||
Name: fmt.Sprintf("%s_latency", handlerName), | ||
Help: fmt.Sprintf("The %s's latency", handlerName), | ||
}), | ||
} | ||
} | ||
|
||
func trackTime(functionToTrack func() error, observer prometheus.Observer) error { | ||
timer := prometheus.NewTimer(observer) | ||
defer timer.ObserveDuration() | ||
|
||
return functionToTrack() | ||
} | ||
|
||
func (hm *HandlerMetrics) GetSuccessCount() (float64, error) { | ||
return hm.getLabeledCounterValue(success) | ||
} | ||
|
||
func (hm *HandlerMetrics) GetFailureCount() (float64, error) { | ||
return hm.getLabeledCounterValue(failure) | ||
} | ||
|
||
func (hm *HandlerMetrics) GetLatencySampleCount() (*uint64, error) { | ||
m := &io_prometheus_client.Metric{} | ||
err := hm.latency.Write(m) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return m.GetSummary().SampleCount, nil | ||
} | ||
|
||
func (hm *HandlerMetrics) getLabeledCounterValue(label string) (float64, error) { | ||
m := &io_prometheus_client.Metric{} | ||
err := hm.result.WithLabelValues(label).Write(m) | ||
|
||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return m.GetCounter().GetValue(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package metrics | ||
|
||
import ( | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promauto" | ||
"github.com/prometheus/client_model/go" | ||
) | ||
|
||
var ( | ||
rejectedMessages = newRejectedMessagesCounter() | ||
) | ||
|
||
func ReportRejectedMessage() { | ||
rejectedMessages.Inc() | ||
} | ||
|
||
func GetRejectedMessagesValue() (float64, error) { | ||
m := &io_prometheus_client.Metric{} | ||
err := rejectedMessages.Write(m) | ||
|
||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return m.GetCounter().GetValue(), nil | ||
} | ||
|
||
func newRejectedMessagesCounter() prometheus.Counter { | ||
return promauto.NewCounter(prometheus.CounterOpts{ | ||
Namespace: grabbitPrefix, | ||
Subsystem: "messages", | ||
Name: "rejected_messages", | ||
Help: "counting the rejected messages", | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.