Documentation
¶
Overview ¶
Package probe converts database, HTTP, and custom ping functions into readiness/liveness helpers. See ExampleNewPingProbe, ExampleNewHTTPProbe, and ExampleNewHTTPProbe_withOptions for quick-start patterns.
Index ¶
- type DBPinger
- type Func
- type HTTPDoer
- type HTTPProbeOption
- func WithHTTPAllowedStatuses(statuses ...int) HTTPProbeOption
- func WithHTTPClient(client HTTPDoer) HTTPProbeOption
- func WithHTTPDrainResponseBody(enabled bool) HTTPProbeOption
- func WithHTTPRequestMutator(mutator HTTPRequestMutator) HTTPProbeOption
- func WithHTTPResponseValidator(validator HTTPResponseValidator) HTTPProbeOption
- func WithHTTPStatusExpectation(expect HTTPStatusExpectation) HTTPProbeOption
- type HTTPRequestMutator
- type HTTPResponseValidator
- type HTTPStatusExpectation
- type MongoPinger
- type PingFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Func ¶
Func represents a health check that returns an error when the resource is unavailable.
func NewDBPingProbe ¶
NewDBPingProbe creates a Func that pings databases such as PostgreSQL using the provided client.
Example ¶
package main
import (
"context"
"fmt"
"github.com/drblury/apiweaver/probe"
)
type stubDB struct {
err error
}
func (s *stubDB) PingContext(ctx context.Context) error {
return s.err
}
func main() {
probeFunc := probe.NewDBPingProbe("postgres", &stubDB{})
fmt.Println(probeFunc(context.Background()))
}
Output: <nil>
func NewHTTPProbe ¶
func NewHTTPProbe(name, method, target string, client HTTPDoer, opts ...HTTPProbeOption) Func
NewHTTPProbe creates a Func that performs an HTTP request against the supplied endpoint. The probe succeeds when the response status code is within the 2xx range.
Example ¶
package main
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"github.com/drblury/apiweaver/probe"
)
func main() {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "ok")
}))
defer server.Close()
probeFunc := probe.NewHTTPProbe("docs", http.MethodGet, server.URL, server.Client())
fmt.Println(probeFunc(context.Background()))
}
Output: <nil>
Example (DefaultClient) ¶
package main
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"github.com/drblury/apiweaver/probe"
)
func main() {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
defer ts.Close()
probeFunc := probe.NewHTTPProbe("docs", http.MethodGet, ts.URL, nil)
fmt.Println(probeFunc(context.Background()))
}
Output: <nil>
Example (WithOptions) ¶
package main
import (
"context"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"github.com/drblury/apiweaver/probe"
)
func main() {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Authorization") != "Bearer demo" {
w.WriteHeader(http.StatusUnauthorized)
return
}
w.Header().Set("X-Version", "123")
w.WriteHeader(http.StatusAccepted)
}))
defer server.Close()
probeFunc := probe.NewHTTPProbe(
"docs",
http.MethodGet,
server.URL,
nil,
probe.WithHTTPRequestMutator(func(req *http.Request) error {
req.Header.Set("Authorization", "Bearer demo")
return nil
}),
probe.WithHTTPAllowedStatuses(http.StatusAccepted),
probe.WithHTTPResponseValidator(func(resp *http.Response) error {
if resp.Header.Get("X-Version") == "" {
return errors.New("missing version header")
}
return nil
}),
)
fmt.Println(probeFunc(context.Background()))
}
Output: <nil>
func NewMongoPingProbe ¶
func NewMongoPingProbe(client MongoPinger, readPref *readpref.ReadPref) Func
NewMongoPingProbe creates a Func that pings MongoDB using the provided client. If readPref is nil it defaults to readpref.Primary.
func NewPingProbe ¶
NewPingProbe wraps a PingFunc with standardised error handling suitable for InfoHandler probes.
Example ¶
package main
import (
"context"
"fmt"
"github.com/drblury/apiweaver/probe"
)
func main() {
probeFunc := probe.NewPingProbe("noop", func(ctx context.Context) error {
return nil
})
fmt.Println(probeFunc(context.Background()))
}
Output: <nil>
type HTTPProbeOption ¶ added in v0.2.0
type HTTPProbeOption func(*httpProbeConfig)
HTTPProbeOption configures the behaviour of NewHTTPProbe.
func WithHTTPAllowedStatuses ¶ added in v0.2.0
func WithHTTPAllowedStatuses(statuses ...int) HTTPProbeOption
WithHTTPAllowedStatuses restricts the probe to succeed only for the provided status codes.
func WithHTTPClient ¶ added in v0.2.0
func WithHTTPClient(client HTTPDoer) HTTPProbeOption
WithHTTPClient overrides the HTTP client used for the probe.
func WithHTTPDrainResponseBody ¶ added in v0.2.0
func WithHTTPDrainResponseBody(enabled bool) HTTPProbeOption
WithHTTPDrainResponseBody toggles draining of the response body after validation.
func WithHTTPRequestMutator ¶ added in v0.2.0
func WithHTTPRequestMutator(mutator HTTPRequestMutator) HTTPProbeOption
WithHTTPRequestMutator registers a mutator that runs before the request is dispatched.
func WithHTTPResponseValidator ¶ added in v0.2.0
func WithHTTPResponseValidator(validator HTTPResponseValidator) HTTPProbeOption
WithHTTPResponseValidator registers a validator that runs after a response is received.
func WithHTTPStatusExpectation ¶ added in v0.2.0
func WithHTTPStatusExpectation(expect HTTPStatusExpectation) HTTPProbeOption
WithHTTPStatusExpectation installs a custom status validation function.
type HTTPRequestMutator ¶ added in v0.2.0
HTTPRequestMutator allows callers to tweak the outbound request prior to dispatch.
type HTTPResponseValidator ¶ added in v0.2.0
HTTPResponseValidator inspects the received response and can veto the probe.
type HTTPStatusExpectation ¶ added in v0.2.0
HTTPStatusExpectation determines whether a given HTTP status code is acceptable.
type MongoPinger ¶
MongoPinger captures the subset of the MongoDB client used for readiness checks.