Skip to content

Commit f3a0f84

Browse files
committed
refactor(stacktrace): add captureStack helper for DRY capture logic
Introduced captureStack() as the single source of truth for stack capture implementation, eliminating code duplication across capture functions. This consolidates the iterator(skip, maxDepth, opts).capture() pattern into a single helper, reducing duplicate code by ~20 lines.
1 parent 183be78 commit f3a0f84

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

internal/stacktrace/stacktrace.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,18 +245,38 @@ func parseSymbol(name string) symbol {
245245
}
246246
}
247247

248+
// captureStack is the core stack capture implementation used by all public capture functions.
249+
// It captures a stack trace with the given skip count, depth limit, and frame processing options.
250+
func captureStack(skip int, maxDepth int, opts frameOptions) StackTrace {
251+
return iterator(skip, maxDepth, opts).capture()
252+
}
253+
248254
// Capture create a new stack trace from the current call stack
249255
func Capture() StackTrace {
250256
return SkipAndCapture(defaultCallerSkip)
251257
}
252258

253259
// SkipAndCapture creates a new stack trace from the current call stack, skipping the first `skip` frames
254260
func SkipAndCapture(skip int) StackTrace {
255-
return iterator(skip, defaultMaxDepth, frameOptions{
261+
return captureStack(skip, defaultMaxDepth, frameOptions{
256262
skipInternalFrames: true,
257263
redactCustomerFrames: false,
258264
internalPackagePrefixes: internalSymbolPrefixes,
259-
}).capture()
265+
})
266+
}
267+
268+
// SkipAndCaptureWithInternalFrames creates a new stack trace from the current call stack without filtering internal frames.
269+
// This is useful for tracer span error stacktraces where we want to capture all frames.
270+
func SkipAndCaptureWithInternalFrames(depth int, skip int) StackTrace {
271+
// Use default depth if not specified
272+
if depth == 0 {
273+
depth = defaultMaxDepth
274+
}
275+
return captureStack(skip, depth, frameOptions{
276+
skipInternalFrames: false,
277+
redactCustomerFrames: false,
278+
internalPackagePrefixes: nil,
279+
})
260280
}
261281

262282
// CaptureRaw captures only program counters without symbolication.
@@ -275,11 +295,11 @@ func CaptureRaw(skip int) RawStackTrace {
275295
// This is designed for telemetry logging where we want to see internal frames for debugging
276296
// but need to redact customer code for security
277297
func CaptureWithRedaction(skip int) StackTrace {
278-
return iterator(skip+1, defaultMaxDepth, frameOptions{
298+
return captureStack(skip+1, defaultMaxDepth, frameOptions{
279299
skipInternalFrames: false, // Keep DD internal frames
280300
redactCustomerFrames: true, // Redact customer code
281301
internalPackagePrefixes: internalSymbolPrefixes,
282-
}).capture()
302+
})
283303
}
284304

285305
// Symbolicate converts raw PCs to a full StackTrace with symbolication,

0 commit comments

Comments
 (0)