Loki

  • Canonical Observability
Channel Revision Published Runs on
latest/stable 160 10 Sep 2024
Ubuntu 20.04
latest/candidate 160 01 Aug 2024
Ubuntu 20.04
latest/beta 161 01 Aug 2024
Ubuntu 20.04
latest/edge 173 12 Oct 2024
Ubuntu 20.04
1.0/stable 104 12 Dec 2023
Ubuntu 20.04
1.0/candidate 104 22 Nov 2023
Ubuntu 20.04
1.0/beta 104 22 Nov 2023
Ubuntu 20.04
1.0/edge 104 22 Nov 2023
Ubuntu 20.04
juju deploy loki-k8s
Show information

Platform:

charms.loki_k8s.v0.charm_logging

This charm library contains utilities to automatically forward your charm logs to a loki-push-api endpoint.

(yes! charm code, not workload code!)

If your charm isn't already related to Loki using any of the consumers/forwarders from the loki_push_api library, you need to:

charmcraft fetch-lib charms.loki_k8s.v1.loki_push_api

and add the logging consumer that matches your use case. See https://charmhub.io/loki-k8s/libraries/loki_push_apihttps://charmhub.io/loki-k8s/libraries/loki_push_api for more information.

Once your charm is related to, for example, COS' Loki charm (or a Grafana Agent), you will be able to inspect in real time from the Grafana dashboard the logs emitted by your charm.

Labels

The library will inject the following labels into the records sent to Loki:

  • model: name of the juju model this charm is deployed to
  • model_uuid: uuid of the model
  • application: juju application name (such as 'mycharm')
  • unit: unit name (such as 'mycharm/0')
  • charm_name: name of the charm (whatever is in metadata.yaml) under 'name'.
  • juju_hook_name: name of the juju event being processed ` service_name: name of the service this charm represents. Defaults to app name, but can be configured by the user.
Usage

To start using this library, you need to do two things: that returns an http/https endpoint url. If you are using the LokiPushApiConsumer as

  1. decorate your charm class with

    @log_charm(loki_push_api_endpoint="my_logging_endpoints")

  2. add to your charm a "my_logging_endpoint" (you can name this attribute whatever you like) property

self.logging = LokiPushApiConsumer(self, ...), the implementation could be:

@property
def my_logging_endpoints(self) -> List[str]:
    '''Loki push API endpoints for charm logging.'''
    # this will return an empty list if there is no relation or there is no data yet in the relation
    return ["http://loki-0.loki.svc.cluster.local:3100"]

The log_charm decorator will take these endpoints and set up the root logger (as in python's logging module root logger) to forward all logs to these loki endpoints.

TLS support

If your charm integrates with a tls provider which is also trusted by the logs receiver, you can configure TLS by passing a server_cert parameter to the decorator.

If you're not using the same CA as the loki-push-api endpoint you are sending logs to, you'll need to implement a cert-transfer relation to obtain the CA certificate from the same CA that Loki is using.

@log_charm(loki_push_api_endpoint="my_logging_endpoint", server_cert="my_server_cert")
class MyCharm(...):
    ...

    @property
    def my_server_cert(self) -> Optional[str]:
        '''Absolute path to a server crt if TLS is enabled.'''
        if self.tls_is_enabled():
            return "/path/to/my/server_cert.crt"

def is_enabled()

Whether charm logging is enabled.

Description

We assume it is enabled, unless the envvar CHARM_LOGGING_ENABLED is set to 0 (or anything except 1).

class CharmLoggingError

Description

Base class for all exceptions raised by this module. None

class InvalidEndpointError

Description

Raised if an endpoint is invalid. None

class InvalidEndpointsError

Description

Raised if an endpoint is invalid. None

def charm_logging_disabled()

Contextmanager to temporarily disable charm logging.

Description

For usage in tests.

def log_charm(
    logging_endpoints: str,
    server_cert,
    service_name
)

Set up the root logger to forward any charm logs to one or more Loki push API endpoints.

Arguments

server_cert

method or property on the charm type that returns an optional absolute path to a tls certificate to be used when sending traces to a remote server. If it returns None, an insecure connection will be used.

logging_endpoints

name of a property on the charm type that returns a sequence of (fully resolvable) Loki push API urls. If None, charm logging will be effectively disabled. Else, the root logger will be set up to forward all logs to those endpoints.

service_name

service name tag to attach to all logs generated by this charm. Defaults to the juju application name this charm is deployed under.

Description

Usage:

from charms.loki_k8s.v0.charm_logging import log_charm from charms.loki_k8s.v1.loki_push_api import LokiPushApiConsumer from ops import CharmBase

@log_charm( logging_endpoints="loki_push_api_urls", ) class MyCharm(CharmBase):

def __init__(self, framework: Framework):
    ...
    self.logging = LokiPushApiConsumer(self, ...)

@property
def loki_push_api_urls(self) -> Optional[List[str]]:
    return [endpoint['url'] for endpoint in self.logging.loki_endpoints]

Methods