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 #1 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 1.1
  • Loading branch information
andyone committed Dec 23, 2015
2 parents aa06ff7 + 0d5e5a2 commit 9707ea5
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 9 deletions.
10 changes: 10 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## Changelog

#### v1.1

* Added `Collector` for collecting metrics for some period
* Some minor improvements

#### v1

Initial public release
80 changes: 71 additions & 9 deletions librato.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import (

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

const VERSION = "1.0"
// VERSION contains current version of librato package and used as part of User-Agent
const VERSION = "1.1"

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

Expand Down Expand Up @@ -54,6 +55,16 @@ type Metrics struct {
ErrorHandler func(errs []error)
}

// Collector struct
type Collector struct {
period time.Duration
lastSendingDate int64
collectFunc func() []Measurement

// Function executed if we have errors while sending data to Librato
ErrorHandler func(errs []error)
}

// Gauge struct
type Gauge struct {

Expand Down Expand Up @@ -251,6 +262,24 @@ func NewMetrics(period time.Duration, maxQueueSize int) (*Metrics, error) {
return metrics, nil
}

// NewCollector create new metrics struct for async metrics collecting and sending
func NewCollector(period time.Duration, collectFunc func() []Measurement) *Collector {
collector := &Collector{
period: period,
lastSendingDate: -1,
collectFunc: collectFunc,
}

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

sources = append(sources, collector)

return collector
}

// AddMetric synchronously send metric to librato
func AddMetric(m Measurement) []error {
err := m.Validate()
Expand Down Expand Up @@ -343,7 +372,30 @@ func (mt *Metrics) Send() []error {

mt.lastSendingDate = time.Now().Unix()

return mt.sendData()
data := convertMeasurementSlice(mt.queue)

mt.queue = make([]Measurement, 0)

return execRequest(req.POST, APIEndpoint+"/v1/metrics/", data)
}

// Send sends metrics data to Librato service
func (cl *Collector) Send() []error {
if Mail == "" || Token == "" {
return []error{errors.New("Access credentials is not set")}
}

ms := cl.collectFunc()

if ms == nil || len(ms) == 0 {
return []error{}
}

cl.lastSendingDate = time.Now().Unix()

data := convertMeasurementSlice(ms)

return execRequest(req.POST, APIEndpoint+"/v1/metrics/", data)
}

// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down Expand Up @@ -379,13 +431,23 @@ func (mt *Metrics) execErrorHandler(errs []error) {
mt.ErrorHandler(errs)
}

// sendData send json encoded metrics data to Librato service
func (mt *Metrics) sendData() []error {
data := convertQueue(mt.queue)
// getPeriod return sending period
func (cl *Collector) getPeriod() time.Duration {
return cl.period
}

mt.queue = make([]Measurement, 0)
// getLastSendingDate return last sending date
func (cl *Collector) getLastSendingDate() int64 {
return cl.lastSendingDate
}

return execRequest(req.POST, APIEndpoint+"/v1/metrics/", data)
// execErrorHandler exec error handler if present
func (cl *Collector) execErrorHandler(errs []error) {
if cl.ErrorHandler == nil {
return
}

cl.ErrorHandler(errs)
}

// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down Expand Up @@ -428,9 +490,9 @@ func sendingLoop() {
}
}

// convertQueue convert slice with measurements to struct
// convertMeasurementSlice convert slice with measurements to struct
// with counters and gauges slices
func convertQueue(queue []Measurement) *mesData {
func convertMeasurementSlice(queue []Measurement) *mesData {
result := &mesData{}

now := time.Now().Unix()
Expand Down

0 comments on commit 9707ea5

Please sign in to comment.