-
Notifications
You must be signed in to change notification settings - Fork 536
feat(tracer): add whole trace id to pprof labels and enable them with appsec #5064
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1018,11 +1018,7 @@ func (t *tracer) StartSpan(operationName string, options ...StartSpanOption) *Sp | |
| log.Debug("Started Span: %v, Operation: %s, Resource: %s, Tags: %v, %v", //nolint:gocritic // Debug logging needs full span representation | ||
| span, span.name, span.resource, &span.meta, span.metrics) | ||
| } | ||
| if cSnap.ProfilerHotspotsEnabled || cSnap.ProfilerEndpoints { | ||
| t.applyPPROFLabels(span.pprofCtxRestore, span, cSnap) | ||
| } else { | ||
| span.pprofCtxRestore = nil | ||
| } | ||
| t.applyPPROFLabels(span.pprofCtxRestore, span, cSnap) | ||
| if cSnap.DebugAbandonedSpans { | ||
| select { | ||
| case t.abandonedSpansDebugger.In <- newAbandonedSpanCandidate(span, false): | ||
|
|
@@ -1052,13 +1048,21 @@ func (t *tracer) StartSpan(operationName string, options ...StartSpanOption) *Sp | |
| // many times each endpoint is called. | ||
| // +checklocksignore — Initialization time, called from StartSpan before span is shared. | ||
| func (t *tracer) applyPPROFLabels(ctx gocontext.Context, span *Span, snap internalconfig.SpanStartSnapshot) { | ||
| // "local root span id" and "trace id" are emitted for code hotspots and when | ||
| // AppSec is enabled, so it can correlate security events with traces/profiles. | ||
| correlate := snap.ProfilerHotspotsEnabled || appsec.Enabled() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When AppSec is enabled with both profiler label features disabled, Useful? React with 👍 / 👎. |
||
| if !correlate && !snap.ProfilerEndpoints { | ||
| // No feature needs pprof labels; nothing to restore when the span finishes. | ||
| span.pprofCtxRestore = nil | ||
| return | ||
| } | ||
| // Important: The label keys are ordered alphabetically to take advantage of | ||
| // an upstream optimization that landed in go1.24. This results in ~10% | ||
| // better performance on BenchmarkStartSpan. See | ||
| // https://go-review.googlesource.com/c/go/+/574516 for more information. | ||
| labels := make([]string, 0, 3*2 /* 3 key value pairs */) | ||
| labels := make([]string, 0, 4*2 /* up to 4 key value pairs */) | ||
| localRootSpan := span.Root() | ||
| if snap.ProfilerHotspotsEnabled && localRootSpan != nil { | ||
| if correlate && localRootSpan != nil { | ||
| spanID := localRootSpan.getSpanID() | ||
| labels = append(labels, traceprof.LocalRootSpanID, strconv.FormatUint(spanID, 10)) | ||
| } | ||
|
|
@@ -1077,6 +1081,11 @@ func (t *tracer) applyPPROFLabels(ctx gocontext.Context, span *Span, snap intern | |
| } | ||
| } | ||
| } | ||
| if correlate { | ||
| // hexEncodedCached memoizes the hex on the (not-yet-shared) context so | ||
| // child spans reuse it: one hex allocation per trace, not per span. | ||
| labels = append(labels, traceprof.TraceID, span.context.traceID.hexEncodedCached()) | ||
| } | ||
| if len(labels) > 0 { | ||
| pprofActive := pprof.WithLabels(ctx, pprof.Labels(labels...)) | ||
| span.pprofCtxRestore = ctx | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When AppSec is enabled while endpoint profiling is explicitly off, this makes
correlatetrue andapplyPPROFLabelsnow stores a non-nilpprofCtxActive. Later,Span.SetTag(ext.ResourceName, ...)only checkss.pprofCtxActive != nilbefore addingtraceprof.TraceEndpoint(span.go:490-498), so web/custom spans that update their resource after start begin emittingtrace endpointlabels even withWithProfilerEndpoints(false). Please keep an explicit endpoints-enabled state for that update path or avoid creating endpoint labels unless endpoint profiling was enabled.Useful? React with 👍 / 👎.