Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Unmarshal and Load methods #8

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ func main() {
expvar.Get("fib:rec:count").(metric.Metric).Add(1)
start := time.Now()
fmt.Fprintf(w, "%d", fibrec(40))
expvar.Get("fib:rec:sec").(metric.Metric).Add(float64(time.Now().Sub(start)) / float64(time.Second))
expvar.Get("fib:rec:sec").(metric.Metric).
Add(float64(time.Now().Sub(start)) / float64(time.Second))
})
fmt.Println("Listen on :8000")
http.ListenAndServe(":8000", nil)
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
module github.com/zserge/metric

go 1.13
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
14 changes: 12 additions & 2 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"expvar"
"fmt"
"log"
"net/http"
"sort"
"strings"
Expand All @@ -19,6 +20,7 @@ var (
<meta charset="utf-8">
<title>Metrics report</title>
<meta name="viewport" content="width=device-width">
<meta http-equiv="refresh" content="1">
<style>
* { margin: 0; padding: 0; box-sizing: border-box; font-family: monospace; font-size: 12px; }
.container {
Expand Down Expand Up @@ -171,8 +173,16 @@ func Handler(snapshot func() map[string]Metric) http.Handler {
metrics := []h{}
for name, metric := range snapshot() {
m := h{}
b, _ := json.Marshal(metric)
json.Unmarshal(b, &m)
b, err := json.Marshal(metric)
if err != nil {
log.Println(err)
}

if err := json.Unmarshal(b, &m); err != nil {
log.Println("error at", name+":", err)
log.Println(string(b))
}

m["name"] = name
metrics = append(metrics, m)
}
Expand Down
Loading