Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/posener/client-timing
An HTTP client for go-server-timing middleware. Enables automatic timing propagation through HTTP calls between servers.
https://github.com/posener/client-timing
client go golang http http-timing timing
Last synced: 2 months ago
JSON representation
An HTTP client for go-server-timing middleware. Enables automatic timing propagation through HTTP calls between servers.
- Host: GitHub
- URL: https://github.com/posener/client-timing
- Owner: posener
- License: mit
- Created: 2018-02-23T01:52:45.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2020-03-13T18:47:59.000Z (almost 5 years ago)
- Last Synced: 2024-10-04T13:04:22.390Z (3 months ago)
- Topics: client, go, golang, http, http-timing, timing
- Language: Go
- Homepage:
- Size: 15.6 KB
- Stars: 24
- Watchers: 2
- Forks: 6
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
- awesome-go - client-timing - An HTTP client for go-server-timing middleware. - ★ 10 (Web Frameworks)
- awesome-go-extra - client-timing - server-timing middleware. Enables automatic timing propagation through HTTP calls between servers.|20|6|1|2018-02-23T01:52:45Z|2020-03-13T18:47:59Z| (Web Frameworks / Fail injection)
README
# client-timing
[![Build Status](https://travis-ci.org/posener/client-timing.svg?branch=master)](https://travis-ci.org/posener/client-timing)
[![codecov](https://codecov.io/gh/posener/client-timing/branch/master/graph/badge.svg)](https://codecov.io/gh/posener/client-timing)
[![GoDoc](https://godoc.org/github.com/posener/client-timing?status.svg)](http://godoc.org/github.com/posener/client-timing)
[![Go Report Card](https://goreportcard.com/badge/github.com/posener/client-timing)](https://goreportcard.com/report/github.com/posener/client-timing)An HTTP client for [go-server-timing](https://github.com/mitchellh/go-server-timing) middleware.
## Features:
* An HTTP `Client` or `RoundTripper`, fully compatible with Go's standard library.
* Automatically time HTTP requests sent from an HTTP handler.
* Collects all timing headers from upstream servers.
* Customize timing headers according to the request, response and error of the HTTP round trip.## Install
`go get -u github.com/posener/client-timing`
## Usage
1. Add a `*clienttiming.Timer` to your server handler, or create it in the handler function itself.
2. Wrap the `http.Handler` with [`servertiming.Middleware`](https://godoc.org/github.com/mitchellh/go-server-timing#Middleware).
2. In the handler function, having `timer` of type `*clienttiming.Timer` and `req` is the `*http.Request`:a. Create an [`*http.Client`](https://godoc.org/net/http#Client) using `timer.Client(req.Context())`
b. Or create an [`http.RoundTripper`](https://godoc.org/net/http#RoundTripper) using `timer.Transport(req.Context())`
3. Use option a or b directly or inject it to a library that accepts them, in your outgoing HTTP request
from the handler.
4. That is it! the timing header will appear in the response from the handler.## Example
Suppose we have an HTTP handler:
```go
type handler struct {
timer *clienttiming.Timer
}
```Our usage of that handler will be:
```go
func main() {
h := &handler{
timer: clienttiming.New(clienttiming.WithName("my-server")),
}
log.Fatal(http.ListenAndServe(":8080", servertiming.Middleware(h)))
}
```### Example for `Client` function:
```go
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Create an http client using the request context
c := h.timer.Client(r.Context())// Perform HTTP requests, as many as you like
resp, err := c.Get("https://golang.org/")...
}
```### Example for `Transport` function:
```go
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Instrument an http client with a timing transport
c := &http.Client{
Transport: h.timer.Transport(r.Context()),
}// Perform HTTP requests, as many as you like
resp, err := c.Get("https://golang.org/")...
}
```### Run the [example](./example/main.go)
`go run ./example/main.go`