nettrace

package module
v0.0.0-...-6c5b6f3 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2020 License: MIT Imports: 4 Imported by: 0

README

nettrace

License GoDoc travis Go Report Card

There is nettrace in go source, but unfortunately it is internal has not been explored and be used.

httptrace uses nettrace to trace dns lookup and connecting, so we wrap it as a higher nettrace.

Finally the solution is like below:

our nettrace --> httptrace --> internal trace.

How to use it?

   var result = make(map[string]string)
   
   // create the nettrace instance and set its hooks
   trace := &ClientTrace{
   	DNSStart: func(name string) {
   		result["DNSStart"] = name
   	},
   	DNSDone: func(netIPs []net.IPAddr, coalesced bool, err error) {
   		var ips []string
   		for _, ip := range netIPs {
   			ips = append(ips, ip.String())
   		}
   		result["DNSDone"] = strings.Join(ips, ",")
   	},
   	ConnectStart: func(network, addr string) {
   		result["ConnectStart"] = network + "@" + addr
   	},
   	ConnectDone: func(network, addr string, err error) {
   		result["ConnectDone"] = fmt.Sprintf("%s@%s@%v", network, addr, err)
   	},
   }

   // create a context to use this trace
   ctx := WithClientTrace(context.Background(), trace)

   d := net.Dialer{Timeout: 5 * time.Second}
   // dial with this context
   conn, err := d.DialContext(ctx, "tcp", "www.baidu.com:80")
   if err != nil {
   	t.Fatal(err)
   }
   conn.Close()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithClientTrace

func WithClientTrace(ctx context.Context, trace *ClientTrace) context.Context

WithClientTrace returns a new context based on the provided parent ctx. HTTP client requests made with the returned context will use the provided trace hooks, in addition to any previous hooks registered with ctx. Any hooks defined in the provided trace will be called first.

Types

type ClientTrace

type ClientTrace struct {
	// DNSStart is called with the hostname of a DNS lookup
	// before it begins.
	DNSStart func(name string)

	// DNSDone is called after a DNS lookup completes (or fails).
	// The coalesced parameter is whether singleflight de-dupped
	// the call. The addrs are of type net.IPAddr but can't
	// actually be for circular dependency reasons.
	DNSDone func(netIPs []net.IPAddr, coalesced bool, err error)

	// ConnectStart is called before a Dial, excluding Dials made
	// during DNS lookups. In the case of DualStack (Happy Eyeballs)
	// dialing, this may be called multiple times, from multiple
	// goroutines.
	ConnectStart func(network, addr string)

	// ConnectStart is called after a Dial with the results, excluding
	// Dials made during DNS lookups. It may also be called multiple
	// times, like ConnectStart.
	ConnectDone func(network, addr string, err error)
}

ClientTrace contains a set of hooks for tracing events within the net package. Any specific hook may be nil.

func ContextClientTrace

func ContextClientTrace(ctx context.Context) *ClientTrace

ContextClientTrace returns the ClientTrace associated with the provided context. If none, it returns nil.

type ClientTraceKey

type ClientTraceKey struct{}

ClientTraceKey is a context.Context Value key. Its associated value should be a *Trace struct.

Jump to

Keyboard shortcuts

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