swarm-otel: scrape the authenticated targets only once the collector has its client secret
An empty secret file made the prometheus receiver request a token for each published target every scrape; authelia's per-address limit then refused every client behind that address. Without a secret the collector starts on the config minus those jobs. full-setup asserts the limiter never fired.
This commit is contained in:
parent
9237948c2c
commit
480855dfaf
1 changed files with 157 additions and 4 deletions
|
|
@ -145,8 +145,9 @@ let
|
|||
# after a reboot the delivery unit below fetches it again. The container
|
||||
# sees the directory read-only at the same path. The file starts out empty
|
||||
# (tmpfiles, below) so `LoadCredential` has a source before the first
|
||||
# fetch; a collector that starts on it is refused by the stores until the
|
||||
# fetched secret restarts it.
|
||||
# fetch; a collector that starts on it exports nothing and scrapes only the
|
||||
# targets that need no secret (`scrapeNeedsSecret`) until the fetched secret
|
||||
# restarts it.
|
||||
#
|
||||
# Spelled once, for the unit that writes it and the option that names it,
|
||||
# which are a few hundred lines apart: a collector reading a path nothing
|
||||
|
|
@ -163,8 +164,53 @@ let
|
|||
# collector cannot open, discovered at runtime and nowhere else.
|
||||
collectorSecretPath = "/run/credentials/opentelemetry-collector.service/${collectorCredentialId}";
|
||||
|
||||
# A container module: the collector starts on the full config only when the
|
||||
# credential file it loaded holds a secret.
|
||||
#
|
||||
# The prometheus receiver reads `client_secret_file` on every scrape and
|
||||
# requests a token even when the file is empty, so each authenticated scrape
|
||||
# target is refused at authelia's token endpoint once a minute, and the
|
||||
# endpoint's per-address rate limit then answers every client of this
|
||||
# container with 429 for minutes. Without a secret the collector therefore
|
||||
# starts on the same config minus the `publishedScrapeTargets` jobs (the
|
||||
# receiver and its pipeline too when nothing else is scraped). The
|
||||
# authenticated exporters already refuse an empty secret locally. The
|
||||
# delivery unit restarts a running collector when it writes the secret.
|
||||
scrapeNeedsSecret =
|
||||
{ config, ... }:
|
||||
let
|
||||
collector = config.services.opentelemetry-collector;
|
||||
settings = collector.settings;
|
||||
withoutPublished = scrapeWithoutJobs {
|
||||
inherit settings;
|
||||
jobs = lib.attrNames cfg.publishedScrapeTargets;
|
||||
pipeline = "metrics/${swarmTierName}";
|
||||
};
|
||||
# `otelcol validate`, as upstream's module does for `settings`.
|
||||
validated =
|
||||
name: value:
|
||||
pkgs.runCommandLocal "${name}.yaml"
|
||||
{ generated = (pkgs.formats.yaml { }).generate "${name}.yaml" value; }
|
||||
''
|
||||
cp $generated $out
|
||||
${lib.getExe collector.package} validate --config=file:$out
|
||||
'';
|
||||
start = pkgs.writeShellScript "opentelemetry-collector-start" ''
|
||||
config=${validated "config" settings}
|
||||
if [ ! -s "$CREDENTIALS_DIRECTORY/${collectorCredentialId}" ]; then
|
||||
echo "the client secret is empty: not scraping the targets that need it until the secret is delivered and this unit restarts" >&2
|
||||
config=${validated "config-without-secret" withoutPublished}
|
||||
fi
|
||||
exec ${lib.getExe collector.package} --config=file:"$config"
|
||||
'';
|
||||
in
|
||||
{
|
||||
systemd.services.opentelemetry-collector.serviceConfig.ExecStart = lib.mkForce "${start}";
|
||||
};
|
||||
|
||||
atomicWriteSecret = import ./lib/atomic-write-secret.nix { };
|
||||
refreshConsumer = import ./lib/refresh-consumer.nix { };
|
||||
scrapeWithoutJobs = import ./lib/scrape-without-jobs.nix { inherit lib; };
|
||||
|
||||
# `attrNames` is sorted, so this is a function of the hive SET and not of
|
||||
# the order anyone wrote it in.
|
||||
|
|
@ -776,7 +822,7 @@ in
|
|||
# The bind source must exist when the container starts.
|
||||
"d ${collectorSecretDir} 0700 root root -"
|
||||
# `LoadCredential=` fails the unit when the file is missing; an empty
|
||||
# one lets the collector start and be refused by the stores.
|
||||
# one lets the collector start, without the scrape jobs that need the secret.
|
||||
"f ${collectorSecretInContainer} 0400 root root -"
|
||||
]
|
||||
# Unless a deployment delivers its own secret to that very path.
|
||||
|
|
@ -1133,7 +1179,9 @@ in
|
|||
name = cfg.machine;
|
||||
consumers = [ "opentelemetry-collector" ];
|
||||
})
|
||||
];
|
||||
]
|
||||
# The scrape side of the client secret: see `scrapeNeedsSecret`.
|
||||
++ lib.optional haveCollectorSecret scrapeNeedsSecret;
|
||||
|
||||
system.stateVersion = config.system.stateVersion;
|
||||
services.hyperhive.swarmContainer = { inherit privateNetwork; };
|
||||
|
|
|
|||
Loading…
Reference in a new issue