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 #6 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 2.0.2
  • Loading branch information
andyone authored Jun 28, 2016
2 parents d40fd55 + b7ca0c2 commit 87cb6be
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
9 changes: 9 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 7 additions & 3 deletions examples/basic_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,23 @@ func main() {
librato.Mail = "[email protected]"
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),
},
)
Expand Down
40 changes: 26 additions & 14 deletions librato.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

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

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

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

Expand All @@ -308,6 +311,8 @@ func AddMetric(m ...Measurement) []error {
}
}

appendGlobalPrefix(data)

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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand All @@ -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:
Expand All @@ -527,17 +528,28 @@ func convertMeasurementSlice(queue []Measurement) *measurements {

counter := m.(Counter)

if counter.MeasureTime != 0 {
counter.MeasureTime = now
}

result.Counters = append(result.Counters, counter)
}
}

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{
Expand Down

0 comments on commit 87cb6be

Please sign in to comment.