Monitoring and Observability
Start with Monitoring and Observability
Learn monitoring and observability through an executable example, then connect the small demonstration to production code.
What you’ll learn
- Explain the main purpose of monitoring and observability.
- Read and run the complete Go example in this lesson.
- Apply the concept and recognize common mistakes.
Understand the idea
Observability commonly combines metrics, logs and traces. Counters reveal trends, structured logs explain discrete events, and traces connect work across request paths. Define indicators that reflect users’ experience.
The example increments an atomic request counter through an HTTP handler and exposes a simple text value.
Visualize how it works
Follow this diagram before executing the example. Check where a value or decision changes what runs next.
Tools, syntax and practical notes
For production, use standard telemetry tooling, bounded labels, useful latency histograms, consistent request IDs and alerts tied to measurable objectives. The example does not depend on a metrics backend.
Run a complete Go example
This example is a complete program. Save it as main.go or paste it into the Go Playground.
Go code
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"sync/atomic"
)
func main() {
var requests atomic.Uint64
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { requests.Add(1); fmt.Fprint(w, "ok") })
for i := 0; i < 3; i++ {
rec := httptest.NewRecorder()
handler(rec, httptest.NewRequest("GET", "/", nil))
}
fmt.Println("Requests:", requests.Load())
}
Output
Requests: 3Trace what the program does
Three handler calls each increment the atomic counter exactly once; loading the counter reports three. This simple counter illustrates one metric, not a full monitoring system.
Avoid common mistakes
- A single counter cannot reveal latency distributions or underlying causes.
- Do not label sensitive user identifiers directly on high-cardinality metrics.
Try it yourself
Count successful and failed responses separately and write tests for the counter behavior.
Tip: Edit the example above, predict the output, and run the modified code to test your reasoning.
Check your understanding
What you should remember
You have practiced Monitoring and Observability in a small, self-contained program. The important habit is to predict what runs, inspect the actual output and distinguish the language rule from the particular example.
Before continuing, make sure you can explain the program to someone who has not seen it, including the line that changes its result.
Reaching the recap and completing the quizzes are separate milestones.