Watch
0
0
Fork
You've already forked hyperhive
0

k8s: pod logs and metrics reach the hive collector

This commit is contained in:
atlas 2026-11-21 14:01:39 +01:00 • committed by mara
commit dbcc47573e
3 changed files with 447 additions and 51 deletions

View file

@ -24,6 +24,7 @@ in
imports = [
./host-services.nix
./node.nix
./pod-logs.nix
];
options.services.hyperhive.k8s = {

View file

@ -0,0 +1,119 @@
# 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+(?P<level>TRACE|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;
};
};
};
}

View file

@ -73,6 +73,25 @@ let
};
};
# Where a pod pushes telemetry: the hive collector, by the name the node's
# CoreDNS answers with the bridge address (./host-services.nix). A unit's own
# endpoint is a loopback forwarder or a gateway route that needs a token, and
# a pod has neither. `otlpEndpointVars` are the variables a unit may name its
# endpoint in; the first is the standard one and is always set.
otelCfg = hyperhiveCfg.otel;
collectorEndpoint = "http://${otelCfg.collector.host}:${toString hyperhiveCfg.swarm.hiveOtel.collectorPort}";
otlpEndpointVars = [
"OTEL_EXPORTER_OTLP_ENDPOINT"
"SWARM_CI_METRICS_OTLP_ENDPOINT"
];
podOtlpEnv =
unitEnv:
lib.optionalAttrs otelCfg.collector.enable (
lib.genAttrs (lib.filter (
n: n == "OTEL_EXPORTER_OTLP_ENDPOINT" || unitEnv ? ${n}
) otlpEndpointVars) (_: collectorEndpoint)
);
fail = name: msg: throw "services.hyperhive.k8s: ${name}: ${msg}";
labelsFor = name: {
@ -127,6 +146,7 @@ let
env =
unitEnv
// podOtlpEnv unitEnv
// lib.optionalAttrs (runtimeDir != null) { RUNTIME_DIRECTORY = runtimeDir; }
// lib.optionalAttrs (stateDir != null) { STATE_DIRECTORY = stateDir; }
// lib.optionalAttrs (svc.healthEnv != null) {