Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

🪡 Dead simple, lightweight tracing.

License

NotificationsYou must be signed in to change notification settings

kamilsk/tracer

Repository files navigation

Dead simple, lightweight tracing.

BuildDocumentationQualityTemplateCoverageAwesome

💡 Idea

The tracer provides API to trace execution flow.

funcDo(ctx context.Context) {defertracer.Fetch(ctx).Start().Stop()// do some heavy job}

A full description of the idea is availablehere.

🏆 Motivation

AtAvito, we use theJaeger - a distributed tracing platform.It is handy in most cases, but at production, we also use sampling. So, what is a problem, you say?

I had 0.02% requests with awrite: broken pipe error and it was difficult to find the appropriate one intheSentry which also has trace related to it in theJaeger.

For that reason, I wrote the simple solution to handle this specific case and found the bottleneck in our code quickly.

🤼‍♂️ How to

import ("context""io""net/http""time""github.com/kamilsk/tracer")funcInjectTracer(handler http.Handler) http.Handler {returnhttp.HandlerFunc(func(rw http.ResponseWriter,req*http.Request) {req=req.WithContext(tracer.Inject(req.Context(),make([]*tracer.Call,0,10)))handler.ServeHTTP(rw,req)})}funcHandle(rw http.ResponseWriter,req*http.Request) {ctx,cancel:=context.WithTimeout(req.Context(),time.Second)defercancel()call:=tracer.Fetch(req.Context()).Start(req.Header.Get("X-Request-Id"))defercall.Stop()...call.Checkpoint("serialize")data,err:=FetchData(ctx,req.Body)iferr!=nil {http.Error(rw,err.Error(),http.StatusInternalServerError)return}call.Checkpoint("store")iferr:=StoreIntoDatabase(ctx,data);err!=nil {http.Error(rw,http.StatusText(http.StatusInternalServerError),http.StatusInternalServerError)return}rw.WriteHeader(http.StatusOK)}funcFetchData(ctx context.Context,r io.Reader) (Data,error) {defertracer.Fetch(ctx).Start().Stop()// fetch a data into a struct}funcStoreIntoDatabase(ctx context.Context,dataData)error {defertracer.Fetch(ctx).Start().Stop()// store the data into a database}

Output:

allocates at call stack: 0, detailed call stack:call Handle [ca7a87c4-58d0-4fdf-857c-ef49fc3bf271]: 14.038083ms, allocates: 2checkpoint [serialize]: 1.163587mscheckpoint [store]: 2.436265mscall FetchData: 1.192829ms, allocates: 0call StoreIntoDatabase: 10.428663ms, allocates: 0

🧩 Integration

The library usesSemVer for versioning, and it is notBC-safe through major releases.You can usego modules to manage its version.

$ go get github.com/kamilsk/tracer@latest

made with ❤️ for everyone


[8]ページ先頭

©2009-2025 Movatter.jp