Skip to content

Commit

Permalink
complete the doc
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilsk committed Jun 24, 2019
1 parent 1362c6d commit 63047d5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
11 changes: 11 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,22 @@ import "context"

type key struct{}

// Fetch tries to get the tracer from a context or returns safe nil.
//
// tracer.Fetch(context.Background()).Start().Stop() // won't panic
//
func Fetch(ctx context.Context) *Trace {
trace, _ := ctx.Value(key{}).(*Trace)
return trace
}

// Inject returns a new context with injected into it the tracer.
//
// func (server *server) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
// req = req.WithContext(tracer.Inject(req.Context(), make([]*trace.Call, 0, 10)))
// server.routing.Handle(rw, req)
// }
//
func Inject(ctx context.Context, stack []*Call) context.Context {
return context.WithValue(ctx, key{}, &Trace{stack: stack})
}
30 changes: 30 additions & 0 deletions tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@ import (
"time"
)

// Trace holds information about a current execution flow.
type Trace struct {
stack []*Call
allocates int
}

// Start creates a call entry and marks its start time.
//
// func Do(ctx context.Context) {
// call := tracer.Fetch(ctx).Start()
// defer call.Stop()
// }
//
func (trace *Trace) Start(labels ...string) *Call {
if trace == nil {
return nil
Expand All @@ -29,6 +37,7 @@ func (trace *Trace) Start(labels ...string) *Call {
return call
}

// String returns a string representation of the current execution flow.
func (trace *Trace) String() string {
if trace == nil {
return ""
Expand Down Expand Up @@ -71,6 +80,7 @@ func (trace *Trace) String() string {
return builder.String()
}

// Call holds information about a current function call.
type Call struct {
id string
labels []string
Expand All @@ -80,6 +90,18 @@ type Call struct {
allocates int
}

// Checkpoint stores timestamp of a current execution position of the current call.
//
// func Do(ctx context.Context) {
// call := tracer.Fetch(ctx).Start()
// defer call.Stop()
// ...
// call.Checkpoint()
// ...
// call.Checkpoint("id", "labelX", "labelY")
// ...
// }
//
func (call *Call) Checkpoint(labels ...string) {
if call == nil {
return
Expand All @@ -96,6 +118,13 @@ func (call *Call) Checkpoint(labels ...string) {
call.checkpoints = append(call.checkpoints, checkpoint)
}

// Stop marks the end time of the current call.
//
// func Do(ctx context.Context) {
// defer tracer.Fetch(ctx).Start().Stop()
// ...
// }
//
func (call *Call) Stop() {
if call == nil {
return
Expand All @@ -104,6 +133,7 @@ func (call *Call) Stop() {
call.stop = time.Now()
}

// Checkpoint holds information about a current execution position of a current call.
type Checkpoint struct {
id string
labels []string
Expand Down

0 comments on commit 63047d5

Please sign in to comment.