gotrace

package module
v0.2.2-0...-80432f9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 17, 2021 License: MIT Imports: 10 Imported by: 0

README

Overview

This lib provides useful functions to handle runtime goroutine stack. Such as wait for goroutines to exit.

Check the godoc for usage.

Here is the example to use it with standard Go testing: link.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Signal

func Signal(signals ...os.Signal) context.Context

Signal to cancel the returned context, default signal is CTRL+C .

Example
package main

import (
	"fmt"
	"os"
	"runtime"
	"strings"
	"time"

	"github.com/ysmood/gotrace"
)

func main() {
	// Skip the test for Windows because it can't send signal programatically.
	if runtime.GOOS == "windows" {
		fmt.Println("true")
		return
	}

	go func() {
		traces := gotrace.Wait(gotrace.Signal())
		fmt.Println(strings.Contains(traces.String(), "gotrace_test.ExampleSignal"))
	}()

	time.Sleep(100 * time.Millisecond)

	p, _ := os.FindProcess(os.Getpid())
	_ = p.Signal(os.Interrupt)

	time.Sleep(100 * time.Millisecond)

}
Output:

true

func Timeout

func Timeout(d time.Duration) context.Context

Timeout shortcut for context.WithTimeout(context.Background(), d)

Types

type Ignore

type Ignore func(*Trace) bool

Ignore filter

func CombineIgnores

func CombineIgnores(list ...Ignore) Ignore

CombineIgnores into one

Example
package main

import (
	"context"
	"fmt"
	"strings"
	"time"

	"github.com/ysmood/gotrace"
)

func main() {
	ignore := gotrace.CombineIgnores(
		gotrace.IgnoreCurrent(),
		func(t *gotrace.Trace) bool {
			return strings.Contains(t.Raw, "ExampleCombineIgnores.func2")
		},
	)

	go func() {
		time.Sleep(2 * time.Second)
	}()

	go func() {
		time.Sleep(time.Second)
	}()

	start := time.Now()
	gotrace.Wait(context.TODO(), ignore)
	end := time.Since(start)

	if time.Second < end && end < 2*time.Second {
		fmt.Println("only waits for the second goroutine")
	}

}
Output:

only waits for the second goroutine

func IgnoreCurrent

func IgnoreCurrent() Ignore

IgnoreCurrent Trace list

Example
package main

import (
	"context"
	"fmt"
	"time"

	"github.com/ysmood/gotrace"
)

func main() {
	ignore := gotrace.IgnoreCurrent()

	go func() {
		time.Sleep(time.Second)
	}()

	start := time.Now()
	gotrace.Wait(context.TODO(), ignore)
	end := time.Since(start)

	if end > time.Second {
		fmt.Println("waited for 1 second")
	}

}
Output:

waited for 1 second

func IgnoreFuncs

func IgnoreFuncs(names ...string) Ignore

IgnoreFuncs ignores a Trace if it's first Stack's Func equals one of the names.

Example
package main

import (
	"context"
	"fmt"
	"time"

	"github.com/ysmood/gotrace"
)

func main() {
	ignoreCurrent := gotrace.IgnoreCurrent()
	ignore := gotrace.IgnoreFuncs("internal/poll.runtime_pollWait")

	go func() {
		time.Sleep(time.Second)
	}()

	start := time.Now()
	gotrace.Wait(context.TODO(), ignore, ignoreCurrent)
	end := time.Since(start)

	if end > time.Second {
		fmt.Println("waited for 1 second")
	}

}
Output:

waited for 1 second

func IgnoreList

func IgnoreList(list Traces) Ignore

IgnoreList of Trace

type Stack

type Stack struct {
	Func string
	Loc  string
}

Stack infoo

type Trace

type Trace struct {
	Raw         string
	GoroutineID int64
	WaitReason  string // https://github.com/golang/go/blob/874b3132a84cf76da6a48978826c04c380a37a50/src/runtime/runtime2.go#L997
	Stacks      []Stack
	// contains filtered or unexported fields
}

Trace of one goroutine

func (Trace) String

func (t Trace) String() string

String interface for fmt

type Traces

type Traces []*Trace

Traces of goroutines

func Get

func Get(all bool) Traces

Get the Trace of the calling goroutine. If all is true, all other goroutines' Traces will be appended into the result too.

Example
package main

import (
	"fmt"

	"github.com/ysmood/gotrace"
)

func main() {
	list := gotrace.Get(true)

	fmt.Println("goroutine count:", len(list))
	fmt.Println("id of current:", list[0].GoroutineID)
	fmt.Println("caller of current:", list[0].Stacks[2].Func)

}
Output:


goroutine count: 2
id of current: 1
caller of current: github.com/ysmood/gotrace_test.ExampleGet()

func Wait

func Wait(ctx context.Context, ignores ...Ignore) (remain Traces)

Wait for other goroutines that are not ignored to exit. It returns the ones that are still active. It keeps counting the active goroutines that are not ignored, if the number is zero return.

func (Traces) Any

func (list Traces) Any() bool

Any item exists in the list

func (Traces) String

func (list Traces) String() string

String interface for fmt. It will merge similar trace together and print counts.

Example
package main

import (
	"fmt"
	"strings"
	"time"

	"github.com/ysmood/gotrace"
)

func main() {
	go func() {
		time.Sleep(time.Second)
	}()

	traces := gotrace.Wait(gotrace.Timeout(0))

	str := fmt.Sprintf("%v %v", traces[0], traces)

	fmt.Println(strings.Contains(str, "gotrace_test.ExampleTraces_String"))

}
Output:

true

Directories

Path Synopsis
pkg

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL