Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

simple http example that increments a metric on request #9

Merged
merged 1 commit into from Jul 14, 2016
Jump to file or symbol
Failed to load files and symbols.
+58 −0
Split
@@ -0,0 +1,58 @@
+package main
+
+import (
+ "fmt"
+ "net/http"
+ "os"
+
+ "github.com/performancecopilot/speed"
+)
+
+var metric *speed.PCPSingletonMetric
+
+func main() {
+
+ var err error
+ metric, err = speed.NewPCPSingletonMetric(
+ 0,
+ "http.requests",
+ speed.Int32Type,
+ speed.CounterSemantics,
+ speed.OneUnit,
+ "Number of Requests",
+ "Counter that increments every request",
+ )
+ if err != nil {
+ panic(err)
+ }
+
+ writer, err := speed.NewPCPWriter("example", speed.ProcessFlag)
+ if err != nil {
+ panic(err)
+ }
+
+ err = writer.Register(metric)
+ if err != nil {
+ panic(err)
+ }
+
+ err = writer.Start()
+ if err != nil {
+ panic(err)
+ }
+ defer writer.Stop()
+
+ http.HandleFunc("/increment", handleIncrement)
+ go http.ListenAndServe(":8080", nil)
+
+ fmt.Println("To stop the server press enter")
+ os.Stdin.Read(make([]byte, 1))
+ os.Exit(0)
+}
+
+func handleIncrement(w http.ResponseWriter, r *http.Request) {
+ v := metric.Val().(int32)
+ v++
+ metric.Set(v)
+ fmt.Fprintf(w, "incremented\n")
+}