go-ethereum/common/tracing/context.go
SHIVAM SHARMA b1d86bd6ea
Shivam/txpool tracing (#604)
* lock, unlock to rlock, runlock

* add : tracing Pending() and Locals()

* Log time spent in committing a tx during mining

* Remove data from logging

* Move log into case where a tx completes without error

* profile fillTransactions

* fix conflict

* bug fixes

* add logs

* txpool: add tracing in Pending()

* rearrange tracing

* add attributes

* fix

* fix

* log error in profiling

* update file mode and file path for profiling

* full profiling

* fix log

* fix log

* less wait

* fix

* fix

* logs

* worker: use block number for prof files

* initial

* txList add

* fix gas calculation

* fix

* green tests

* linters

* prettify

* allocate less

* no locks between pending and reorg

* no locks

* no locks on locals

* more tests

* linters

* less allocs

* comment

* optimize errors

* linters

* fix

* fix

* Linters

* linters

* linters

* simplify errors

* atomics for transactions

* fix

* optimize workers

* fix copy

* linters

* txpool tracing

* linters

* fix tracing

* duration in mcs

* locks

* metrics

* fix

* cache hit/miss

* less locks on evict

* remove once

* remove once

* switch off pprof

* fix data race

* fix data race

* add : sealed total/empty blocks metric gauge

* add : RPC debug_getTraceStack

* fix : RPC debug_getTraceStack

* fix : RPC debug_getTraceStack for all go-routines

* linters

* add data race test on txpool

* fix concurrency

* noleak

* increase batch size

* prettify

* tests

* baseFee mutex

* panic fix

* linters

* fix gas fee data race

* linters

* more transactions

* debug

* debug

* fix ticker

* fix test

* add cacheMu

* more tests

* fix test panic

* linters

* add statistics

* add statistics

* txitems data race

* fix tx list Has

* fix : lint

Co-authored-by: Arpit Temani <temaniarpit27@gmail.com>
Co-authored-by: Jerry <jerrycgh@gmail.com>
Co-authored-by: Manav Darji <manavdarji.india@gmail.com>
Co-authored-by: Evgeny Danienko <6655321@bk.ru>
2022-12-12 12:34:02 +05:30

102 lines
1.9 KiB
Go

package tracing
import (
"context"
"time"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)
type tracerKey struct{}
type Option func(context.Context, trace.Span)
func WithTracer(ctx context.Context, tr trace.Tracer) context.Context {
return context.WithValue(ctx, tracerKey{}, tr)
}
func FromContext(ctx context.Context) trace.Tracer {
tr, _ := ctx.Value(tracerKey{}).(trace.Tracer)
return tr
}
func StartSpan(ctx context.Context, snapName string) (context.Context, trace.Span) {
tr := FromContext(ctx)
if tr == nil {
return ctx, nil
}
ctx, span := tr.Start(ctx, snapName)
ctx = WithTracer(ctx, tr)
return ctx, span
}
func EndSpan(span trace.Span) {
if span != nil {
span.End()
}
}
func Trace(ctx context.Context, spanName string) (context.Context, trace.Span) {
tr := FromContext(ctx)
if tr == nil {
return ctx, nil
}
return tr.Start(ctx, spanName)
}
func Exec(ctx context.Context, instrumentationName, spanName string, opts ...Option) {
var span trace.Span
tr := FromContext(ctx)
if tr == nil && len(instrumentationName) != 0 {
tr = otel.GetTracerProvider().Tracer(instrumentationName)
ctx = WithTracer(ctx, tr)
}
if tr != nil {
ctx, span = tr.Start(ctx, spanName)
}
for _, optFn := range opts {
optFn(ctx, span)
}
if tr != nil {
span.End()
}
}
func WithTime(fn func(context.Context, trace.Span)) Option {
return func(ctx context.Context, span trace.Span) {
ElapsedTime(ctx, span, "elapsed", fn)
}
}
func ElapsedTime(ctx context.Context, span trace.Span, msg string, fn func(context.Context, trace.Span)) {
var now time.Time
if span != nil {
now = time.Now()
}
fn(ctx, span)
if span != nil {
span.SetAttributes(attribute.Int(msg, int(time.Since(now).Microseconds())))
}
}
func SetAttributes(span trace.Span, kvs ...attribute.KeyValue) {
if span != nil {
span.SetAttributes(kvs...)
}
}