Go API Documentation

github.com/TykTechnologies/tyk/internal/debug2

No package summary is available.

Package

Files: 1. Third party imports: 0. Imports from organisation: 0. Tests: 0. Benchmarks: 0.

Vars

var headerMatchRe = regexp.MustCompile(`^[0-9]+ @ 0x.*`)

Types

Record

Record captures goroutine states

Field name Field type Comment
buffer

*bytes.Buffer

No comment on field.
ignores

[]string

No comment on field.
type Record struct {
	buffer	*bytes.Buffer
	ignores	[]string
}

Functions

func NewRecord

NewRecord creates a new Record and populates it with the current goroutine dump.

func NewRecord() *Record {
	result := &Record{
		buffer: bytes.NewBuffer([]byte{}),
	}

	pprof.Lookup("goroutine").WriteTo(result.buffer, 1)

	result.SetIgnores([]string{
		"runtime/pprof.writeRuntimeProfile",
	})
	return result
}

Cognitive complexity: 3, Cyclomatic complexity: 1

Uses: bytes.NewBuffer, pprof.Lookup.

func (*Record) Count

Count returns the number of unique goroutines in the Record.

func (r *Record) Count() int {
	return len(r.parseGoroutines())
}

Cognitive complexity: 0, Cyclomatic complexity: 1

func (*Record) SetIgnores

func (r *Record) SetIgnores(ignores []string) {
	r.ignores = ignores
}

Cognitive complexity: 0, Cyclomatic complexity: 1

func (*Record) Since

Since compares the current Record with another Record and returns a new Record containing only the goroutines found in the current Record but not in the last.

func (r *Record) Since(last *Record) *Record {
	currentGoroutines := r.parseGoroutines()
	lastGoroutines := last.parseGoroutines()

	diffBuffer := bytes.NewBuffer([]byte{})
	for header, stack := range currentGoroutines {
		if _, exists := lastGoroutines[header]; !exists {
			diffBuffer.WriteString(header + "\n")
			for _, line := range stack {
				diffBuffer.WriteString(line + "\n")
			}
		}
	}

	return &Record{
		buffer: diffBuffer,
	}
}

Cognitive complexity: 10, Cyclomatic complexity: 4

Uses: bytes.NewBuffer.

func (*Record) String

String implements the fmt.Stringer interface, providing a formatted view of the goroutines in the Record.

func (r *Record) String() string {
	goroutines := r.parseGoroutines()
	var builder strings.Builder
	builder.WriteString(fmt.Sprintf("Number of goroutines: %d\n", len(goroutines)))
	for header, stack := range goroutines {
		builder.WriteString("--- Goroutine ---\n")
		builder.WriteString(header + "\n")
		for _, line := range stack {
			builder.WriteString(line + "\n")
		}
	}
	return builder.String()
}

Cognitive complexity: 6, Cyclomatic complexity: 3

Uses: fmt.Sprintf, strings.Builder.

Private functions

func parseGoroutines

parseGoroutines parses goroutines from the buffer into a map where each key is a goroutine header and the value is its stack trace as a slice of strings.

parseGoroutines () map[string][]string
References: strings.Contains, strings.Split.


Tests

Files: 1. Third party imports: 1. Imports from organisation: 0. Tests: 1. Benchmarks: 1.

Test functions

TestNewRecordWithGoroutines

References: assert.Equal, assert.NoError, context.Background, context.WithTimeout, debug2.NewRecord, fmt.Print, runtime.GC, time.Millisecond, time.Second, time.Sleep.

Benchmark functions

BenchmarkNewRecordWithGoroutines

References: assert.Equal, assert.Greater, debug2.NewRecord, runtime.GC, sync.WaitGroup, time.Millisecond, time.Sleep.