
Company News
Socket Partners with Replit to Block Malicious Packages in AI-Powered Development
Replit is integrating Socket Firewall into its AI-powered development experience to help protect builders from malicious open source packages.
Welcome to GoTracer! A package for tracing Go applications, re-invented with all recent insights on metrics collection and tracing. It is a spiritual successor of xlab/statsd_metrics, but with omitted "metrics" part to fully focus on tracing.
GoTracer is built around go.opentelemetry.io/otel package and its Tracer interface. Any Otel-compatible pipeline can be used to collect and aggregate the traces collected by this tool.
We anticipate to use the following OTel receivers:
There are certain ways to transform traces into metrics, a great article on this topic: https://newrelic.com/blog/nerdlog/transforming-traces
import "upd.dev/xlab/gotracer"
type MyService struct {
svcTags gotracer.Tags
}
func NewMyService() *MyService {
return &MyService{
svcTags: gotracer.NewTag("svc", "myService"),
}
}
func (s *MyService) SomeFunc(ctx context.Context) error {
defer gotracer.Trace(&ctx, s.svcTags)()
// do a subcall
s.SomeOtherFunc(ctx)
err := db.Query(ctx, "SELECT * FROM users")
if err != nil {
gotracer.TraceError(ctx, err)
return err
}
return nil
}
func (s *MyService) SomeOtherFunc(ctx context.Context) {
defer gotracer.Trace(&ctx, s.svcTags)()
// do something
}
gotracer.Trace is used to initiate a span within a service methodgotracer.TraceError is used to end span, add the error and mark span as failedgotracer.Tags is used to add tags to the trace spangotracer.NewTag is a shortcut for gotracer.NewTagsgotracer.WithTags can add more tags to the existing span.The line defer gotracer.Trace(&ctx)() unfolds into the following runtime actions:
Trace() returns a type SpanEnderFn func() that ends span with a success and records the duration.deferred.*context.Context), so the context can be updated with the span data in-place.gotracer.TraceError on the same context later.TraceError will mark it as failed and add the error to the span, but also tombstone the context.SpanEnderFn function will be no-op.TraceError on a tombstoned context, it will emit a warning with a stacktrace in the logs, considered a programming error.TraceError on a an empty or nil context, it will create a virtual span, e.g. via TraceError(nil, err, tags)nil context into Trace will emit a warning with a stacktrace in the logs, considered a programming error. Falls back to Traceless (see below).metrics package.ctx := context.Background()
fmt.Printf("pointer to context: %p\n", &ctx)
defer gotracer.Trace(&ctx)()
fmt.Printf("pointer to a new context: %p\n", &ctx)
It allows to keep a single return value of gotracer.Trace that can be used to end the span only.
gotracer.Tags provides a way to add additional tags to the span. Note that tags for the span are managed before span is created.
svcTags := gotracer.NewTag("svc", "myService")
additionalTags := svcTags.WithTags(gotracer.NewTag("block_height", 3245674))
defer gotracer.Trace(&ctx, additionalTags)()
Sometimes tags only known at certain point of the span execution, so we can use WithTags to add them.
svcTags := gotracer.Tags{
"svc": "myService",
}
defer gotracer.Trace(&ctx, svcTags)()
// much later
additionalTags := gotracer.NewTag("block_height", 3245674)
// gotracer.WithTags will fetch the span from the context and add tags to it
gotracer.WithTags(ctx, additionalTags)
Sometimes, especially with Goroutines, we need to trace anonymous functions. There is a helper function gotracer.TraceWithName that can be used to trace a function and give it a name explicitly.
func SomeFunc(ctx context.Context) {
defer gotracer.Trace(&ctx, "SomeFunc")()
// run a goroutine
go func() {
defer gotracer.TraceWithName(&ctx, "SomeOtherFuncGoroutine")()
// do something
// ctx here holds the span of SomeOtherFuncGoroutine
}()
// ctx here holds the span of SomeFunc
gotracer.WithTags(ctx, gotracer.NewTag("is_goroutine", false))
}
The trickiest of all tricks here is that the root context is captured as a value, so the reference to the context inside the goroutine closure will update only that child context with a value. The parent context of SomeFunc will hold the original value of the SomeFunc span.
While this sounds scary, a typical usage pattern is to just avoid overthinking and keep placing the gotracer.Trace* calls in the beginning of the most functions.
While spans are generated from the context, it's possible to create a span without it. This is still useful because the actual call stack can be deducted from the stack dump. This is the same as getting the function name from the latest stack frame, but instead, we get all the parent function names as well and construct a trace path out of many virtual spans.
Unfortunately, there is no way to get the timing of the parent spans. So, only the latest span will have the duration. We assume that there aren't many functions that lack a context.
import "upd.dev/xlab/gotracer"
type MyService struct {
svcTags gotracer.Tags
}
func NewMyService() *MyService {
return &MyService{
svcTags: gotracer.NewTag("svc", "myService"),
}
}
func (s *MyService) SomeFuncWithoutContext() error {
defer gotracer.Traceless(nil, s.svcTags)()
// do a subcall
s.SomeOtherFuncWithoutContext()
return nil
}
func (s *MyService) SomeOtherFuncWithoutContext() {
defer gotracer.Traceless(nil, s.svcTags)()
// do something
}
For the sake of API completeness, there is a TracelessWithName. But we discourage doing that.
func (s *MyService) SomeFuncWithoutContext() error {
defer gotracer.Traceless(nil, s.svcTags)()
// run a goroutine
go func() {
ctx := context.Background()
defer gotracer.TracelessWithName(&ctx, "SomeOtherFuncGoroutine", s.svcTags)()
// do stuff
gotracer.WithTags(ctx, gotracer.NewTag("is_goroutine", true))
}()
return nil
}
Traceless won't care about the context that is nil. It will just create a span without prior context. If the context is set, it will be used to:
gotracer.Traceless will act the same way as gotracer.Trace.gotracer.Trace is used to trace a method with a context.gotracer.Traceless is used to trace a method without a context.gotracer.TraceWithName is used to trace an anonymous closure with a given name.gotracer.TracelessWithName is used to trace an anonymous closure without a context, with a given name.gotracer.Tags is used to add tags to the span.gotracer.NewTags is used to create a new set of tags.gotracer.NewTag is a shortcut for gotracer.NewTagsgotracer.WithTags can add more tags to the existing span.gotracer.TraceError is used to end span, set the error and mark span as failed.All tracing functions are here to collect as much info about call stack, associate tags and measure timing of function execution. They're designed to have very little overhead in terms of line code and runtime performance.
If tracing is disabled, all tracing functions are no-op and should not affect the runtime performance. Including the tags management - tag joins are heavy on GC if used in hot paths. So WithTags will no-op.
When enbled, the tracing info will be processed with OTal config and client and will be sent to the collecting backend.
Refer to the example's main.go for more details. The example will output the traces to local SigNoz instance.
cd example && go run main.go
[Hello!] We expect some SigNoz OTEL collector listening on DSN localhost:4317
...
cfg := &gotracer.Config{
Enabled: true, // kept for app-level config, not consumed by gotracer itself
CollectorDSN: "localhost:4317", // OTLP/gRPC endpoint
CollectorSecureSSL: false, // use insecure gRPC when false
CollectorHeaders: map[string]string{
"authorization": "Bearer <token>",
},
EnvName: "local",
ServiceName: "my-service",
ServiceVersion: "dev",
ClusterID: "svc-us-east",
}
gotracer.Enable(cfg, otel.InitExporter)
exporters/otel.InitExporter uses sdktrace.WithBatcher(exporter), so the OpenTelemetry Go SDK batch span processor defaults apply unless you override them with standard OTEL env vars:
OTEL_BSP_MAX_QUEUE_SIZE: default 2048 spans buffered in memory before new spans start getting dropped.OTEL_BSP_MAX_EXPORT_BATCH_SIZE: default 512 spans sent in one export batch.OTEL_BSP_SCHEDULE_DELAY: default 5000 ms between scheduled exports.OTEL_BSP_EXPORT_TIMEOUT: default 30000 ms export timeout.So, with the defaults used by this repository, up to 2048 finished spans can sit in memory in the internal OTEL queue waiting to be exported, and they are normally flushed in batches of up to 512.
FAQs
Unknown package
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
Replit is integrating Socket Firewall into its AI-powered development experience to help protect builders from malicious open source packages.

Security News
npm confirmed a tooling bug incorrectly marked several one-character packages as security holders and said it was working on a rollback.

Research
/Security News
Newer packages in this compromise use native extensions and .pth loaders to execute JavaScript stealers in developer environments.