Created
April 1, 2017 14:53
-
-
Save ru-rocker/e443d8a1bdc07b54b7a7abbc868e69e7 to your computer and use it in GitHub Desktop.
metrics middleware
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
package lorem_metrics | |
import ( | |
"github.com/go-kit/kit/metrics" | |
"time" | |
) | |
func Metrics(requestCount metrics.Counter, | |
requestLatency metrics.Histogram) ServiceMiddleware { | |
return func(next Service) Service { | |
return metricsMiddleware{ | |
next, | |
requestCount, | |
requestLatency, | |
} | |
} | |
} | |
// Make a new type and wrap into Service interface | |
// Add expected metrics property to this type | |
type metricsMiddleware struct { | |
Service | |
requestCount metrics.Counter | |
requestLatency metrics.Histogram | |
} | |
// Implement service functions and add label method for our metrics | |
func (mw metricsMiddleware) Word(min, max int) (output string) { | |
defer func(begin time.Time) { | |
lvs := []string{"method", "Word"} | |
mw.requestCount.With(lvs...).Add(1) | |
mw.requestLatency.With(lvs...).Observe(time.Since(begin).Seconds()) | |
}(time.Now()) | |
output = mw.Service.Word(min, max) | |
return | |
} | |
func (mw metricsMiddleware) Sentence(min, max int) (output string) { | |
defer func(begin time.Time) { | |
lvs := []string{"method", "Sentence"} | |
mw.requestCount.With(lvs...).Add(1) | |
mw.requestLatency.With(lvs...).Observe(time.Since(begin).Seconds()) | |
}(time.Now()) | |
output = mw.Service.Sentence(min, max) | |
return | |
} | |
func (mw metricsMiddleware) Paragraph(min, max int) (output string) { | |
defer func(begin time.Time) { | |
lvs := []string{"method", "Paragraph"} | |
mw.requestCount.With(lvs...).Add(1) | |
mw.requestLatency.With(lvs...).Observe(time.Since(begin).Seconds()) | |
}(time.Now()) | |
output = mw.Service.Paragraph(min, max) | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment