From 6d24155149d9df2469db212026781fc95ca486c1 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Tue, 15 Nov 2016 10:54:47 -0500 Subject: [PATCH 1/3] Custom engine support --- librato.go | 44 +++++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/librato.go b/librato.go index 2e20cab..bf1e842 100644 --- a/librato.go +++ b/librato.go @@ -22,7 +22,7 @@ import ( // ////////////////////////////////////////////////////////////////////////////////// // // VERSION contains current version of librato package and used as part of User-Agent -const VERSION = "3.0.1" +const VERSION = "4.0.0" // ////////////////////////////////////////////////////////////////////////////////// // @@ -49,10 +49,10 @@ type Metrics struct { lastSendingDate int64 initialized bool queue []Measurement - engine *req.Engine // Function executed if we have errors while sending data to Librato ErrorHandler func(errs []error) + Engine *req.Engine } // Collector struct @@ -60,10 +60,10 @@ type Collector struct { period time.Duration lastSendingDate int64 collectFunc func() []Measurement - engine *req.Engine // Function executed if we have errors while sending data to Librato ErrorHandler func(errs []error) + Engine *req.Engine } // Gauge struct @@ -228,6 +228,12 @@ var ( // APIEndpoint contians URL of Librato API endpoint var APIEndpoint = "https://metrics-api.librato.com" +// Engine is global req.Engine which used for sync requests +var Engine = &req.Engine{} + +// UseGlobalEngine set to true for using global engine for all requests +var UseGlobalEngine = false + // ////////////////////////////////////////////////////////////////////////////////// // // List of sources @@ -237,11 +243,9 @@ var sources []DataSource var ( errAccessCredentials = []error{errors.New("Access credentials is not set")} errEmptyStreamName = []error{errors.New("Stream name can't be empty")} + errEngineIsNil = []error{errors.New("Engine is nil")} ) -// Request engine -var engine = &req.Engine{} - // ////////////////////////////////////////////////////////////////////////////////// // // NewMetrics create new metrics struct for async metrics sending @@ -252,7 +256,12 @@ func NewMetrics(period time.Duration, maxQueueSize int) (*Metrics, error) { initialized: true, queue: make([]Measurement, 0), lastSendingDate: -1, - engine: &req.Engine{}, + } + + if UseGlobalEngine { + metrics.Engine = Engine + } else { + metrics.Engine = &req.Engine{} } err := validateMetrics(metrics) @@ -277,7 +286,6 @@ func NewCollector(period time.Duration, collectFunc func() []Measurement) *Colle period: period, collectFunc: collectFunc, lastSendingDate: -1, - engine: &req.Engine{}, } if sources == nil { @@ -285,6 +293,12 @@ func NewCollector(period time.Duration, collectFunc func() []Measurement) *Colle go sendingLoop() } + if UseGlobalEngine { + collector.Engine = Engine + } else { + collector.Engine = &req.Engine{} + } + sources = append(sources, collector) return collector @@ -318,7 +332,7 @@ func AddMetric(m ...Measurement) []error { } } - return execRequest(engine, req.POST, APIEndpoint+"/v1/metrics/", data) + return execRequest(Engine, req.POST, APIEndpoint+"/v1/metrics/", data) } // AddAnnotation synchronously send annotation to librato @@ -333,7 +347,7 @@ func AddAnnotation(stream string, a Annotation) []error { return []error{err} } - return execRequest(engine, req.POST, APIEndpoint+"/v1/annotations/"+stream, a) + return execRequest(Engine, req.POST, APIEndpoint+"/v1/annotations/"+stream, a) } // DeleteAnnotations synchronously remove annotation stream on librato @@ -342,7 +356,7 @@ func DeleteAnnotations(stream string) []error { return errEmptyStreamName } - return execRequest(engine, req.DELETE, APIEndpoint+"/v1/annotations/"+stream, nil) + return execRequest(Engine, req.DELETE, APIEndpoint+"/v1/annotations/"+stream, nil) } // ////////////////////////////////////////////////////////////////////////////////// // @@ -396,7 +410,7 @@ func (mt *Metrics) Send() []error { mt.queue = make([]Measurement, 0) - errs := execRequest(mt.engine, req.POST, APIEndpoint+"/v1/metrics/", data) + errs := execRequest(mt.Engine, req.POST, APIEndpoint+"/v1/metrics/", data) mt.execErrorHandler(errs) @@ -433,7 +447,7 @@ func (cl *Collector) Send() []error { cl.lastSendingDate = time.Now().Unix() data := convertMeasurementSlice(measurements) - errs = execRequest(cl.engine, req.POST, APIEndpoint+"/v1/metrics/", data) + errs = execRequest(cl.Engine, req.POST, APIEndpoint+"/v1/metrics/", data) cl.execErrorHandler(errs) @@ -549,6 +563,10 @@ func convertMeasurementSlice(data []Measurement) measurements { // execRequest create and execute request to API func execRequest(engine *req.Engine, method, url string, data interface{}) []error { + if engine == nil { + return errEngineIsNil + } + if engine.UserAgent == "" { engine.SetUserAgent("go-ek-librato", VERSION) } From 27828d890588efbb235c32805b3ef9dadef23bd3 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Tue, 15 Nov 2016 10:58:24 -0500 Subject: [PATCH 2/3] Updated TravisCI config and examples --- .travis.yml | 2 +- examples/annotations_example.go | 2 +- examples/async_example.go | 2 +- examples/basic_example.go | 2 +- examples/collector_example.go | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index dc18493..9523a35 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,7 +23,7 @@ matrix: before_install: - go get pkg.re/essentialkaos/ek.v5 - mkdir -p $GOPATH/src/pkg.re/essentialkaos - - ln -sf $GOPATH/src/github.com/essentialkaos/librato $GOPATH/src/pkg.re/essentialkaos/librato.v3 + - ln -sf $GOPATH/src/github.com/essentialkaos/librato $GOPATH/src/pkg.re/essentialkaos/librato.v4 script: - go build examples/annotations_example.go diff --git a/examples/annotations_example.go b/examples/annotations_example.go index c511411..a992d59 100644 --- a/examples/annotations_example.go +++ b/examples/annotations_example.go @@ -6,7 +6,7 @@ import ( "fmt" "time" - "pkg.re/essentialkaos/librato.v3" + "pkg.re/essentialkaos/librato.v4" ) // ////////////////////////////////////////////////////////////////////////////////// // diff --git a/examples/async_example.go b/examples/async_example.go index b33624f..f71ac9d 100644 --- a/examples/async_example.go +++ b/examples/async_example.go @@ -8,7 +8,7 @@ import ( "pkg.re/essentialkaos/ek.v5/rand" - "pkg.re/essentialkaos/librato.v3" + "pkg.re/essentialkaos/librato.v4" ) // ////////////////////////////////////////////////////////////////////////////////// // diff --git a/examples/basic_example.go b/examples/basic_example.go index 7c47dfb..56e386e 100644 --- a/examples/basic_example.go +++ b/examples/basic_example.go @@ -8,7 +8,7 @@ import ( "pkg.re/essentialkaos/ek.v5/rand" - "pkg.re/essentialkaos/librato.v3" + "pkg.re/essentialkaos/librato.v4" ) // ////////////////////////////////////////////////////////////////////////////////// // diff --git a/examples/collector_example.go b/examples/collector_example.go index 9f81517..e03f279 100644 --- a/examples/collector_example.go +++ b/examples/collector_example.go @@ -8,7 +8,7 @@ import ( "pkg.re/essentialkaos/ek.v5/rand" - "pkg.re/essentialkaos/librato.v3" + "pkg.re/essentialkaos/librato.v4" ) // ////////////////////////////////////////////////////////////////////////////////// // From 4096e17dfd3ba674261f35b5421ecc46f09fbaa7 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Tue, 15 Nov 2016 17:11:49 -0500 Subject: [PATCH 3/3] Readme updated --- readme.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/readme.md b/readme.md index e6d9cfc..e5317e7 100644 --- a/readme.md +++ b/readme.md @@ -1,17 +1,17 @@ -# Librato [![GoDoc](https://godoc.org/pkg.re/essentialkaos/librato.v3?status.svg)](https://godoc.org/pkg.re/essentialkaos/librato.v3) [![Build Status](https://travis-ci.org/essentialkaos/librato.svg?branch=master)](https://travis-ci.org/essentialkaos/librato) [![codebeat badge](https://codebeat.co/badges/f82e704d-67a7-4c6f-9e5d-1acf058c937b)](https://codebeat.co/projects/github-com-essentialkaos-librato) +# Librato [![GoDoc](https://godoc.org/pkg.re/essentialkaos/librato.v4?status.svg)](https://godoc.org/pkg.re/essentialkaos/librato.v4) [![Build Status](https://travis-ci.org/essentialkaos/librato.svg?branch=master)](https://travis-ci.org/essentialkaos/librato) [![codebeat badge](https://codebeat.co/badges/f82e704d-67a7-4c6f-9e5d-1acf058c937b)](https://codebeat.co/projects/github-com-essentialkaos-librato) Package for working with [Librato Metrics](https://www.librato.com) API from Go code. ## Installation ```` -go get pkg.re/essentialkaos/librato.v3 +go get pkg.re/essentialkaos/librato.v4 ```` For update to latest stable release, do: ```` -go get -u pkg.re/essentialkaos/librato.v3 +go get -u pkg.re/essentialkaos/librato.v4 ```` ## Examples