package stats
import (
"fmt"
estats "google.golang.org/grpc/experimental/stats"
"google.golang.org/grpc/internal"
"google.golang.org/grpc/stats"
)
type MetricsRecorderList struct {
internal .EnforceMetricsRecorderEmbedding
metricsRecorders []estats .MetricsRecorder
}
func NewMetricsRecorderList (shs []stats .Handler ) *MetricsRecorderList {
var mrs []estats .MetricsRecorder
for _ , sh := range shs {
if mr , ok := sh .(estats .MetricsRecorder ); ok {
mrs = append (mrs , mr )
}
}
return &MetricsRecorderList {
metricsRecorders : mrs ,
}
}
func verifyLabels(desc *estats .MetricDescriptor , labelsRecv ...string ) {
if got , want := len (labelsRecv ), len (desc .Labels )+len (desc .OptionalLabels ); got != want {
panic (fmt .Sprintf ("Received %d labels in call to record metric %q, but expected %d." , got , desc .Name , want ))
}
}
func (l *MetricsRecorderList ) RecordInt64Count (handle *estats .Int64CountHandle , incr int64 , labels ...string ) {
verifyLabels (handle .Descriptor (), labels ...)
for _ , metricRecorder := range l .metricsRecorders {
metricRecorder .RecordInt64Count (handle , incr , labels ...)
}
}
func (l *MetricsRecorderList ) RecordInt64UpDownCount (handle *estats .Int64UpDownCountHandle , incr int64 , labels ...string ) {
verifyLabels (handle .Descriptor (), labels ...)
for _ , metricRecorder := range l .metricsRecorders {
metricRecorder .RecordInt64UpDownCount (handle , incr , labels ...)
}
}
func (l *MetricsRecorderList ) RecordFloat64Count (handle *estats .Float64CountHandle , incr float64 , labels ...string ) {
verifyLabels (handle .Descriptor (), labels ...)
for _ , metricRecorder := range l .metricsRecorders {
metricRecorder .RecordFloat64Count (handle , incr , labels ...)
}
}
func (l *MetricsRecorderList ) RecordInt64Histo (handle *estats .Int64HistoHandle , incr int64 , labels ...string ) {
verifyLabels (handle .Descriptor (), labels ...)
for _ , metricRecorder := range l .metricsRecorders {
metricRecorder .RecordInt64Histo (handle , incr , labels ...)
}
}
func (l *MetricsRecorderList ) RecordFloat64Histo (handle *estats .Float64HistoHandle , incr float64 , labels ...string ) {
verifyLabels (handle .Descriptor (), labels ...)
for _ , metricRecorder := range l .metricsRecorders {
metricRecorder .RecordFloat64Histo (handle , incr , labels ...)
}
}
func (l *MetricsRecorderList ) RecordInt64Gauge (handle *estats .Int64GaugeHandle , incr int64 , labels ...string ) {
verifyLabels (handle .Descriptor (), labels ...)
for _ , metricRecorder := range l .metricsRecorders {
metricRecorder .RecordInt64Gauge (handle , incr , labels ...)
}
}
func (l *MetricsRecorderList ) RegisterAsyncReporter (reporter estats .AsyncMetricReporter , metrics ...estats .AsyncMetric ) func () {
descriptorsMap := make (map [*estats .MetricDescriptor ]bool , len (metrics ))
for _ , m := range metrics {
descriptorsMap [m .Descriptor ()] = true
}
unregisterFns := make ([]func (), 0 , len (l .metricsRecorders ))
for _ , mr := range l .metricsRecorders {
wrappedCallback := func (recorder estats .AsyncMetricsRecorder ) error {
wrappedRecorder := &asyncRecorderWrapper {
delegate : recorder ,
descriptors : descriptorsMap ,
}
return reporter .Report (wrappedRecorder )
}
unregisterFns = append (unregisterFns , mr .RegisterAsyncReporter (estats .AsyncMetricReporterFunc (wrappedCallback ), metrics ...))
}
return internal .AsyncReporterCleanupDelegate (defaultCleanUp (unregisterFns ))
}
func defaultCleanUp(unregisterFns []func ()) func () {
return func () {
for _ , unregister := range unregisterFns {
unregister ()
}
}
}
type asyncRecorderWrapper struct {
delegate estats .AsyncMetricsRecorder
descriptors map [*estats .MetricDescriptor ]bool
}
func (w *asyncRecorderWrapper ) RecordInt64AsyncGauge (handle *estats .Int64AsyncGaugeHandle , value int64 , labels ...string ) {
d := handle .Descriptor ()
if _ , ok := w .descriptors [d ]; !ok {
return
}
verifyLabels (d , labels ...)
w .delegate .RecordInt64AsyncGauge (handle , value , labels ...)
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .