Step 01 of 06
Understand the idea
A website monitor asks a server a question and records the answer. Dashboards, alerts, and certificate warnings all build on that simple check. In this lesson, you will write a small program that asks once and reports what happened.
Key technical ideas
What a check can tell you
A monitor cannot know whether a site is always up. It can answer a smaller question: Did this URL respond when I checked it, from this computer, just now? The same site may work from one place and fail from another. Beacon records the result and when the check happened.
Read the HTTP status code
An HTTP response includes a three-digit status code. Codes beginning with 2 usually mean success; 3 means the request was redirected; 4 means there is a problem with the request; and 5 means the server failed. A response alone does not mean the request succeeded, so check the code.
Tell connection errors from HTTP errors
resp, err := http.Get(url) returns a response and an error. If err is not nil, the request failed before the server sent an HTTP response, and resp is nil. Do not use resp in that case. If err is nil, check resp.StatusCode; the server may still have returned an error such as 503.
Close the response body
Go does not read the response body for you. Close it after you finish, or the program may keep connections open. In a loop, enough open connections can stop the program from making new requests. Add defer resp.Body.Close() after checking for an error.
package main
import (
"fmt"
"net/http"
)
func main() {
resp, err := http.Get("https://example.com")
if err != nil {
// Branch one: no transaction happened. resp is nil — do not touch it.
fmt.Println("unreachable:", err)
return
}
defer resp.Body.Close()
// Branches two and three: the transaction worked. Now read the verdict.
fmt.Println("status:", resp.StatusCode)
}Nine lines holding all three branches, the closed body, and a status code read rather than assumed. Type it out — don't paste it — and run it. Then break it deliberately: point it at https://not-a-real-host.invalid and watch the other branch run. A failure path you have never executed is a failure path you do not have.
The two failures, side by side
| No answer arrived | A bad answer arrived | |
|---|---|---|
What happened | The request never completed | The request completed fine |
In Go |
|
|
Typical cause | DNS, routing, refusal, timeout | Bad deploy, dependency down, bad path |
Who you'd wake up | Network or platform | Whoever owns the service |
Beacon records it as | A transport error, named by layer | A reachable endpoint, unhealthy |
Technical terms used at work
Probe — One execution of a check against one target. Beacon's unit of work — and the same word Kubernetes uses for its liveness and readiness checks, which you will meet again in course four.
Target — The thing being watched: a URL, or a host and port. One constant today, a configured list by course two.
Transport error — A failure that happened before any HTTP response existed. In Go, the non-nil error from the client.
Status code — The server's own three-digit verdict on the request it just handled.
