witness

package module
v0.0.0-...-a908594 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2024 License: Apache-2.0 Imports: 16 Imported by: 2

README

Go Report Card Build Status Coverage Status

Witness

Enables debugging of http requests via UI. It is like chrome devtools for go backend.

How it works

It uses httptrace.WithClientTrace to make a http.RoundTripper eavesdropping on http connection. This allows detailed analysis of various http request stages. All information gathered then pushed using EventStream to UI running in browser.

Usage

The idea is to observe http client you want to debug by calling witness.DebugClient with the http client in question. For example, if you "own" a client to make http calls to some API:

// init http client with any configuration you need
cl := &http.Client{}

// pass client to witness to wrap its Transport with eavesdropping functionality
// it will also start SSE notification channel
witness.DebugClient(cl)

// then use cl as http client for making request

Alternatively, is you are interested in behaviour of some third-party http client, k8s client for example, you could eavesdrop on http client created by k8s go client code. TODO: make demo of k8s client eavesdropping

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DebugClient

func DebugClient(client *http.Client, ctx context.Context)

func InstrumentClient

func InstrumentClient(client *http.Client, n Notifier, includeBody bool)

func NewSSENotifier

func NewSSENotifier() (transport *sse)

Types

type Event

type Event struct {
	Name    string      `json:"name"`
	Payload interface{} `json:"payload"`
	Delay   int64       `json:"delay"`
}

type Notifier

type Notifier interface {
	Init(context.Context)
	Notify(RoundTripLog)
}

Notifier interface must be implemented by a transport.

var DefaultNotifier Notifier = NewSSENotifier()

type RequestError

type RequestError struct {
	Message string `json:"message"`
	Details error  `json:"details"`
}

type RequestLog

type RequestLog struct {
	Method string              `json:"method"`
	Url    string              `json:"url"`
	Query  map[string][]string `json:"query"`
	Header http.Header         `json:"header"`
	Body   string              `json:"body"`
}

type ResponseLog

type ResponseLog struct {
	Status        string      `json:"status"`
	StatusCode    int         `json:"statusCode"`
	Header        http.Header `json:"header"`
	ContentLength int64       `json:"contentLength"`
	Body          string      `json:"body"`
}

type RoundTripLog

type RoundTripLog struct {
	ID           string        `json:"id"`
	RequestLog   *RequestLog   `json:"requestLog"`
	ResponseLog  *ResponseLog  `json:"responseLog"`
	Error        *RequestError `json:"error"`
	Timeline     *Timeline     `json:"timeline"`
	Duration     string        `json:"duration"`
	DurationNano int64         `json:"durationNano"`
	Done         bool          `json:"done"`
}

type Timeline

type Timeline struct {
	StartedAt time.Time `json:"startedAt"`
	Events    []Event   `json:"events"`
}

Directories

Path Synopsis
example
k8s Module

Jump to

Keyboard shortcuts

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