Adding observability to our project
For simplicity, we will use slog for logging, Prometheus for metrics, and OTel for tracing. This way, we will explore multiple approaches and add all the observability tools we have seen in this chapter. However, it is up to you to select the tools you consider the best option for your project; for example, you could use OTel for everything.
So, let’s start adding some logging to our application.
Logging
One of the typical cases where we want to have logging is whenever there is an error, so let’s add some logging to our handleCreateList function:
func handleCreateList(w http.ResponseWriter, r *http.Request) {
var list ShoppingList
defer r.Body.Close()
err := json.NewDecoder(r.Body).Decode(&list)
if err != nil {
slog.Info("Invalid request body", slog.Any("error", err))
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
newList, err := repository.AddShoppingList...