httpconntrace

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2024 License: GPL-3.0 Imports: 6 Imported by: 2

Documentation

Overview

Package httpconntrace provides a way to trace the local and remote endpoints used by an HTTP connection while performing an *http.Client request.

Internally, we use net/http/httptrace to collect the connection *Endpoints.

Operationally, you need to use Do where you would otherwise call *http.Client.Do method. The *Endpoints are returned along with the response.

Collecting the connection *Endpoints is important to map the HTTP response with the connection that actually serviced the request.

Example
package main

import (
	"fmt"
	"net/http"
	"net/http/httptest"

	"github.com/rbmk-project/common/httpconntrace"
)

func main() {
	// Create a test server that just echoes back
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("Hello, World!"))
	}))
	defer ts.Close()

	// Create and send request
	req, err := http.NewRequest("GET", ts.URL, nil)
	if err != nil {
		fmt.Printf("failed to create request: %s\n", err)
		return
	}

	// Use Do instead of client.Do to get connection endpoints
	resp, endpoints, err := httpconntrace.Do(http.DefaultClient, req)
	if err != nil {
		fmt.Printf("request failed: %s\n", err)
		return
	}
	defer resp.Body.Close()

	// Print the endpoints we collected
	fmt.Printf("Local: %v\n", endpoints.LocalAddr.IsValid())
	fmt.Printf("Remote: %v\n", endpoints.RemoteAddr.IsValid())

}
Output:

Local: true
Remote: true

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Endpoints

type Endpoints struct {
	// LocalAddr is the local address of the connection.
	LocalAddr netip.AddrPort

	// RemoteAddr is the remote address of the connection.
	RemoteAddr netip.AddrPort
}

Endpoints contains the connection endpoints extacted by Do.

func Do

func Do(client *http.Client, req *http.Request) (*http.Response, *Endpoints, error)

Do performs an HTTP request using *http.Client.Do and uses net/http/httptrace to extract the local and remote *Endpoints used by the connection.

Internally, this function creates a new context for tracing purposes, to avoid accidentally composing the net/http/httptrace trace with other possible context traces that may have already been present in the request context. Obviously, this means that using this function prevents one to observe connection events with a trace.

Note that this function assumes we're using TCP and casts the connection addresses to *net.TCPAddr to extract the endpoints. If the we're not using TCP, the returned [*Endpoint] will contain zero initialized (i.e., invalid) addresses.

We return *Endpoints rather than Endpoints because the structure is larger than 32 bytes and could possibly be further extended in the future to include additional fields.

Jump to

Keyboard shortcuts

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