Last active
August 29, 2015 14:02
-
-
Save up1/418493f6a01c6bf58bdc to your computer and use it in GitHub Desktop.
Go => Using expvar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "expvar" | |
| "log" | |
| ) | |
| var ( | |
| counter = expvar.NewInt("counter for my program") | |
| ) | |
| func printVars() { | |
| log.Println("expvars:") | |
| expvar.Do(func(kv expvar.KeyValue) { | |
| switch kv.Key { | |
| case "memstats": | |
| // Do nothing, this is a big output. | |
| default: | |
| log.Printf("\t%s -> %s", kv.Key, kv.Value) | |
| } | |
| }) | |
| } | |
| func main() { | |
| counter.Add(1) | |
| counter.Add(2) | |
| counter.Add(1) | |
| printVars() | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "expvar" | |
| "io" | |
| "log" | |
| "net/http" | |
| ) | |
| var ( | |
| counter = expvar.NewInt("counter for my program") | |
| ) | |
| func CounterServer(w http.ResponseWriter, req *http.Request) { | |
| counter.Add(1) | |
| io.WriteString(w, "Count data ...") | |
| } | |
| func main() { | |
| http.HandleFunc("/", CounterServer) | |
| http.ListenAndServe(":8080", nil) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "g2g" | |
| "expvar" | |
| "time" | |
| "fmt" | |
| "net/http" | |
| "io" | |
| ) | |
| var ( | |
| myCounter = expvar.NewInt("Demo counter") | |
| ) | |
| func CounterServer(w http.ResponseWriter, req *http.Request) { | |
| io.WriteString(w, "Count data ...") | |
| go myCounter.Add(1) | |
| } | |
| func main() { | |
| interval := 5 * time.Second | |
| timeout := 3 * time.Second | |
| g, err := g2g.NewGraphite("127.0.0.1:2003", interval, timeout) | |
| if err != nil { | |
| fmt.Printf("Error =>%v", err) | |
| } | |
| g.Register("up1.demo.counter", myCounter) | |
| http.HandleFunc("/", CounterServer) | |
| http.ListenAndServe(":8080", nil) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func (g *Graphite) loop() { | |
| ticker := time.NewTicker(g.interval) | |
| defer ticker.Stop() | |
| for { | |
| select { | |
| case nv := <-g.registrations: | |
| g.vars[nv.name] = nv.v | |
| case <-ticker.C: | |
| g.postAll() | |
| case q := <-g.shutdown: | |
| g.connection.Close() | |
| g.connection = nil | |
| q <- true | |
| return | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment