Skip to content
This repository has been archived by the owner on May 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #20 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 4.0.0
  • Loading branch information
andyone authored Nov 16, 2016
2 parents 60299ee + 4096e17 commit e7cb623
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/annotations_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
"time"

"pkg.re/essentialkaos/librato.v3"
"pkg.re/essentialkaos/librato.v4"
)

// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down
2 changes: 1 addition & 1 deletion examples/async_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"pkg.re/essentialkaos/ek.v5/rand"

"pkg.re/essentialkaos/librato.v3"
"pkg.re/essentialkaos/librato.v4"
)

// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down
2 changes: 1 addition & 1 deletion examples/basic_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"pkg.re/essentialkaos/ek.v5/rand"

"pkg.re/essentialkaos/librato.v3"
"pkg.re/essentialkaos/librato.v4"
)

// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down
2 changes: 1 addition & 1 deletion examples/collector_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"pkg.re/essentialkaos/ek.v5/rand"

"pkg.re/essentialkaos/librato.v3"
"pkg.re/essentialkaos/librato.v4"
)

// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down
44 changes: 31 additions & 13 deletions librato.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

// ////////////////////////////////////////////////////////////////////////////////// //

Expand All @@ -49,21 +49,21 @@ 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
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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -277,14 +286,19 @@ func NewCollector(period time.Duration, collectFunc func() []Measurement) *Colle
period: period,
collectFunc: collectFunc,
lastSendingDate: -1,
engine: &req.Engine{},
}

if sources == nil {
sources = make([]DataSource, 0)
go sendingLoop()
}

if UseGlobalEngine {
collector.Engine = Engine
} else {
collector.Engine = &req.Engine{}
}

sources = append(sources, collector)

return collector
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
}

// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit e7cb623

Please sign in to comment.