# The logs of the cluster's pods, read by the host's collector. # # On a host that runs a collector (`services.opentelemetry-collector`, the hive # tier's, ../otel.nix) and the k3s node, the collector reads the CRI log files # kubelet keeps under /var/log/pods and ships them down its own exporters. The # node is one host, so one reader covers every pod. # docs/4-implementation/k8s/telemetry.md has the design. # # containerd writes each log file as root, mode 0640, so the collector's user # needs an ACL. It is a static user rather than the nixpkgs unit's dynamic one # because an ACL names a user that exists before the unit starts. The ACL # covers /var/log/pods and what kubelet creates under it, and nothing else. { config, lib, ... }: let cfg = config.services.hyperhive.deploy.k8s; user = "opentelemetry-collector"; logsDir = "/var/log/pods"; collector = config.services.opentelemetry-collector; in { options.services.hyperhive.deploy.k8s.podLogsExporters = lib.mkOption { type = lib.types.listOf lib.types.str; default = [ "otlphttp/swarm" ]; internal = true; description = '' The exporters the pod logs pipeline ends in. The hive collector's one exporter by default; the VM test points it at a file. ''; }; config = lib.mkIf (cfg.enable && collector.enable) { users.users.${user} = { isSystemUser = true; group = user; }; users.groups.${user} = { }; systemd.services.opentelemetry-collector.serviceConfig = { DynamicUser = lib.mkForce false; User = user; Group = user; # What `DynamicUser` implied and a static user does not. PrivateTmp = true; ProtectHome = true; RemoveIPC = true; }; # `A+` sets the ACL on the directory and what is in it, and the default # entry on every directory, so the per-pod directories kubelet creates # later inherit it. The `d` line makes the directory exist before kubelet # does, with the mode kubelet gives it. systemd.tmpfiles.rules = [ "d ${logsDir} 0755 root root -" "A+ ${logsDir} - - - - u:${user}:rX,d:u:${user}:rX" ]; services.opentelemetry-collector.settings = { # `file_storage` is the extension the collector defines for its journald # receiver's cursor (../otel.nix): the offsets survive a restart. receivers."file_log/pods" = { include = [ "${logsDir}/*/*/*.log" ]; include_file_path = true; start_at = "beginning"; storage = "file_storage"; operators = [ # CRI framing; reads the namespace, pod, uid and container name from # the file's path into resource attributes. { type = "container"; } # hive-log's text format, `tracing-subscriber`'s default: a # timestamp, then the level. A line that does not start that way # keeps no severity. { type = "regex_parser"; regex = ''^\S+\s+(?PTRACE|DEBUG|INFO|WARN|ERROR)\s''; parse_from = "body"; on_error = "send_quiet"; } { type = "severity_parser"; "if" = "attributes.level != nil"; parse_from = "attributes.level"; overwrite_text = true; } { type = "remove"; "if" = "attributes.level != nil"; field = "attributes.level"; } ]; }; # `service.name` is the container's name without the `swarm-` prefix, # the name the swarm's container services' own forwarders stamp # (`nix/container-modules/swarm-journal-forwarder.nix`). processors."transform/pod-service-name" = { error_mode = "ignore"; log_statements = [ { context = "resource"; statements = [ ''set(attributes["service.name"], attributes["k8s.container.name"])'' ''replace_pattern(attributes["service.name"], "^swarm-", "")'' ]; } ]; }; service.pipelines."logs/pods" = { receivers = [ "file_log/pods" ]; processors = [ "transform/pod-service-name" ]; exporters = cfg.podLogsExporters; }; }; }; }