Documentation ¶
Overview ¶
Detective is a distributed application health monitoring library.
Detective allows you to simultaneously, and independently monitor the health of any application along with its dependencies.
Using Detective ¶
A Detective instance represents a group of dependencies. A dependency can be anything that the application depends on, and which can return some sort of error. For example, a database whose connection time out, or an HTTP web service that doesn't respond.
// Create a new detective instance, and add a dependency d := detective.New("application") dep := d.Dependency("database") // Register a function to detect a fault in the dependency dep.Detect(func() error { // `db` can be an instance of sql.DB err := db.Ping() return err }) // Create a ping endpoint which checks the health of all dependencies // A Detective instance implements the http.Handler interface http.ListenAndServe(":8080", d)
The ping endpoint will check the health of all dependencies by calling their detector function registered in the "Detect" method
Composing instances ¶
The endpoint that was defined in the previous example can itself be registered in other detective instances:
d2 := detective.New("dependent_application") // The ping handler defined in the last example can be registered as an endpoint d2.Endpoint("http://localhost:8080/") // The ping handler of `d2` will now call `d`s ping handler and as a result, monitor `d`s dependencies as well http.ListenAndServe(":8081", d2)
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Dependency ¶
type Dependency struct {
// contains filtered or unexported fields
}
The Dependency type represents a detectable unit. The function provided in the Detect method will be called to monitor the state of the dependency
func (*Dependency) Detect ¶
func (d *Dependency) Detect(df DetectorFunc)
Detect registers a function that will be called to detect the health of a dependency. If the dependency is healthy, a nil value should be returned as the error.
type Detective ¶
type Detective struct {
// contains filtered or unexported fields
}
A Detective instance manages registered dependencies and endpoints. Dependencies can be registered with an instance. Each instance has a state which represents the health of its components.
func New ¶
New creates a new Detective instance. To avoid confusion, the name provided should preferably be unique among dependent detective instances.
func (*Detective) Dependency ¶
func (d *Detective) Dependency(name string) *Dependency
Dependency adds a new dependency to the Detective instance. The name provided should preferably be unique among dependencies registered within the same detective instance.
func (*Detective) Endpoint ¶
Endpoint adds an HTTP endpoint as a dependency to the Detective instance, thereby allowing you to compose detective instances. This method creates a GET request to the provided url. If you want to customize the request (like using a different HTTP method, or adding headers), consider using the EndpointReq method instead.
func (*Detective) EndpointReq ¶
EndpointReq is similar to Endpoint, but takes an HTTP request object instead of a URL. Use this method if you want to customize the request to the ping handler of another detective instance.
func (*Detective) ServeHTTP ¶
func (d *Detective) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP is the HTTP handler function for getting the state of the Detective instance
func (*Detective) WithHTTPClient ¶
WithHTTPClient sets the HTTP Client to be used while hitting the endpoint of another detective HTTP ping handler.
type DetectorFunc ¶
type DetectorFunc func() error
The DetectorFunc type represents the function signature to check the health of a dependency
type State ¶
type State struct { Name string `json:"name"` Ok bool `json:"active"` Status string `json:"status"` Latency time.Duration `json:"latency"` Dependencies []State `json:"dependencies,omitempty"` }
State describes the current status of an entity. This entity can be a Dependency, or a Detective instance. A State can contain other States as well.