Skip to content
ImagineWorld

Observability Through Logging

node_idnode-book_rust-web-programming_chapter-13-observability-through-logging
node_typechapter-node
schema_profileworld.chapter_node.v1
statusactive
entropyhigh
manifoldworld
domainimagineworld.net
authorityinforms
layercorpus
chapter_num13
chapter_titleObservability Through Logging
pillar_membershipJ — architecture_design_craft
governed_byCORPUS
constitutional_citationCONSTITUTION
local graph

Chapter 13 — Observability Through Logging

Section titled “Chapter 13 — Observability Through Logging”

We now have a working to-do application that can either run as a single binary or run as multiple servers. However, we do not actually know what is going on inside our system. Let us say that our system makes a request from one server to another. How do we know that this request was made and what the response was? We don’t. We can try and work out what happened from the error message returned to the frontend, but this might not be clear. We also might not want to expose intricate details of the error to the frontend. To remedy this, we can produce logs of these requests and how the request travels through the system. This also gives us the power to inspect the steps that lead up to the error. Logging also enables us to keep an eye on the health of the system. In this chapter, we will cover the following:

  • What are RESTful services?
  • What is logging?

By the end of this chapter, you will be able to log what is going on in your program, including all the request and response code, by implementing middleware for our logger. You will also learn how to create background tasks that receive log messages from the main application and persist them to a database asynchronously, reducing the workload and latency of the main request-handling path. Finally, we can perform queries on our Elasticsearch database to look for logs.

You can download the example project and code for this book by following the instructions in the Download the example code files section in the Preface of this book.

This chapter’s code files are included in the downloadable code bundle.

Representational state transfer (REST) is an architectural style for our application programming interface (API) to read (GET), update (PUT), create (POST), and delete (DELETE) our users and to-do items. The goal of a RESTful approach is to increase speed/performance, reliability, and the ability to grow by reusing components that can be managed and updated without affecting the system. You may have noticed that before Rust, slow, high-level languages seemed to be a wise choice for web development. This is because they are quicker and safer to write. This is due to the main bottleneck for the speed of processing data in web development being the network connection speed. The RESTful design aims to improve the speed by economizing the system, such as reducing API calls, as opposed to just focusing on algorithm speed. With that in mind, in this section, we can explore the following RESTful concepts:

  • Layered system: This enables us to add extra functionality, such as authorization, without having to change the interface.
  • Uniform system: This simplifies and decouples the architecture, enabling whole parts of the application to evolve independently without clashing.
  • Statelessness: This ensures that our application does not directly save anything on the server. This has implications for microservices and cloud computing.
  • Logging: This enables us to peek into our application and see how it runs, exposing undesirable behavior even if there are no errors displayed.
  • Caching: This enables us to store data in hot memory to reduce the number of calls to a database.
  • Code on demand: This is where our backend server directly runs code on the frontend.

Here, we can see that we have been implementing all the previous concepts as we have gone along when building our to-do app, apart from the code-on-command concept and logging. We can start with code on command.

Code on demand is where the backend server directly executes code on the frontend. This constraint is optional and not widely used. However, it can be useful as it gives the backend server the right to decide when code is executed on the frontend. Getting JavaScript to run in the browser from our server when the API endpoint is called can be achieved by sending a script in the HTML that we return. We can do this with the following code:

</html>
)
}

Here, we can see that we are removing the user token from localStorage and then refreshing the window afterward to be rerouted to the login form. We now must add our logout API endpoint to our factory with the following code:

nanoservices/auth/networking/actix_server/
// src/auth/mod.rs
...
pub mod logout;
...
pub fn auth_factory(app: &mut ServiceConfig) {
app.service(
scope("/api/v1/auth")
...
.route("logout", get().to(
logout::logout)
)
);
}

This is the easiest way to force a logout, as the frontend browser does not have access to the JSON Web Token (JWT) and therefore cannot make any more authenticated HTTP requests.

While our running code in the browser does work, it will leave a dangling entry in our cache. If we want to remove the user session from the cache, we will have to make an HTTP request with the user ID. At this stage in the book, you should be able to add this API endpoint. Covering the addition of this API endpoint at this stage in the book will merely bloat the chapter. If you choose not to add this endpoint, your to-do application will work okay for the rest of the book, but the memory consumption of the cache will grow over time.

Now that we have covered code on command, we can move on to the focus of this chapter, which is observability. We will start this topic with a basic terminal logger. But first, we should explore what logging is.

So far, our application does not log anything. This does not directly affect the running of the app. However, there are some advantages to logging. Logging enables us to debug our applications. Right now, as we are developing locally, it may not seem like logging is really needed. However, in a production environment, there are many reasons why an application can fail, including Docker container orchestration issues. Logs that note what processes have happened can help us pinpoint an error. We can also use logging to see when edge cases and errors arise, so that we can monitor the general health of our application. When it comes to logging, there are four types of logs that we can build: Observability Through Logging

  • Informational (info): This is general logging. If we want to track a general process and how it is progressing, we use this type of log. Examples of using this are starting and stopping the server and logging certain checkpoints that we want to monitor, such as HTTP requests.

  • Verbose: This is information such as the type defined in the previous point. However, it is more granular to inform us of a more detailed flow of a process. This type of log is mainly used for debugging purposes and should generally be avoided when it comes to production settings.

  • Warning: We use this type when we are logging a process that is failing and should not be ignored. However, we can use this instead of raising an error because we do not want the service to be interrupted or the user to be aware of the specific error. The logs themselves are for us to be alerted to the problem, to allow us to then act. Problems such as calls to another server failing are appropriate for this category.

  • Error: This is where the process is interrupted due to an error, and we need to sort it out as quickly as possible. We also need to inform the user that the transaction did not go through. A good example of this is a failure to connect or insert data into a database. If this happens, there is no record of the transaction happening, and it cannot be solved retroactively. However, it should be noted that the process can continue running.

If a warning comes up about the server failing to send an email, connect to another server to dispatch a product for shipping, and so on. Once we have sorted out the problem, we can retroactively make a database call to transactions in this timeframe and make the calls to the server with the right information.

In the worst case, there will be a delay. With the error type, we will not be able to make the database call, as the server was interrupted by an error before the order was even entered in the database. Considering this, it is clear why error logging is highly critical, as the user needs to be informed that there is a problem and their transaction did not go through, prompting them to try again later.

We could consider the option of including enough information in the error logs to retroactively go back and update the database and complete the rest of the process when the issue is resolved, removing the need to inform the user. While this is tempting, we must consider two things. Log data is generally unstructured.

There is no quality control for what goes into a log. Therefore, once we have finally managed to manipulate the log data into the right format, there is still a chance that corrupt data could find its way into the database.

The second issue is that logs are not considered secure. They get copied and sent to other developers in a crisis, and they can be plugged into other pipelines and websites, such as BugSnag, to monitor logs. Considering the nature of logs, it is not good practice to have any identifiable information in a log.

Now that we are all juiced up knowing that logs rock, we can start our logging journey by building a basic logger that writes to the terminal.

Considering our system has multiple servers, and these servers might be running by themselves, or all compiled into one binary in the ingress. Considering the flexibility of our system, we must define a logger in one place and import this logger into any service that wants to log anything. We can supply all our servers with a logger via the glue workspace with the following directory layout:

| ├─ Cargo.toml | | | └─ src | | | ├─ errors.rs | | | ├─ lib.rs | | | ├─ logger | | | │ ├─ logger.rs | | | │ ├─ mod.rs | | | │ └─ network_wrappers | | | │ │ ├─ actix_web.rs | | | │ │ └─ mod.rs | | | └─ token.rs | |

To integrate our logger, we must carry out the following steps:

  1. Defining a logger
  2. Creating a logging middleware
  3. Integrating our logger into our servers

We can start by building out a basic logger.

Before we write any code, we must have the following dependencies in our glue workspace:

## glue/Cargo.toml
...
[dependencies]
...
tracing = "0.1.4"
tracing-subscriber = "0.3.18"
futures-util = "0.3.30"

We will be using the tracing crate to define some logging functions, and tracing-subscriber to define a logger that will subscribe to the log events being produced. Our basic logger is just printing to the terminal. Why are we not just using println!? If we use println!(“hello world”), we are essentially running the following code:

use std::io::{stdout, Write};
let mut lock = stdout().lock();
write!(lock, "hello world").unwrap();

Here, we can see that we are locking the standard output and writing to it. This makes sense as printing to the terminal would not be very useful if half of one message got printed alongside half of another message. This lock mechanism results in a reduction in performance. For instance, it is good practice to log every request that is sent to the server. If we log our requests using println!, even if we have four threads processing requests, each thread processing a request would have to wait its turn to acquire the lock, essentially holding up all threads to a single bottleneck. To stop this from happening, we create a global logger that accepts all logs from all threads. This logger will also remain live for the entire duration of the program.

We can define our logger with the following code:

glue/src/logger/logger.rs
use tracing::Level;
use tracing_subscriber::FmtSubscriber;
pub fn init_logger() {
let subscriber = FmtSubscriber::builder()
.with_max_level(Level::INFO)
.finish();
tracing::subscriber::set_global_default(subscriber)
.expect("Failed to set up logger");
}

At the start of our program, we will call the init_logger() function to initialize our global logger. We also do not want dependent programs having to install the tracing crate on their own dependencies to log messages. Having to install the tracing crate multiple times leads to extra work when trying to update and maintain the tracing crate dependency. To prevent this, we create some wrapper functions around the tracing macros with the following code:

glue/src/logger/logger.rs
pub fn log_info(message: &str) {
tracing::info!("{}", message);
}
pub fn log_warn(message: &str) {
tracing::warn!("{}", message);
}
pub fn log_error(message: &str) {
tracing::error!("{}", message);
}
pub fn log_debug(message: &str) {
tracing::debug!("{}", message);
}
pub fn log_trace(message: &str) {
tracing::trace!("{}", message);
}

And with this, we can now initialize our logger and produce log messages. However, what about logging every HTTP request sent to the server? Sure, we could write log_info for every API endpoint, but this would be a pain to write and maintain. Instead, we can build some middleware to log every HTTP request for us.

Previously, we have implemented the FromRequest trait for our JWT. The FromRequest trait enabled us to extract the token from the header and pass the extracted ID from the token into the view. However, we must pass the JWT struct into the view for this process to work. For our purpose, we need our logger to automatically work on every request, and we want the logger to also log the response code. First, we create an Actix logger struct that implements the Transform trait. The Transform trait is essentially the interface of a service factory. A service is an async function that converts a request to a response. For our middleware, we initially need to import the following code:

glue/src/logger/network_wrappers/actix_web.rs
use actix_web::{dev::ServiceRequest, dev::ServiceResponse, Error};
use actix_web::dev::{Transform, Service};
use futures_util::future::{ok, Ready};
use std::task::{Context, Poll};
use std::pin::Pin;

We can then implement the Transform trait for a struct called ActixLogger, which we create with the following code:

glue/src/logger/network_wrappers/actix_web.rs
pub struct ActixLogger;
impl<S, B> Transform<S, ServiceRequest> for ActixLogger
where
S: Service<
ServiceRequest,
Response = ServiceResponse<B>,
Error = Error
> + 'static,
S::Future: 'static,
{
type Response = ServiceResponse<B>;
type Error = Error;
type InitError = ();
type Transform = LoggingMiddleware<S>;
type Future = Ready<Result<
Self::Transform, Self::InitError
>>;
}
fn new_transform(&self, service: S) -> Self::Future {
ok(LoggingMiddleware { service })
}

There is a lot going on here, but we can break it down by focusing on the new_transform function signature. Here, we take in a service, denoted by S, which is the following definition:

S: Service<ServiceRequest, Response = ServiceResponse, Error = Error

  • ‘static, S::Future: ‘static,

This signature is essentially a service request that returns a response and is a future, which is an async function. We can see that the new_transform function returns the service that is wrapped in a struct called LoggingMiddleware. We will build the LoggingMiddleware struct and implement the Service trait with the following code:

glue/src/network_wrappers/actix_web.rs
pub struct LoggingMiddleware<S> {
service: S,
}
impl<S, B> Service<ServiceRequest> for LoggingMiddleware<S>
where
S: Service<
ServiceRequest,
Response = ServiceResponse<B
>, Error = Error> + 'static,
S::Future: 'static,
{
type Response = ServiceResponse<B>;
type Error = Error;
type Future = Pin<
Box<
dyn futures_util::Future<
Output = Result<
Self::Response,
Self::Error
>
>
>;
fn poll_ready(&self, cx: &mut Context<'_>)
-> Poll<Result<(), Self::Error>> {
self.service.poll_ready(cx)
}
fn call(&self, req: ServiceRequest)
-> Self::Future {
...
}
}

Again, this is a lengthy chunk of code with a lot going on. However, we can see on the call function, we accept a request and return a Future. This Future is pinned because we do not want the future to move in memory because the thread will cycle back to the future to poll the future again, and we do not want the thread to access memory where the future used to be. We also put the future in a box because we do not know the size of the future, and boxing puts the future on the heap memory. If we remember the async chapter, which is Chapter 3, poll_ready is essentially polling the future. If the server is at capacity, then the poll_ready function will return a pending. If the request is ready to be processed, the poll_ready function will return a ready, and the request can start being polled. Inside our call function, we define the logging logic with the following code:

glue/src/logger/network_wrappers/actix_web.rs
fn call(&self, req: ServiceRequest)
-> Self::Future {
let fut = self.service.call(req);
Box::pin(async move {
let res = fut.await?;
let req_info = format!(
"{} {} {}",
res.request().method(),
res.request().uri(),
res.status().as_str()
);
tracing::info!("Request: {}", req_info);
Ok(res)
})
}

Here, we can see that we log the method, endpoint, and response code. Finally, we ensure that our logger is available in the library with the following declarations:

glue/src/logger/network_wrappers/mod.rs
#[cfg(feature = "actix")]
pub mod actix_web;
// File: glue/src/logger/mod.rs
pub mod network_wrappers;
pub mod logger;
// File: glue/src/lib.rs
pub mod errors;
pub mod token;
pub mod logger;

With this, our logger is ready to be integrated into our servers.

For our servers, they all follow the same template. We wrap ActixLogger in our server definition. All our servers should have a layout like the following:

…

use glue::logger::{
logger::init_logger,
network_wrappers::actix_web::ActixLogger
};
use actix_cors::Cors;
#[tokio::main]
async fn main() -> std::io::Result<()> {
init_logger();
run_migrations().await;
HttpServer::new(|| {
let cors = Cors::default().allow_any_origin()
.allow_any_method()
.allow_any_header();
App::new().wrap(ActixLogger)
.wrap(cors)
.configure(api::views_factory)
})
.workers(4)
.bind("127.0.0.1:8081")?
.run()
.await
}

Here, we can see that we initialize the logger with the init_logger function, and then carry on with the rest of the process. For the ingress, we must run both migrations for both servers, but the outline is the same. Repeating the code three times would unnecessarily bloat the chapter.

If we run the ingress server, create a user, log in, and create an item, we should get logs like the following:

2024-07-19T23:36:02.320369Z INFO glue::logger::network_wrappers::actix_web: Request: POST /api/v1/users/create 201 2024-07-19T23:36:02.767376Z INFO glue::logger::network_wrappers::actix_web: Request: GET /api/v1/auth/login 200 2024-07-19T23:36:22.298595Z INFO glue::logger::network_wrappers::actix_web: Request: GET /api/v1/auth/login 200 2024-07-19T23:36:22.310567Z INFO glue::logger::network_wrappers::actix_web: Request: GET /api/v1/get/all 200 2024-07-19T23:36:38.671171Z INFO glue::logger::network_wrappers::actix_web: Request: POST /api/v1/create 201

Here, we can see that the timestamp is also present. This is by default for the logger and should be done as they help with putting together what has happened. Timestamps can also help us sort logs later. For instance, writing a log to disk the very instant that the log was created is not essential. We could batch logs together in memory to perform a batch write if needed. If we have the timestamp, it does not matter what order the logs are written to disk.

Right now, are logging system is printing out to the terminal. This is okay for local development. However, our system might grow to multiple isolated servers running by themselves. If we have multiple servers running by themselves, we would like to have a single place where we can see all the logs.

Some deployment environments provide built-in tooling for inspecting logs. For example, Kubernetes-based applications can use tools such as Lens to inspect pod logs directly. However, this approach depends on having Kubernetes available.

In practice, your application might be deployed in many different ways:

  • Across multiple physical servers that are not part of a single cluster
  • On a single machine without Docker or container orchestration
  • Using simple setups such as docker-compose

Regardless of how your application is deployed, writing logs to a database provides a consistent way to inspect and analyze them. Database-backed logs can be consumed by dashboards and monitoring tools without relying on a specific deployment platform. Observability Through Logging

To achieve maximum flexibility, we can move on to building a logging mechanism for a remote database.

You may have heard of OpenTelemetry or a range of dashboards such as Jaeger. However, at the time of writing this, a lot of the Rust libraries for such platforms are lacking in documentation, maturity, and introducing breaking changes. To show you how to set up OpenTelemetry and off-the-shelf dashboards would result in the book aging quickly. With the skills you have learned in this book, there is nothing stopping you from googling the latest configuration steps to run these off-the-shelf projects. For the rest of this chapter, we are going to build our own async mechanism that shoots logs off to a database, allowing you to query this database for specific logs. Our async mechanism is going to keep the pressure off the logging functionality directly, as we log every request. If we had to await a response from the database for every request, we would dramatically slow down our request processing times. Instead, we are going to shoot a log message off to an async actor, and the async actor is going to handle the logging of the message in the database.

Elasticsearch is a distributed, document-oriented database designed for fast indexing and querying of large volumes of semi-structured data. Unlike traditional relational databases, Elasticsearch is optimized for search-heavy workloads, making it particularly well-suited for logs, metrics, and event data.

Logs naturally fit this model: they are append-only, schema-flexible, and frequently queried by time range, severity, or message content. Elasticsearch allows us to efficiently search and aggregate logs across services without requiring rigid schemas or complex joins.

In this chapter, Elasticsearch serves as a practical example of a log-focused datastore. The logging architecture we build is not tied to Elasticsearch specifically – the same async logging mechanism could be adapted to send logs to any external system or database.

Learning this approach will also give you the skill set to offload other tasks from the request process if needed, using the actor approach. Before we set off building the logging mechanism, however, we must define what an actor is.

An actor is an isolated piece of code that can have state. This actor only communicates with other parts of the program via messages. The actor can have its own internal state that can be updated due to messages. The actor can also send messages to other actors or even create actors. While I do personally like the actor model, we must be careful when or where we apply the actor model. There is some overhead in sending messages and spawning tasks to run actors. So, if your actor is only going to do a couple of operations before finishing, then you might as well just call a couple of async functions inside your future.

Actors really shine when you have a long-running process, and you want to keep sending messages to them. This can allow you to take pressure off the task you are currently performing. It can also keep resources allocated to one actor as opposed to needing to reallocate those resources. For instance, when we make a network connection, there is usually a handshake consisting of messages back and forth to establish that connection. Having an actor maintain that connection and accept messages to send over that connection can reduce the number of handshakes you need to make. Another example is opening a file. When you open a file, the operating system needs to check whether the file is there, whether the reader has permissions, and must acquire a lock for that file. Instead of opening a file to perform one transaction to it and then closing it, we can have an actor maintain the handle of that file and write to it the messages that were sent to the actor. Another advantage to remember is that channels can also act as queues. Channels can have messages build up in them with the actor consuming them at the actor’s own pace if the computer has enough memory to keep the messages in memory, and the channel has enough allocated capacity.

For our logging, we are going to have an actor that consumes log messages from all over the program and send those logs to a database, as shown in Figure 13.1:

Figure 13.1 – Our logging actor

Now that we know what actors are and how we are going to use them, we can start building our remote logging system by building our actor.

Before we build our actor, we need the following crates in our glue workspace:

## glue/Cargo.toml
...
[dependencies]
...
serde_json = { version = "1.0.120", optional = true }
tokio = { version = "1.38.1", optional = true}
reqwest = { version = "0.12.5", optional = true }
chrono = { version = "0.4.38", optional = true }
once_cell = { version = "1.19.0", optional = true }

[figure outside: Figure 13.1]

[features]
actix = ["dep:actix-web"]
rocket = ["dep:rocket"]
axum = ["dep:axum", "dep:tower"]
hyper = ["dep:hyper", "dep:serde_json", "dep:http-body-util"]
elastic-logger = [
"dep:serde_json", "dep:tokio", "dep:reqwest", "dep:chrono"
]

Here, we can see that our elastic-logger feature utilizes all the new dependencies, but we do not need them if we are not performing remote logging, as we will not have an actor to support.

We then define our elastic-logger actor model with the following code:

glue/src/logger/mod.rs
pub mod network_wrappers;
pub mod logger;
#[cfg(feature = "elastic-logger")]
pub mod elastic_actor;

Inside our actor module, we must import the following before we can define our actor:

glue/src/logger/elastic_actor.rs
use tokio::sync::mpsc;
use tokio::sync::mpsc::{Receiver, Sender};
use serde_json::json;
use reqwest::{Client, Body};
use chrono::Utc;
use std::sync::LazyLock as Lazy;
use serde::Serialize;

Our actor is going to need to accept messages with enough data in them to create a log and send it. These messages need to be sent over a channel. Our message takes the following form:

glue/src/logger/elastic_actor.rs
#[derive(Debug, Serialize)]
struct LogMessage {
level: String,
message: String,
}

With this message, we can then build our send_log function with the following code:

glue/src/logger/elastic_actor.rs
pub async fn send_log(level: &str, message: &str) {
static LOG_CHANNEL: Lazy<Sender<LogMessage>> = Lazy::new(|| {
// 模拟发送日志的异步逻辑,实际实现需结合真实代码
// 这里为演示示例,实际需替换为真实代码
// 例如:log_channel.send(LogMessage { level: "error", message: "Error" }).await
})
}
let (tx, rx) = mpsc::channel(100);
tokio::spawn(async move {
elastic_actor(rx).await;
});
tx
});
LOG_CHANNEL.send(LogMessage {
level: level.to_string(),
message: message.to_string(),
}).await.unwrap();
}

Here, we can see that we exploit Lazy::new to create our channel, spawn our actor, and return the channel sender. Lazy::new only gets executed once, so no matter where we call the send_log func-tion, we are sending messages to that one actor that has been spawned the first time the send_log function was called.

For our actor, we define the outline with the following code:

glue/src/logger/elastic_actor.rs
async fn elastic_actor(mut rx: Receiver<LogMessage>) {
let elastic_url = std::env::var(
"ELASTICSEARCH_URL"
).unwrap();
let client = Client::new();
while let Some(log) = rx.recv().await {
...
}
}

Here, we can see that we get the URL to the database, establish an HTTP client, and then run an in-finite loop where we cycle through an iteration of the loop every time a message is received from the channel. Once we get the message from the channel, we create a JSON body and send it via HTTP to the Elasticsearch database with the following code:

glue/src/logger/elastic_actor.rs
let body = json!({
"level": log.level,
"message": log.message,
"timestamp": Utc::now().to_rfc3339()
});
let body = Body::from(serde_json::to_string(&body)
.unwrap());
match client.post(&elastic_url)
header("Content-Type", "application/json")
.header("Accept", "application/json")
.body()
.send()
.await

Ok(result) => {}, Err(e) => {

eprintln!(
"Failed to send log to Elasticsearch: {}",
e
);
}
}

We can see that we directly unwrap the serialization of the message. This is because there will never be an error translating two strings and a timestamp to a JSON string. However, there could be an issue in sending the HTTP request to the Elasticsearch database; therefore, we handle the potential error when sending the HTTP request. This is because our actor is running in the background for the entire duration of the program. We would hate for a temporary network error to kill our actor, blocking us from sending any more logs to the database.

Our actor can now run and accept messages if we call the send_log function anywhere in the code base. We now need to call the send_log function in our logging function.

For our logging functions, we still need to log the message to the terminal so logs can still be recovered if there is a problem with our database. This sending of the message to the database should be triggered if the feature is enabled. Considering this, our init_logger function remains the same, giving us the following outline:

glue/src/logger/logger.rs
...
#[cfg(feature = "elastic-logger")]
use super::elastic_actor::send_log;
pub fn init_logger() {
...
}

Our functions now take the following form:

glue/src/logger/logger.rs
pub async fn log_info(message: &str) {
tracing::info!("{}", message);
#[cfg(feature = "elastic-logger")]
send_log("INFO", message).await;
pub async fn log_warn(message: &str) {
tracing::warn!("{}", message);
#[cfg(feature = "elastic-logger")]
send_log("WARN", message).await;
}
pub async fn log_error(message: &str) {
tracing::error!("{}", message);
#[cfg(feature = "elastic-logger")]
send_log("ERROR", message).await;
}
pub async fn log_debug(message: &str) {
tracing::debug!("{}", message);
#[cfg(feature = "elastic-logger")]
send_log("DEBUG", message).await;
}
pub async fn log_trace(message: &str) {
tracing::trace!("{}", message);
#[cfg(feature = "elastic-logger")]
send_log("TRACE", message).await;
}

With this, our system is now in place to send messages to the database. We now must update our Cargo.toml files for all our elastic-logger feature for the glue module:

…

glue = {
path = "../glue",
features = ["actix", "elastic-logger"]
}

…

File: nanoservices/auth/networking/actix_server/Cargo.toml

Section titled “File: nanoservices/auth/networking/actix_server/Cargo.toml”

…

glue = {
path = "../../../../glue",
features = ["actix", "elastic-logger"]
}
...
# nanoservices/to_do/networking/actix_server/Cargo.toml
...
glue = {
path = "../../../../glue",
features = ["actix", "elastic-logger"]
}
...

Now that our system is fully ready to send logs to our database, we must configure our database.

To enable our database to accept logs, we add the following service to our docker-compose.yml file:

docker-compose.yml
...
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:7.13.4
container_name: elasticsearch
environment:
- discovery.type=single-node
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
ports:
- "9200:9200"
- "9300:9300"

For the Elasticsearch database, we have the following configurations:

  • discovery.type=single-node: This configures Elasticsearch to run in single-node mode, which is suitable for development or testing environments.
  • bootstrap.memory_lock=true: This ensures that the JVM locks the memory used by Elasticsearch, preventing it from being swapped out by the operating system.
  • “ES_JAVA_OPTS=-Xms512m -Xmx512m”: This sets the Java heap size options, specifying that both the initial and maximum heap sizes should be 512 MB.
  • memlock: This prevents the container from swapping memory to disk. Setting both soft and hard limits to -1 means unlimited memory lock.
  • 9200:9200: This maps port 9200 of the container to port 9200 of the host. This is the default port for the Elasticsearch HTTP API.
  • 9300:9300: This maps port 9300 of the container to port 9300 of the host. This is the default port for Elasticsearch transport node communication.

Due to the new database, our .env file should look like the following:

TO_DO_DB_URL=postgres://username:password@localhost/to_do AUTH_DB_URL=postgres://username:password@localhost/to_do AUTH_API_URL=http://127.0.0.1:8081 JWT_SECRET=secret CACHE_API_URL=redis://127.0.0.1:6379 ELASTICSEARCH_URL=http://localhost:9200/logs/_doc

And that is it! Our system is now able to start sending logs out of our database.

If we run our server, and then create a user login using our ingress/scripts/create_login.sh script, we can make a curl request to our database with the following command:

curl -X GET "http://localhost:9200/logs/_search?pretty" \
-H 'Content-Type: application/json' -d'
{
"query": {
"match": {
"level": "INFO"
}
}
}
'

Here, we are making a query for all logs with a level of INFO. Our query gives us the following results:

{
"took" : 39,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.18232156,
"hits" : [
{
"_index" : "logs",
"_type" : "_doc",
"_id" : "NbbV15AB4EMJAczNw0Wr",
"_score" : 0.18232156,
"_source" : {
"level" : "INFO",
"message" : "Request: POST /api/v1/users/create 201",
"timestamp" : "2024-07-22T00:27:08.721360+00:00"
}
},
{
"_index" : "logs",
"_type" : "_doc",
"_id" : "NrbV15AB4EMJAczNw4Xx",
"_score" : 0.18232156,
"_source" : {
"level" : "INFO",
"message" : "Request: GET /api/v1/auth/login 200",
"timestamp" : "2024-07-22T00:27:09.167054+00:00"
}
]
}
}

We can see that both of our logs are here in the database. We can also see that we have a score. The higher the score, the more relevant the log is to the search. We can also see that we have max_score in the response. We can see that the scores in the logs are the same as the maximum scores. Our level: “INFO” matches exactly. We could be more granular and produce even more tags on our logs. For instance, we could also put in a tag for the service and filter by this if we want. As your system gets more complex, logging to a database can be a lifesaver.

Summary In this chapter, we set up a basic logger and created an actor to take pressure off the logger when sending those logs to an Elasticsearch database. With our logging setup, we can inspect logs from multiple distributed servers and query those logs. Not only does this help us debug our systems and monitor the health of our system when it is live, but we also now have the skill set to pass off logs to an actor running in the background, so our server is not held up awaiting network calls to the Elasticsearch database.

We have now reached the end of the development of the to-do application. You can always add more features and improve things, but we would merely be repeating the concepts that we covered in the book to achieve these new features. For instance, adding a logout button and implementing the refresh token mechanism for authentication sessions are probably things you want to add to our to-do application, but covering these features would just bloat the book with repetition of updating the local storage in the frontend with a returned refreshed token, and adding a button on the page to hit the logout API. The rest of the book focuses on how to test your server, how to deploy the system, and some more advanced concepts such as low-level networking. In the next chapter, we will cover unit testing. As stated in the previous chapter, good testing practices are probably the most important software engineering skill that will set you apart from other developers. You will be able to develop complex systems at a safer and faster rate. I’ve spoken publicly about a range of things. When it comes to testing, the outreach is mainly from senior developers and tech leads with reputable backgrounds. Junior developers or developers who have never been pushed to develop their craft don’t really understand testing and how important and useful it is.

Questions

  1. Why do we need logging in a multi-service system?
  2. What is REST, and which HTTP verbs does this chapter associate with common operations?
  3. What does “code on demand” mean in REST, and how does the chapter demonstrate it?
  4. What are the four log levels described, and when would you use each?
  5. Why not just use println! for request logging?
  6. What problem does the Actix middleware solve, and what does it log?
  7. What is an actor in this chapter’s context, and why is it useful for logging?
  8. Why use Elasticsearch for logs, and how do you query logs in the chapter?

Answers

  1. Because without logs, we can’t easily tell what happened inside the system—whether one server called another, what endpoint was hit, what status code came back, or where an error originated. Logs let us reconstruct the “story” of a request through the system, debug failures without exposing sensitive internal details to users, and monitor system health over time.
  2. REST (Representational state transfer) is an architectural style for APIs. In the chapter’s framing: Observability Through Logging

GET reads data POST creates data PUT updates data DELETE removes data

The goal is a predictable, reusable interface that supports performance and evolvability.

  1. “Code on demand” is an optional REST constraint where the server can send executable code to the client to run. The chapter demonstrates this by returning an HTML response containing a

  2. Informational (info): Normal checkpoints (server start/stop, HTTP requests).

Verbose: More granular detail, mainly for debugging; generally avoided in production. Warning: Something went wrong, but you’re not interrupting the user flow (e.g., a downstream call failed, but you can recover later). Error: A failure that interrupts the current operation (e.g., database insert failed, request can’t be completed), though the server can keep running.

  1. println! locks stdout each time it writes. If multiple threads are processing requests, they end up contending for that lock, turning logging into a bottleneck. The chapter uses tracing + tracing-subscriber to set a global logger that can accept log events from across the program more efficiently and consistently.

  2. It avoids manually adding logging to every endpoint. By wrapping the service in middleware, it logs every HTTP request automatically, including the following:

HTTP method URI/path Response status code

So, you get consistent per-request observability without repetitive code.

  1. An actor is a long-running task that owns its own state and communicates via messages (through a channel). It’s useful for logging because the main request path can send log messages to the actor and move on, while the actor handles slower work, such as network I/O to an external log store. This reduces latency and keeps request handling responsive.

  2. Elasticsearch is positioned as a good fit for logs because logs are append-only, semi-structured, and commonly queried by time range, severity, or text search. The chapter queries logs via the Elasticsearch HTTP API using a search request, for example, matching all documents where “level”: “INFO”, and it explains that results include hits plus relevance scoring (_score / max_score).


Get this book’s PDF copy, code bundle, and more

Section titled “Get this book’s PDF copy, code bundle, and more”

Scan the QR code (or go to packtpub.com/unlock). Search for this book by name, confirm the edition, and then follow the steps on the page.

UNLOCK NOW

Note: Have your invoice handy. Purchases made directly from the Packt website don’t require an invoice.

[blank page]