Giter Site home page Giter Site logo

graphite-client's Introduction

graphite-client

Build Status GoDoc codecov

Graphite client written in Go, focused in thread-safe and automatic aggregations.

Installation

Use go get or your favourite dependency manager to install it from github:

go get github.com/gguridi/graphite-client

Dependencies

This library doesn't have external dependencies by itself, only for testing purposes we need Gingko and Gomega.

Configuration

The different options to configure the client can be found here.

Protocols

Currently we support two protocols to connect with graphite.

  • TCP: using the NewGraphiteTCP constructor.
  • UDP: using the NewGraphiteUDP constructor.

Simple client

We can initialise a simple client with one of the constructors:

import (
    graphite "github.com/gguridi/graphite-client"
    "fmt"
)

client := graphite.NewGraphiteTCP(graphite.Config{
    Host: "example.com",
    Port: 2003,
})

and then send one metric to graphite:

if _, err:= client.Send("metric.name.count", 55); err != nil {
    fmt.Println("Unable to send metrics to graphite")
}

or if we don't mind if we could send or not the metric to graphite:

client.Send("metric.name.count", 55)

Aggregator

We can use an aggregator to send more than one metric at a time, for systems that collect a lot of data and we don't want to overload graphite with requests.

We can initialise an aggregator from any of the supported clients:

import (
    graphite "github.com/gguridi/graphite-client"
    "fmt"
)

aggregator := graphite.NewGraphiteTCP(graphite.Config{
    Host: "example.com",
    Port: 2003,
}).NewAggregator()

and then send several metric to graphite:

aggregator.AddSum("metric.received.count", 15)
aggregator.AddSum("metric.received.count", 10)
aggregator.AddSum("metric.sent.count", 5)
aggregator.Flush()

This will send two metrics at once, one for metric.received.count with a value of 25, and another one for metric.sent.count with a value of 5.

Metric types

There are several kind of metrics built-in in the system, that can be automatically used with the method of the Aggregator interface.

  • AddSum: Will initialise a metric where the final value sent to graphite will be the addition of all the values passed to the aggregator. So if we call AddSum with a specific metric path and values 5, 10, 15 and then we Flush, we will be sending a final value of 30 to graphite.
  • Increase: Used as an alias of AddSum where the value incremented is always 1. Useful for giving a comprehensive behaviour to the metric.
  • AddAverage: Will initialise a metric where the final value sent to graphite will be the average of all the values passed to the aggregator. So if we call AddAverage with a specific metric path and values 2, 10, 10 and then we Flush, we will be sending a final value of 7.333333 to graphite. The maximum decimals allowed is 6.
  • SetActive/SetInactive: Will initialise a metric where the final value sent to graphite will be 1 (SetActive) or 0 (SetInactive). This way we can send metrics such service status, etc.

Automatic flush

It's possible to configure the aggregator to periodically flush the values to graphite without having to worry about doing it manually. To make it possible we can initialise the aggregator with:

aggregator := graphite.NewGraphiteTCP(graphite.Config{
    Host: "example.com",
    Port: 2003,
}).NewAggregator().Run(30 * time.Second, nil)

The second parameter that method accepts is a chan bool that we can use to stop the loop whenever we want.

stop := make(chan bool)

aggregator := graphite.NewGraphiteTCP(graphite.Config{
    Host: "example.com",
    Port: 2003,
}).NewAggregator().Run(30 * time.Second, stop)

time.Sleep(600)
stop <- true

graphite-client's People

Contributors

gguridi avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.