tracer

package
v0.0.0-...-d31700d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 28, 2022 License: MIT Imports: 4 Imported by: 0

README

tracer

a tracer for golang.

Usage

func main() {
  ctx := tracer.Start(context.Background(), "main")
  defer tracer.Finish(ctx)

  tracer.Tag(ctx, "key", "value")
  tracer.Logf(ctx, "%v %v", time.Now(), "event")
 
  fmt.Println(tracer.Get(ctx))
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DebugLog

func DebugLog(ctx context.Context, args ...interface{})

DebugLog add a log to a tracer context if debug is enabled

func DebugLogf

func DebugLogf(ctx context.Context, format string, args ...interface{})

DebugLogf add a log to a tracer context if debug is enabled

func DebugTag

func DebugTag(ctx context.Context, k string, v interface{})

DebugTag add a tag to a tracer context if debug is enabled

func Finish

func Finish(ctx context.Context)

Finish finish the tracer on the given context

func IsDebug

func IsDebug(ctx context.Context) bool

func Log

func Log(ctx context.Context, args ...interface{})

Log add a log to a tracer context using Sprint

Example
ctx := Start(context.Background(), "name")

Log(ctx, "a ", "b")
fmt.Printf("%#v\n", Get(ctx).Logs)
Logf(ctx, "a %s", "b")
fmt.Printf("%#v\n", Get(ctx).Logs)

DebugLog(ctx, "a ", "b")
DebugLogf(ctx, "a %s", "b")
fmt.Printf("%#v\n", Get(ctx).Logs)
ctx = SetDebug(ctx)
DebugLog(ctx, "a ", "b")
DebugLogf(ctx, "a %s", "b")
fmt.Printf("%#v\n", Get(ctx).Logs)
Output:

[]string{"a b"}
[]string{"a b", "a b"}
[]string{"a b", "a b"}
[]string{"a b", "a b", "a b", "a b"}

func Logf

func Logf(ctx context.Context, format string, args ...interface{})

Logf add a log to a tracer context using Sprintf

func SetDebug

func SetDebug(ctx context.Context) context.Context

func Start

func Start(ctx context.Context, name string) context.Context

Start start a new tracer on the given context

Example
ctx := Start(context.Background(), "name")
Start(ctx, "child1")
Start(ctx, "child2")
Finish(ctx)

tracer := Get(ctx)
fmt.Println(
	tracer.Name, !tracer.At.IsZero(), tracer.Duration > 0, tracer.Tags, tracer.Logs,
	len(tracer.Children), tracer.Children[0].Name, tracer.Children[1].Name,
)
Output:

name true true map[] [] 2 child1 child2

func StartChild

func StartChild(ctx context.Context, name string) context.Context

Start start a child tracer on the given context

func Tag

func Tag(ctx context.Context, k string, v interface{})

Tag add a tag to a tracer context

Example
ctx := Start(context.Background(), "name")

Tag(ctx, "k", "v")
fmt.Println(Get(ctx).Tags)

DebugTag(ctx, "debugKey", "debugValue")
fmt.Println(Get(ctx).Tags)
ctx = SetDebug(ctx)
DebugTag(ctx, "debugKey", "debugValue")
fmt.Println(Get(ctx).Tags)
Output:

map[k:v]
map[k:v]
map[debugKey:debugValue k:v]

Types

type Tracer

type Tracer struct {
	sync.Mutex

	Name     string                 `json:"name,omitempty"`
	At       time.Time              `json:"at"`
	Duration float64                `json:"duration"` // milliseconds
	Children []*Tracer              `json:"children,omitempty"`
	Tags     map[string]interface{} `json:"tags,omitempty"`
	Logs     []string               `json:"logs,omitempty"`
}

func Get

func Get(ctx context.Context) *Tracer

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL