loki-k8s docs - Logging via Pebble Log Forwarding

Logging via Pebble Log Forwarding

You can easily integrate with the Loki charm to send the logs of the workload via Pebble’s log forwarding feature. This allows you to gather the logs that your workload is printing to stdout and send them to Loki.

If your workload doesn’t produce logs to stdout, you can add a Pebble service that runs tail -f /path/to/log.file to achieve the same result.

Prepare your charm

To start, fetch the loki_push_api charm library:

charmcraft fetch-lib charms.loki_k8s.v1.loki_push_api

Add an integration to your charm for the loki_push_api interface, in metadata.yaml (or charmcraft.yaml, if you use the single-file structure):

requires:
  logging:  # recommended name, but you can change it
    interface: loki_push_api
    description: |
      Receives Loki's push api endpoint address to push logs to, and forwards charm's built-in alert rules to Loki.

Write the integration code

The loki_push_api library provides a Log Forwarder object that conveniently wraps all the necessary things you would need to write. Internally, this objects updates the Pebble layer’s log-targets section with the correct Juju Topology information (e.g., application name, unit name, etc.).

Use the Log Forwarder class by instantiating it in the __init__ method of your charm:

from charms.loki_k8s.v1.loki_push_api import LogForwarder

	def __init__(self, *args):
		...
		self._log_forwarder = LogForwarder(
			self,
			relation_name="logging", # optional, defaults to "logging"
		)
		

The Log Forwarder will, by default, observe relation events on the logging endpoint and enable (or disable) log forwarding automatically.

Test the result

You can now deploy your charm and try to relate it to Loki:

juju deploy <your-charm>
juju deploy loki-k8s loki
juju relate <your-charm> loki

Once the relation is active and healthy, the library will inject a Pebble layer in each workload container the charm has access to, to configure Pebble’s log forwarding and start sending logs to Loki.

Shouldn’t we make clear that the logs LogForwarder will sent are the ones in the STDOUT?