Skip to content

Commit

Permalink
Merge pull request #318306 from flokli/grafana-alloy-module
Browse files Browse the repository at this point in the history
grafana-alloy: add NixOS module
  • Loading branch information
flokli committed Jun 10, 2024
2 parents e66204d + 1fa96ce commit d2d2467
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 1 deletion.
1 change: 1 addition & 0 deletions nixos/modules/module-list.nix
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,7 @@
./services/misc/zoneminder.nix
./services/misc/zookeeper.nix
./services/monitoring/alerta.nix
./services/monitoring/alloy.nix
./services/monitoring/apcupsd.nix
./services/monitoring/arbtt.nix
./services/monitoring/below.nix
Expand Down
80 changes: 80 additions & 0 deletions nixos/modules/services/monitoring/alloy.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{ lib, pkgs, config, ... }:
with lib;
let
cfg = config.services.alloy;
in
{
meta = {
maintainers = with maintainers; [ flokli hbjydev ];
};

options.services.alloy = {
enable = mkEnableOption "Grafana Alloy";

package = mkPackageOption pkgs "grafana-alloy" { };

configPath = mkOption {
type = lib.types.path;
default = "/etc/alloy";
description = ''
Alloy configuration file/directory path.
We default to `/etc/alloy` here, and expect the user to configure a
configuration file via `environment.etc."alloy/config.alloy"`.
This allows config reload, contrary to specifying a store path.
A `reloadTrigger` for `config.alloy` is configured.
Other `*.alloy` files in the same directory (ignoring subdirs) are also
honored, but it's necessary to manually extend
`systemd.services.alloy.reloadTriggers` to enable config reload
during nixos-rebuild switch.
This can also point to another directory containing `*.alloy` files, or
a single Alloy file in the Nix store (at the cost of reload).
Component names must be unique across all Alloy configuration files, and
configuration blocks must not be repeated.
Alloy will continue to run if subsequent reloads of the configuration
file fail, potentially marking components as unhealthy depending on
the nature of the failure. When this happens, Alloy will continue
functioning in the last valid state.
'';
};

extraFlags = mkOption {
type = with lib.types; listOf str;
default = [ ];
example = [ "--server.http.listen-addr=127.0.0.1:12346" "--disable-reporting" ];
description = ''
Extra command-line flags passed to {command}`alloy run`.
See <https://grafana.com/docs/alloy/latest/reference/cli/run/>
'';
};
};


config = mkIf cfg.enable {
systemd.services.alloy = {
wantedBy = [ "multi-user.target" ];
reloadTriggers = [ config.environment.etc."alloy/config.alloy".source or null ];
serviceConfig = {
Restart = "always";
DynamicUser = true;
RestartSec = 2;
SupplementaryGroups = [
# allow to read the systemd journal for loki log forwarding
"systemd-journal"
];
ExecStart = "${lib.getExe cfg.package} run ${cfg.configPath} ${escapeShellArgs cfg.extraFlags}";
ExecReload = "${pkgs.coreutils}/bin/kill -SIGHUP $MAINPID";
ConfigurationDirectory = "alloy";
StateDirectory = "alloy";
WorkingDirectory = "%S/alloy";
Type = "simple";
};
};
};
}
1 change: 1 addition & 0 deletions nixos/tests/all-tests.nix
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ in {
akkoma = handleTestOn [ "x86_64-linux" "aarch64-linux" ] ./akkoma.nix {};
akkoma-confined = handleTestOn [ "x86_64-linux" "aarch64-linux" ] ./akkoma.nix { confined = true; };
alice-lg = handleTest ./alice-lg.nix {};
alloy = handleTest ./alloy.nix {};
allTerminfo = handleTest ./all-terminfo.nix {};
alps = handleTest ./alps.nix {};
amazon-init-shell = handleTest ./amazon-init-shell.nix {};
Expand Down
32 changes: 32 additions & 0 deletions nixos/tests/alloy.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import ./make-test-python.nix ({ lib, pkgs, ... }:

let
nodes = {
machine = {
services.alloy = {
enable = true;
};
environment.etc."alloy/config.alloy".text = "";
};
};
in
{
name = "alloy";

meta = with lib.maintainers; {
maintainers = [ flokli hbjydev ];
};

inherit nodes;

testScript = ''
start_all()
machine.wait_for_unit("alloy.service")
machine.wait_for_open_port(12345)
machine.succeed(
"curl -sSfN http:https://127.0.0.1:12345/-/healthy"
)
machine.shutdown()
'';
})
4 changes: 3 additions & 1 deletion pkgs/by-name/gr/grafana-alloy/package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
, fixup-yarn-lock
, nodejs
, grafana-alloy
, nixosTests
, nix-update-script
, installShellFiles
, testers
Expand Down Expand Up @@ -103,6 +104,7 @@ buildGoModule rec {

passthru = {
tests = {
inherit (nixosTests) alloy;
version = testers.testVersion {
version = "v${version}";
command = "${lib.getExe grafana-alloy} --version";
Expand All @@ -119,7 +121,7 @@ buildGoModule rec {
mainProgram = "alloy";
license = licenses.asl20;
homepage = "https://grafana.com/oss/alloy";
maintainers = with maintainers; [ flokli emilylange ];
maintainers = with maintainers; [ flokli emilylange hbjydev ];
platforms = lib.platforms.unix;
};
}

0 comments on commit d2d2467

Please sign in to comment.