Skip to content

Commit 173e13b

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 3e4409f commit 173e13b

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
@@ -246,18 +246,38 @@ func parseSymbol(name string) symbol {
246246
}
247247
}
248248

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

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

263283
// SkipAndCaptureWithInternalFrames creates a new stack trace from the current call stack without filtering internal frames.
@@ -290,11 +310,11 @@ func CaptureRaw(skip int) RawStackTrace {
290310
// This is designed for telemetry logging where we want to see internal frames for debugging
291311
// but need to redact customer code for security
292312
func CaptureWithRedaction(skip int) StackTrace {
293-
return iterator(skip+1, defaultMaxDepth, frameOptions{
313+
return captureStack(skip+1, defaultMaxDepth, frameOptions{
294314
skipInternalFrames: false, // Keep DD internal frames
295315
redactCustomerFrames: true, // Redact customer code
296316
internalPackagePrefixes: internalSymbolPrefixes,
297-
}).capture()
317+
})
298318
}
299319

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

0 commit comments

Comments
 (0)