From b7ca0c2f8ab524210e52cece6ae5b33697490dd4 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Tue, 28 Jun 2016 17:38:28 -0400 Subject: [PATCH] Global prefix feature --- changelog.md | 9 +++++++++ examples/basic_example.go | 10 +++++++--- librato.go | 40 +++++++++++++++++++++++++-------------- 3 files changed, 42 insertions(+), 17 deletions(-) diff --git a/changelog.md b/changelog.md index 837fc6f..d10f251 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,14 @@ ## Changelog +#### v2.0.2 + +* Added global prefix feature +* Minor improvements + +#### v2.0.1 + +* EK package updated to latest version + #### v2.0.0 * Added usage examples diff --git a/examples/basic_example.go b/examples/basic_example.go index 1c4a56e..2511163 100644 --- a/examples/basic_example.go +++ b/examples/basic_example.go @@ -17,19 +17,23 @@ func main() { librato.Mail = "mail@domain.com" librato.Token = "abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234" + // We use prefix "example:" which will be added to each gauge and counter sended + // to Librato + librato.Prefix = "example:" + for { errs := librato.AddMetric( librato.Gauge{ - Name: "example:gauge_1", + Name: "gauge_1", Value: rand.Int(1000), }, librato.Gauge{ - Name: "example:gauge_2", + Name: "gauge_2", Value: float64(rand.Int(1000)) / float64(rand.Int(20)), Source: "go_librato_example", }, librato.Counter{ - Name: "example:counter_1", + Name: "counter_1", Value: rand.Int(1000), }, ) diff --git a/librato.go b/librato.go index bdf6a1c..1cb41b0 100644 --- a/librato.go +++ b/librato.go @@ -23,7 +23,7 @@ import ( // ////////////////////////////////////////////////////////////////////////////////// // // VERSION contains current version of librato package and used as part of User-Agent -const VERSION = "2.0.1" +const VERSION = "2.0.2" // ////////////////////////////////////////////////////////////////////////////////// // @@ -224,6 +224,9 @@ var ( Token = "" ) +// Global prefix which will be used for all metrics +var Prefix = "" + // APIEndpoint contians URL of Librato API endpoint var APIEndpoint = "https://metrics-api.librato.com" @@ -282,7 +285,7 @@ func NewCollector(period time.Duration, collectFunc func() []Measurement) *Colle // AddMetric synchronously send metric to librato func AddMetric(m ...Measurement) []error { - data := &measurements{} + data := measurements{} var errs []error @@ -308,6 +311,8 @@ func AddMetric(m ...Measurement) []error { } } + appendGlobalPrefix(data) + return execRequest(req.POST, APIEndpoint+"/v1/metrics/", data) } @@ -388,6 +393,8 @@ func (mt *Metrics) Send() []error { mt.queue = make([]Measurement, 0) + appendGlobalPrefix(data) + errs := execRequest(req.POST, APIEndpoint+"/v1/metrics/", data) mt.execErrorHandler(errs) @@ -500,10 +507,8 @@ func sendingLoop() { // convertMeasurementSlice convert slice with measurements to struct // with counters and gauges slices -func convertMeasurementSlice(queue []Measurement) *measurements { - result := &measurements{} - - now := time.Now().Unix() +func convertMeasurementSlice(queue []Measurement) measurements { + result := measurements{} for _, m := range queue { switch m.(type) { @@ -514,10 +519,6 @@ func convertMeasurementSlice(queue []Measurement) *measurements { gauge := m.(Gauge) - if gauge.MeasureTime != 0 { - gauge.MeasureTime = now - } - result.Gauges = append(result.Gauges, gauge) case Counter: @@ -527,10 +528,6 @@ func convertMeasurementSlice(queue []Measurement) *measurements { counter := m.(Counter) - if counter.MeasureTime != 0 { - counter.MeasureTime = now - } - result.Counters = append(result.Counters, counter) } } @@ -538,6 +535,21 @@ func convertMeasurementSlice(queue []Measurement) *measurements { return result } +// appendGlobalPrefix append global prefix to each measurement +func appendGlobalPrefix(data measurements) { + if Prefix == "" { + return + } + + for _, c := range data.Counters { + c.Name = Prefix + c.Name + } + + for _, g := range data.Gauges { + g.Name = Prefix + g.Name + } +} + // execRequest create and execute request to API func execRequest(method, url string, data interface{}) []error { request := req.Request{