Skip to content

Instantly share code, notes, and snippets.

@grisha
Created December 22, 2016 14:05
Show Gist options
  • Save grisha/9561e7837cff1340b218054f36430187 to your computer and use it in GitHub Desktop.
Save grisha/9561e7837cff1340b218054f36430187 to your computer and use it in GitHub Desktop.
Simple Graphite Server in Golang
// go get github.com/tgres/tgres
// go run simpletgres.go
package main
import (
"fmt"
"math"
"net/http"
"time"
"github.com/tgres/tgres/dsl"
h "github.com/tgres/tgres/http"
"github.com/tgres/tgres/rrd"
)
func sinTime(t time.Time, span time.Duration) float64 {
x := 2 * math.Pi / span.Seconds() * float64(t.Unix()%(span.Nanoseconds()/1e9))
return math.Sin(x)
}
func populateSeries(step, span time.Duration) *rrd.DataSource {
ds := rrd.NewDataSource(rrd.DSSpec{
Step: 1 * time.Second,
RRAs: []rrd.RRASpec{
rrd.RRASpec{Step: step, Span: span},
},
})
start := time.Now().Add(-span)
for i := 0; i < int(span/step); i++ {
t := start.Add(time.Duration(i) * step)
ds.ProcessDataPoint(sinTime(t, span), t)
}
return ds
}
func main() {
step := 10 * time.Second
span := 100 * step
ds := populateSeries(step, span)
rcache := dsl.NewReadCacheFromMap(map[string]rrd.DataSourcer{"foo.bar": ds})
http.HandleFunc("/metrics/find", h.GraphiteMetricsFindHandler(rcache))
http.HandleFunc("/render", h.GraphiteRenderHandler(rcache))
listenSpec := ":8088"
fmt.Printf("Waiting for requests on %s\n", listenSpec)
http.ListenAndServe(listenSpec, nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment