Merge pull request #6 from shwoodard/allow-instrumentation

allow instrumenting serialization and deserialization--event firehose
This commit is contained in:
Sam Woodard 2015-07-31 16:45:23 -07:00
commit 7750131b7c
2 changed files with 143 additions and 4 deletions

View File

@ -16,9 +16,11 @@ import (
)
func createBlog(w http.ResponseWriter, r *http.Request) {
jsonapiRuntime := jsonapi.NewRuntime().Instrument("blogs.create")
blog := new(Blog)
if err := jsonapi.UnmarshalPayload(r.Body, blog); err != nil {
if err := jsonapiRuntime.UnmarshalPayload(r.Body, blog); err != nil {
http.Error(w, err.Error(), 500)
return
}
@ -28,12 +30,13 @@ func createBlog(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(201)
w.Header().Set("Content-Type", "application/vnd.api+json")
if err := jsonapi.MarshalOnePayload(w, blog); err != nil {
if err := jsonapiRuntime.MarshalOnePayload(w, blog); err != nil {
http.Error(w, err.Error(), 500)
}
}
func listBlogs(w http.ResponseWriter, r *http.Request) {
jsonapiRuntime := jsonapi.NewRuntime().Instrument("blogs.list")
// ...fetch your blogs, filter, offset, limit, etc...
// but, for now
@ -41,7 +44,7 @@ func listBlogs(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Header().Set("Content-Type", "application/vnd.api+json")
if err := jsonapi.MarshalManyPayload(w, blogs); err != nil {
if err := jsonapiRuntime.MarshalManyPayload(w, blogs); err != nil {
http.Error(w, err.Error(), 500)
}
}
@ -54,18 +57,42 @@ func showBlog(w http.ResponseWriter, r *http.Request) {
intId, err := strconv.Atoi(id)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
jsonapiRuntime := jsonapi.NewRuntime().Instrument("blogs.show")
// but, for now
blog := testBlogForCreate(intId)
w.WriteHeader(200)
w.Header().Set("Content-Type", "application/vnd.api+json")
if err := jsonapi.MarshalOnePayload(w, blog); err != nil {
if err := jsonapiRuntime.MarshalOnePayload(w, blog); err != nil {
http.Error(w, err.Error(), 500)
}
}
func main() {
jsonapi.Instrumentation = func(r *jsonapi.Runtime, eventType jsonapi.Event, callGUID string, dur time.Duration) {
metricPrefix := r.Value("instrument").(string)
if eventType == jsonapi.UnmarshalStart {
fmt.Printf("%s: id, %s, started at %v\n", metricPrefix+".jsonapi_unmarshal_time", callGUID, time.Now())
}
if eventType == jsonapi.UnmarshalStop {
fmt.Printf("%s: id, %s, stopped at, %v , and took %v to unmarshal paylaod\n", metricPrefix+".jsonapi_unmarshal_time", callGUID, time.Now(), dur)
}
if eventType == jsonapi.MarshalStart {
fmt.Printf("%s: id, %s, started at %v\n", metricPrefix+".jsonapi_marshal_time", callGUID, time.Now())
}
if eventType == jsonapi.MarshalStop {
fmt.Printf("%s: id, %s, stopped at, %v , and took %v to marshal paylaod\n", metricPrefix+".jsonapi_marshal_time", callGUID, time.Now(), dur)
}
}
http.HandleFunc("/blogs", func(w http.ResponseWriter, r *http.Request) {
if !regexp.MustCompile(`application/vnd\.api\+json`).Match([]byte(r.Header.Get("Accept"))) {
http.Error(w, "Unsupported Media Type", 415)
@ -157,7 +184,9 @@ func exerciseHandler() {
w := httptest.NewRecorder()
fmt.Println("============ start list ===========\n")
http.DefaultServeMux.ServeHTTP(w, req)
fmt.Println("============ stop list ===========\n")
jsonReply, _ := ioutil.ReadAll(w.Body)
@ -172,7 +201,9 @@ func exerciseHandler() {
w = httptest.NewRecorder()
fmt.Println("============ start show ===========\n")
http.DefaultServeMux.ServeHTTP(w, req)
fmt.Println("============ stop show ===========\n")
jsonReply, _ = ioutil.ReadAll(w.Body)
@ -191,7 +222,9 @@ func exerciseHandler() {
w = httptest.NewRecorder()
fmt.Println("============ start create ===========\n")
http.DefaultServeMux.ServeHTTP(w, req)
fmt.Println("============ stop create ===========\n")
buf := bytes.NewBuffer(nil)
io.Copy(buf, w.Body)

106
runtime.go Normal file
View File

@ -0,0 +1,106 @@
package jsonapi
import (
"crypto/rand"
"fmt"
"io"
"time"
)
type Event int
const (
UnmarshalStart Event = iota
UnmarshalStop
MarshalStart
MarshalStop
)
type Runtime struct {
ctx map[string]interface{}
}
type Events func(*Runtime, Event, string, time.Duration)
var Instrumentation Events
func NewRuntime() *Runtime { return &Runtime{make(map[string]interface{})} }
func (r *Runtime) WithValue(key string, value interface{}) *Runtime {
r.ctx[key] = value
return r
}
func (r *Runtime) Value(key string) interface{} {
return r.ctx[key]
}
func (r *Runtime) Instrument(key string) *Runtime {
return r.WithValue("instrument", key)
}
func (r *Runtime) shouldInstrument() bool {
return Instrumentation != nil
}
func (r *Runtime) UnmarshalPayload(reader io.Reader, model interface{}) error {
return r.instrumentCall(UnmarshalStart, UnmarshalStop, func() error {
return UnmarshalPayload(reader, model)
})
}
func (r *Runtime) MarshalOnePayload(w io.Writer, model interface{}) error {
return r.instrumentCall(MarshalStart, MarshalStop, func() error {
return MarshalOnePayload(w, model)
})
}
func (r *Runtime) MarshalManyPayload(w io.Writer, models []interface{}) error {
return r.instrumentCall(MarshalStart, MarshalStop, func() error {
return MarshalManyPayload(w, models)
})
}
func (r *Runtime) MarshalOnePayloadEmbedded(w io.Writer, model interface{}) error {
return r.instrumentCall(MarshalStart, MarshalStop, func() error {
return MarshalOnePayloadEmbedded(w, model)
})
}
func (r *Runtime) instrumentCall(start Event, stop Event, c func() error) error {
if !r.shouldInstrument() {
return c()
}
instrumentationGUID, err := newUUID()
if err != nil {
return err
}
begin := time.Now()
Instrumentation(r, start, instrumentationGUID, time.Duration(0))
if err := c(); err != nil {
return err
}
diff := time.Duration(time.Now().UnixNano() - begin.UnixNano())
Instrumentation(r, stop, instrumentationGUID, diff)
return nil
}
// citation: http://play.golang.org/p/4FkNSiUDMg
func newUUID() (string, error) {
uuid := make([]byte, 16)
n, err := io.ReadFull(rand.Reader, uuid)
if n != len(uuid) || err != nil {
return "", err
}
// variant bits; see section 4.1.1
uuid[8] = uuid[8]&^0xc0 | 0x80
// version 4 (pseudo-random); see section 4.1.3
uuid[6] = uuid[6]&^0xf0 | 0x40
return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:]), nil
}