Understanding the Log Structure
Promtail handles logs that include embedded JSON strings. Consider sample log entries where properties like the HTTP method, route, and status code are embedded within a JSON object. Although you can still search for text, promoting these values to labels simplifies filtering and querying. For example, a typical log entry might appear as follows:Updating the Promtail Configuration
To extract these properties as labels, update your Promtail configuration file (typically namedpromtail.yaml). Begin by examining the log structure to confirm the placement of the required data, and then add a new pipeline stage.
Below is an example of the original Promtail configuration snippet:
The first JSON stage extracts the inner “log” property from the incoming message, while the second JSON stage works on the extracted “log” object to obtain the “code” and “method” fields. These extracted properties can then be used as labels in Loki for more detailed queries.
Deploying the Updated Configuration
Once you have saved the updatedpromtail.yaml, update the Promtail secret in your Kubernetes cluster to deploy these changes. Follow these steps:
-
Retrieve the Current Secret:
Execute the command below to save the existing secret to a file:
-
Delete the Existing Secret:
Remove the current secret with:
-
Create a New Secret from the Updated File:
Recreate the secret using:
-
Restart the Promtail Pod:
Since Promtail pods use this secret, restart the pod to apply the changes:
After a few seconds, Kubernetes will launch a new pod with the updated configuration. Verify the running pods by executing:
Ensure you apply these commands carefully in your production environment. Always back up your current configuration before making changes.
Verifying the Changes in Grafana
After deploying the updated configuration, return to Grafana to confirm that the labels have been correctly applied. Look for log entries that now include thecode and method labels. For example:
By following these detailed steps, you have enhanced your logging pipeline by converting essential log properties into labels. This improved configuration not only streamlines the log analysis process but also boosts the efficiency of your monitoring setup using Grafana and Loki. For additional guidance, check out the official Loki Documentation and Promtail Repository.