File: stacktrace.go

package info (click to toggle)
golang-github-newrelic-go-agent 3.15.2-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 8,356 kB
  • sloc: sh: 65; makefile: 6
file content (117 lines) | stat: -rw-r--r-- 2,862 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package newrelic

import (
	"bytes"
	"path"
	"runtime"
	"strings"
)

// stackTrace is a stack trace.
type stackTrace []uintptr

// getStackTrace returns a new stackTrace.
func getStackTrace() stackTrace {
	skip := 1 // skip runtime.Callers
	callers := make([]uintptr, maxStackTraceFrames)
	written := runtime.Callers(skip, callers)
	return callers[:written]
}

type stacktraceFrame struct {
	Name string
	File string
	Line int64
}

func (f stacktraceFrame) formattedName() string {
	if strings.HasPrefix(f.Name, "go.") {
		// This indicates an anonymous struct. eg.
		// "go.(*struct { github.com/newrelic/go-agent.threadWithExtras }).NoticeError"
		return f.Name
	}
	return path.Base(f.Name)
}

func (f stacktraceFrame) isAgent() bool {
	// Note this is not a contains conditional rather than a prefix
	// conditional to handle anonymous functions like:
	// "go.(*struct { github.com/newrelic/go-agent.threadWithExtras }).NoticeError"
	return strings.Contains(f.Name, "github.com/newrelic/go-agent/v3/internal.") ||
		strings.Contains(f.Name, "github.com/newrelic/go-agent/v3/newrelic.")
}

func (f stacktraceFrame) WriteJSON(buf *bytes.Buffer) {
	buf.WriteByte('{')
	w := jsonFieldsWriter{buf: buf}
	if f.Name != "" {
		w.stringField("name", f.formattedName())
	}
	if f.File != "" {
		w.stringField("filepath", f.File)
	}
	if f.Line != 0 {
		w.intField("line", f.Line)
	}
	buf.WriteByte('}')
}

func writeFrames(buf *bytes.Buffer, frames []stacktraceFrame) {
	// Remove top agent frames.
	for len(frames) > 0 && frames[0].isAgent() {
		frames = frames[1:]
	}
	// Truncate excessively long stack traces (they may be provided by the
	// customer).
	if len(frames) > maxStackTraceFrames {
		frames = frames[0:maxStackTraceFrames]
	}

	buf.WriteByte('[')
	for idx, frame := range frames {
		if idx > 0 {
			buf.WriteByte(',')
		}
		frame.WriteJSON(buf)
	}
	buf.WriteByte(']')
}

func (st stackTrace) frames() []stacktraceFrame {
	if len(st) == 0 {
		return nil
	}
	frames := runtime.CallersFrames(st) // CallersFrames is only available in Go 1.7+
	fs := make([]stacktraceFrame, 0, maxStackTraceFrames)
	var frame runtime.Frame
	more := true
	for more {
		frame, more = frames.Next()
		fs = append(fs, stacktraceFrame{
			Name: frame.Function,
			File: frame.File,
			Line: int64(frame.Line),
		})
	}
	return fs
}

// WriteJSON adds the stack trace to the buffer in the JSON form expected by the
// collector.
func (st stackTrace) WriteJSON(buf *bytes.Buffer) {
	frames := st.frames()
	writeFrames(buf, frames)
}

// MarshalJSON prepares JSON in the format expected by the collector.
func (st stackTrace) MarshalJSON() ([]byte, error) {
	estimate := 256 * len(st)
	buf := bytes.NewBuffer(make([]byte, 0, estimate))

	st.WriteJSON(buf)

	return buf.Bytes(), nil
}