Skip to content

Commit

Permalink
review the api
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilsk committed Jun 23, 2019
1 parent efcbaef commit 920f6b0
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 57 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ GOFLAGS = -mod=vendor
TIMEOUT = 1s


.DEFAULT_GOAL = test


.PHONY: deps
deps:
@go mod tidy && go mod vendor && go mod verify
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
4 changes: 2 additions & 2 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
}

Expand Down
6 changes: 3 additions & 3 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
18 changes: 9 additions & 9 deletions helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
Expand Down
37 changes: 15 additions & 22 deletions tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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++
}
Expand All @@ -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
Expand All @@ -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
}
26 changes: 8 additions & 18 deletions tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 920f6b0

Please sign in to comment.