Giter Site home page Giter Site logo

go-statsd-client's Introduction

go-statsd-client

Build Status GoDoc Go Report Card License

About

A StatsD client (UDP) for Go.

Docs

Viewable online at godoc.org.

Example

Some examples:

import (
    "log"

    "github.com/cactus/go-statsd-client/v5/statsd"
)

func main() {
    // First create a client config. Here is a simple config that sends one
    // stat per packet (for compatibility).
    config := &statsd.ClientConfig{
        Address: "127.0.0.1:8125",
        Prefix: "test-client",
    }

    /*
    // This one is for a client that re-resolves the hostname ever 30 seconds.
    // Useful if the address of a hostname changes frequently. Note that this
    // type of client has some additional locking overhead for safety.
    // As such, leave ResInetval as the zero value (previous exmaple) if you
    // don't specifically need this functionality.
    config := &statsd.ClientConfig{
        Address: "127.0.0.1:8125",
        Prefix: "test-client",
        ResInterval: 30 * time.Second,
    }

    // This one is for a buffered client, which sends multiple stats in one
    // packet, is recommended when your server supports it (better performance).
    config := &statsd.ClientConfig{
        Address: "127.0.0.1:8125",
        Prefix: "test-client",
        UseBuffered: true,
        // interval to force flush buffer. full buffers will flush on their own,
        // but for data not frequently sent, a max threshold is useful
        FlushInterval: 300*time.Millisecond,
    }

    // This one is for a buffered resolving client, which sends multiple stats
    // in one packet (like previous example), as well as re-resolving the
    // hostname every 30 seconds.
    config := &statsd.ClientConfig{
        Address: "127.0.0.1:8125",
        Prefix: "test-client",
        ResInterval: 30 * time.Second,
        UseBuffered: true,
        FlushInterval: 300*time.Millisecond,
    }

    // This one is an example of configuring "Tag" support
    // Supported formats are:
    //   InfixComma
    //   InfixSemicolon
    //   SuffixOctothorpe
    // The default, if not otherwise specified, is SuffixOctothorpe.
    config := &statsd.ClientConfig{
        Address: "127.0.0.1:8125",
        Prefix: "test-client",
        ResInterval: 30 * time.Second,
        TagFormat: statsd.InfixSemicolon,
    }
    */

    // Now create the client
    client, err := statsd.NewClientWithConfig(config)

    // and handle any initialization errors
    if err != nil {
        log.Fatal(err)
    }

    // make sure to close to clean up when done, to avoid leaks.
    defer client.Close()

    // Send a stat
    client.Inc("stat1", 42, 1.0)

    // Send a stat with "Tags"
    client.Inc("stat2", 41, 1.0, Tag{"mytag", "tagval"})
}

Legacy Example

A legacy client creation method is still supported. This is retained so as not to break or interrupt existing integrations.

import (
    "log"

    "github.com/cactus/go-statsd-client/v5/statsd"
)

func main() {
    // first create a client
    // The basic client sends one stat per packet (for compatibility).
    client, err := statsd.NewClient("127.0.0.1:8125", "test-client")

    // A buffered client, which sends multiple stats in one packet, is
    // recommended when your server supports it (better performance).
    // client, err := statsd.NewBufferedClient("127.0.0.1:8125", "test-client", 300*time.Millisecond, 0)

    // handle any errors
    if err != nil {
        log.Fatal(err)
    }
    // make sure to close to clean up when done, to avoid leaks.
    defer client.Close()

    // Send a stat
    client.Inc("stat1", 42, 1.0)
}

See docs for more info. There is also some additional example code in the test-client directory.

Contributors

See here.

Alternative Implementations

See the statsd wiki for some additional client implementations (scroll down to the Go section).

License

Released under the MIT license. See LICENSE.md file for details.

go-statsd-client's People

Contributors

dropwhile avatar weikai77 avatar falun avatar taraspos avatar khssnv avatar dvirsky avatar jsocol avatar joeshaw avatar nrwiersma avatar richarddbarnett avatar sandeep4 avatar shawnps avatar tepx avatar tonyghita avatar davidmecham avatar shsing2000 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.