Git Product home page Git Product logo

gostats's Introduction

GoStats

Simple Stats for Go Services

At Green Man Gaming we have a large number of services that run and we have a requirement to monitor them all.

To do this we rely on have stats we can cURL out. We have this for Scala via Ostrich from the guys over at Twitter. However, for Google Go we needed something similar and couldn't find something suitable.

GoStats was born.

go get github.com/greenmangaming/gostats

Three types of metrics are supported:

  1. Counters
  2. Labels
  3. Timers (count, sum, averages and percentiles)

Usage

Let's take the default example from Writing Web Applications of a very simple Go Http Server:

package main

import (
  "fmt"
  "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
  fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}

func main() {
  http.HandleFunc("/", handler)
  http.ListenAndServe(":8080", nil)
}

Let's do the same things with Stats:

package main

import (
  "fmt"
  "net/http"

  "github.com/greenmangaming/gostats"
)

func handler(w http.ResponseWriter, r *http.Request) {
  gostats.Stats.Time("root", func() {
    fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
  })
}

func main() {
  gostats.Stats.Addr = ":8081"
  go gostats.Stats.ListenAndServe()

  http.HandleFunc("/", handler)
  http.ListenAndServe(":8080", nil)
}

That's it!

Compile/run and do curl http://localhost:8080/Lee and then curl http://localhost:8081/stats:

➜  ~  curl http://localhost:8080/Lee
Hi there, I love Lee!%
➜  ~  curl http://localhost:8081/stats
{"counters":{},"labels":{},"metrics":{"root":{"avg":19959,"count":1,"p25":19959,
"p5":19959,"p75":19959,"p9":19959,"p95":19959,"p99":19959,"p999":19959,"p9999":19959,
"sum":19959}}}%

Timings are reported in Nanoseconds.

Counters

Counters are just that. They count every call. I recommend using this as a goroutine:

go gostats.Stats.IncrementCounter("gophers")

Labels

Labels are very basic and just allow you to set the current status of something:

go gostats.Stats.Label("groundhog_day", "yes")

Timers

Timers are a little bit more involved than the other types. An example of the usage of it is above.

gostats's People

Contributors

leepa avatar

Stargazers

Álex González avatar

Watchers

Álex González avatar Krzysztof Bandurski avatar Adam Finzel avatar James Cloos avatar

Forkers

deane

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.