KubFly SDK for Go / nethttp
package nethttp
import "kubfly.io/sdk/nethttp"
Package nethttp is the integration of net/http: a Handler for the server and a RoundTripper for the client. Each request is a frame of a scope, the metrics of its route and an edge of the stream traffic; the headers of KubFly carry the caller to the server and the identity of the processes both ways — the kubfly-sdk-http module of the Java SDK with the filters of its frameworks.
mux := http.NewServeMux()
mux.HandleFunc("GET /orders/{id}", order)
server := &http.Server{Handler: nethttp.Handler(mux)}
client := &http.Client{Transport: nethttp.Transport(http.DefaultTransport)}
The server:
- a frame of sdk/http/server/nethttp/request with the tags method and uri (the path, without the query), the caller of the request its caller (KubFly-Frame, traceparent, tracestate; KUBFLY_TRACE_ACCEPT);
- the metrics sdk/http/server/<METHOD>/<route>/<measure>: duration (ns), 1xx … 5xx, errors (a panic of the
handler), cancelled (the client left before the answer). The route is the pattern of http.ServeMux without its method and host: "GET /orders/{id}" — orders.{id}, "/" — root; a request no pattern took — unmatched;
- the edge of the stream traffic: the caller of KubFly with the names of its KubFly-Peer, else one external peer; a caller of KubFly gets KubFly-Frame and KubFly-Peer of this process in the answer.
The client:
- a frame of sdk/http/client/nethttp/request with the tags method, host and uri; the headers of the frame go to the server (KUBFLY_TRACE_SEND, KUBFLY_TRACE_EXCLUDE), KubFly-Peer — to a server that answered with its own;
- the metrics sdk/http/client/<host>/<METHOD>:<path>/<measure>, the host in lower case with its port: at most KUBFLY_HTTP_CLIENT_HOSTS hosts (the rest — other) and KUBFLY_HTTP_CLIENT_ROUTES routes of a host (the rest — <METHOD>:other); errors — no answer, cancelled — the context of the request ended;
- the edge: a server of KubFly by the headers of its answer, else the host; a request without an answer goes to the server that answered last from that host.
Constants
const ( // ClientHostsVariable is the most hosts of the metrics of the client: the rest are other. ClientHostsVariable = "KUBFLY_HTTP_CLIENT_HOSTS" // DefaultClientHosts is the most hosts by default. DefaultClientHosts = 100 // ClientRoutesVariable is the most routes of one host: the rest are <METHOD>:other. ClientRoutesVariable = "KUBFLY_HTTP_CLIENT_ROUTES" // DefaultClientRoutes is the most routes of a host by default. DefaultClientRoutes = 100 )
The variables of the integration, with their defaults.
const ServerThread = "http"
ServerThread is the name of the thread of each request: a goroutine has no name, the work of a request is named here, with a number of its own per request — in the frame of the request itself (api.Scope.OnThread).
Variables
var Config = registerConfig(configOf(api.Config.Vars))
Config is the configuration of the integration.
Functions
func ClientHost
func ClientHost(target *url.URL) string
ClientHost is the host of a request in lower case, with its port if it has one: the level of the metrics of its routes, the address of its peer; unknown without one.
func ClientPath
func ClientPath(target *url.URL) string
ClientPath is the path of a request, as it went, without the query — tokens in it never reach a journal; / for none.
func Handler
func Handler(next http.Handler) http.Handler
Handler is a handler that serves each request of the next one in a frame of sdk/http/server/nethttp/request, with the caller of the request as its caller, on a thread of its own (ServerThread), and records the metrics of its route and its edge of the stream traffic. The route is the pattern http.ServeMux matched (http.Request.Pattern): wrap the mux, not a handler of one route.
func ServerAnswer
func ServerAnswer(caller *api.Caller, frame *api.Frame, set func(name string, value string))
ServerAnswer puts KubFly-Frame and KubFly-Peer of the frame that serves a request into its answer, when its caller is of KubFly (api.PeerAnswers) — for a server of another library.
func ServerPeer
func ServerPeer(caller *api.Caller, header func(name string) string) api.PeerSeen
ServerPeer is the peer of an incoming request by its caller and its headers: the caller of KubFly with the names of its KubFly-Peer, else api.PeerExternal — for a server of another library.
func Transport
func Transport(next http.RoundTripper) http.RoundTripper
Transport is a RoundTripper that sends each request with the next one (nil — http.DefaultTransport) in a frame of sdk/http/client/nethttp/request, the headers of its frame and the identity of the process on it, and records the metrics of its route and its edge of the stream traffic. The request of the caller is never changed: one with headers is a clone.
Types
type Configuration
type Configuration struct {
// ClientHosts is the most hosts of the metrics of the client (KUBFLY_HTTP_CLIENT_HOSTS, 100).
ClientHosts *api.Param[int]
// ClientRoutes is the most routes of one host (KUBFLY_HTTP_CLIENT_ROUTES, 100).
ClientRoutes *api.Param[int]
// Problems are the wrong values that fell back to their defaults.
Problems []string
}
Configuration is the configuration of the integration, taken once from api.Config.Vars when the package loads.