From 920f6b01b73c1676b54f099ccde1f1a40966dd5e Mon Sep 17 00:00:00 2001 From: Kamil Samigullin Date: Sun, 23 Jun 2019 20:39:17 +0300 Subject: [PATCH] review the api --- Makefile | 3 +++ README.md | 6 +++--- context_test.go | 4 ++-- example_test.go | 6 +++--- helper_test.go | 18 +++++++++--------- tracer.go | 37 +++++++++++++++---------------------- tracer_test.go | 26 ++++++++------------------ 7 files changed, 43 insertions(+), 57 deletions(-) diff --git a/Makefile b/Makefile index 5d8009a..63af7f4 100644 --- a/Makefile +++ b/Makefile @@ -5,6 +5,9 @@ GOFLAGS = -mod=vendor TIMEOUT = 1s +.DEFAULT_GOAL = test + + .PHONY: deps deps: @go mod tidy && go mod vendor && go mod verify diff --git a/README.md b/README.md index 58eb859..b7d1c08 100644 --- a/README.md +++ b/README.md @@ -49,15 +49,15 @@ func Handle(rw http.ResponseWriter, req *http.Request) { ctx, cancel := context.WithTimeout(req.Context(), time.Second) defer cancel() - call := tracer.Fetch(req.Context()).Start().Mark(req.Header.Get("X-Request-Id")) + call := tracer.Fetch(req.Context()).Start(req.Header.Get("X-Request-Id")) defer call.Stop() ... - call.Checkpoint().Mark("serialize") + call.Checkpoint("serialize") data := FetchData(ctx, req.Body) - call.Checkpoint().Mark("store") + call.Checkpoint("store") if err := StoreIntoDatabase(ctx, data); err != nil { http.Error(rw, http.StatusText(http.StatusInternalServerError), diff --git a/context_test.go b/context_test.go index d867d7b..0cc7c83 100644 --- a/context_test.go +++ b/context_test.go @@ -12,14 +12,14 @@ func TestContext(t *testing.T) { if Fetch(context.Background()) != nil { t.Error("expected nil") } - Fetch(context.Background()).Start().Mark("no panic").Stop() + Fetch(context.Background()).Start("no panic").Stop() }) t.Run("fetch injected", func(t *testing.T) { ctx := Inject(context.Background(), nil) if Fetch(ctx) == nil { t.Error("unexpected nil") } - Fetch(ctx).Start().Mark("allocation").Stop() + Fetch(ctx).Start("allocation").Stop() }) } diff --git a/example_test.go b/example_test.go index 111eef7..d38b19a 100644 --- a/example_test.go +++ b/example_test.go @@ -64,12 +64,12 @@ func Handle(rw http.ResponseWriter, req *http.Request) { ctx, cancel := context.WithTimeout(req.Context(), time.Second) defer cancel() - call := tracer.Fetch(req.Context()).Start().Mark(req.Header.Get("X-Request-Id")) + call := tracer.Fetch(req.Context()).Start(req.Header.Get("X-Request-Id")) defer call.Stop() time.Sleep(time.Millisecond) - call.Checkpoint().Mark("serialize") + call.Checkpoint("serialize") data, err := FetchData(ctx, req.Body) if err != nil { http.Error(rw, err.Error(), http.StatusInternalServerError) @@ -78,7 +78,7 @@ func Handle(rw http.ResponseWriter, req *http.Request) { time.Sleep(time.Millisecond) - call.Checkpoint().Mark("store") + call.Checkpoint("store") if err := StoreIntoDatabase(ctx, data); err != nil { http.Error(rw, err.Error(), http.StatusInternalServerError) return diff --git a/helper_test.go b/helper_test.go index 099ba08..16caab9 100644 --- a/helper_test.go +++ b/helper_test.go @@ -22,24 +22,24 @@ func callerC() CallerInfo { } func traceRoot(ctx context.Context) { - call := Fetch(ctx).Start().Mark("root") + call := Fetch(ctx).Start("root") defer call.Stop() - call.Checkpoint().Mark("checkpointA") + call.Checkpoint("checkpointA") traceA(ctx) - call.Checkpoint().Mark("checkpointB") + call.Checkpoint("checkpointB") traceB(ctx) } func traceA(ctx context.Context) { - call := Fetch(ctx).Start().Mark("A") + call := Fetch(ctx).Start("A") defer call.Stop() - call.Checkpoint().Mark("checkpointA1") + call.Checkpoint("checkpointA1") traceA1(ctx) - call.Checkpoint().Mark("checkpointA2") + call.Checkpoint("checkpointA2") traceA2(ctx) } @@ -55,13 +55,13 @@ func traceA2(ctx context.Context) { } func traceB(ctx context.Context) { - call := Fetch(ctx).Start().Mark("B") + call := Fetch(ctx).Start("B") defer call.Stop() - call.Checkpoint().Mark("checkpointB1") + call.Checkpoint("checkpointB1") traceB1(ctx) - call.Checkpoint().Mark("checkpointB2") + call.Checkpoint("checkpointB2") func(ctx context.Context) { defer Fetch(ctx).Start().Stop() }(ctx) diff --git a/tracer.go b/tracer.go index d849306..fb6aafd 100644 --- a/tracer.go +++ b/tracer.go @@ -12,15 +12,19 @@ type Trace struct { allocates int } -func (trace *Trace) Start() *Call { +func (trace *Trace) Start(labels ...string) *Call { if trace == nil { return nil } + var id string + if len(labels) > 0 { + id, labels = labels[0], labels[1:] + } + call := &Call{id: id, labels: labels, caller: Caller(3), start: time.Now()} if len(trace.stack) == cap(trace.stack) { trace.allocates++ } - call := &Call{caller: Caller(3), start: time.Now()} trace.stack = append(trace.stack, call) return call } @@ -68,19 +72,24 @@ func (trace *Trace) String() string { } type Call struct { + id string + labels []string caller CallerInfo start, stop time.Time - id string checkpoints []*Checkpoint allocates int } -func (call *Call) Checkpoint() *Checkpoint { +func (call *Call) Checkpoint(labels ...string) *Checkpoint { if call == nil { return nil } - checkpoint := &Checkpoint{timestamp: time.Now()} + var id string + if len(labels) > 0 { + id, labels = labels[0], labels[1:] + } + checkpoint := &Checkpoint{id: id, labels: labels, timestamp: time.Now()} if len(call.checkpoints) == cap(call.checkpoints) { call.allocates++ } @@ -89,15 +98,6 @@ func (call *Call) Checkpoint() *Checkpoint { return checkpoint } -func (call *Call) Mark(id string) *Call { - if call == nil { - return nil - } - - call.id = id - return call -} - func (call *Call) Stop() { if call == nil { return @@ -108,13 +108,6 @@ func (call *Call) Stop() { type Checkpoint struct { id string + labels []string timestamp time.Time } - -func (checkpoint *Checkpoint) Mark(id string) { - if checkpoint == nil { - return - } - - checkpoint.id = id -} diff --git a/tracer_test.go b/tracer_test.go index 8a09a6f..ca03f24 100644 --- a/tracer_test.go +++ b/tracer_test.go @@ -9,8 +9,8 @@ import ( ) func TestTrace_Start(t *testing.T) { - (*Trace)(nil).Start().Mark("no panic") - (&Trace{}).Start().Mark("one allocation") + (*Trace)(nil).Start("no panic") + (&Trace{}).Start("one allocation") } func TestTrace_String(t *testing.T) { @@ -26,8 +26,8 @@ func TestTrace_String(t *testing.T) { t.Errorf("\n expected: %+#v \n obtained: %+#v", expected, obtained) } - call := trace.Start().Mark("fn call") - call.Checkpoint().Mark("checkpoint") + call := trace.Start("fn call") + call.Checkpoint("checkpoint") call.Stop() if expected, obtained := "allocates at call stack: 1", trace.String(); !strings.Contains(obtained, expected) { t.Errorf("\n expected: %+#v \n obtained: %+#v", expected, obtained) @@ -36,23 +36,13 @@ func TestTrace_String(t *testing.T) { } func TestCall_Checkpoint(t *testing.T) { - (*Call)(nil).Checkpoint().Mark("no panic") - (&Call{}).Checkpoint().Mark("one allocation") -} - -func TestCall_Mark(t *testing.T) { - (*Call)(nil).Mark("no panic") - (&Call{}).Mark("by id") + (*Call)(nil).Checkpoint("no panic") + (&Call{}).Checkpoint("one allocation") } func TestCall_Stop(t *testing.T) { - (*Call)(nil).Mark("no panic").Stop() - (&Call{}).Mark("success").Stop() -} - -func TestCheckpoint_Mark(t *testing.T) { - (*Checkpoint)(nil).Mark("no panic") - (&Checkpoint{}).Mark("by id") + (*Call)(nil).Stop() + (&Call{}).Stop() } // BenchmarkTracing/silent-12 200000 7755 ns/op 1816 B/op 24 allocs/op