Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: switch to pkgs.nix-based alloy module & package #17

Merged
merged 9 commits into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 71 additions & 14 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 4 additions & 38 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

srvos.url = "github:nix-community/srvos";
srvos.inputs.nixpkgs.follows = "nixpkgs";

pkgs-nix.url = "github:ALT-F4-LLC/pkgs.nix";
pkgs-nix.inputs.nixpkgs.follows = "nixpkgs";
};

outputs = inputs @ {flake-parts, ...}:
Expand All @@ -26,44 +29,7 @@

formatter = pkgs.alejandra;

packages = {
gc-fwd = inputs.nixos-generators.nixosGenerate {
inherit system;
modules = [
inputs.srvos.nixosModules.server
inputs.srvos.nixosModules.hardware-amazon
./modules/profiles/common.nix
./modules/mixins/alloy-forwarder
];
format = "amazon"; # ami
};

ecs-node = inputs.nixos-generators.nixosGenerate {
inherit system;
modules = [
inputs.srvos.nixosModules.server
inputs.srvos.nixosModules.hardware-amazon
./modules/profiles/common.nix
./modules/mixins/ecs-agent
];
format = "amazon"; # ami
};

actions-runner = inputs.nixos-generators.nixosGenerate {
inherit system;
modules = [
({...}: { amazonImage.sizeMB = 6 * 1024; })
inputs.srvos.nixosModules.server
inputs.srvos.nixosModules.hardware-amazon
./modules/profiles/common.nix
./modules/mixins/github-actions
];
specialArgs = {
diskSize = 6 * 1024; # 6GB
};
format = "amazon"; # ami
};
};
packages = import ./nix/images.nix { inherit system inputs; };
};
};
}
22 changes: 14 additions & 8 deletions modules/mixins/alloy-forwarder/config.alloy
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import.git "gcloud" {
import.git "grafana_cloud" {
repository = "https://github.com/grafana/alloy-modules.git"
path = "modules/cloud/grafana/cloud/module.river"
path = "modules/cloud/grafana/cloud/module.alloy"
revision = "main"
pull_frequency = "0s"
}

gcloud.stack "default" {
stack_name = env("GRAFANA_CLOUD_STACK")
token = env("GRAFANA_CLOUD_TOKEN")
grafana_cloud.stack "receivers" {
stack_name = env("GRAFANA_CLOUD_STACK")
token = env("GRAFANA_CLOUD_TOKEN")
}

prometheus.receive_http "forward" {
Expand All @@ -22,14 +22,20 @@ prometheus.receive_http "forward" {

prometheus.scrape "linux_node" {
targets = prometheus.exporter.unix.node.targets
forward_to = [
grafana_cloud.stack.receivers.metrics,
]
forward_to = [ grafana_cloud.stack.receivers.metrics ]
}

prometheus.exporter.unix "node" {
}

prometheus.exporter.self "agent" {
}

prometheus.scrape "agent" {
targets = prometheus.exporter.self.agent.targets
forward_to = [ grafana_cloud.stack.receivers.metrics ]
}

loki.source.api "receive" {
http {
listen_address = "0.0.0.0"
Expand Down
22 changes: 17 additions & 5 deletions modules/mixins/alloy-forwarder/default.nix
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
{lib, ...}: {
imports = [../alloy];
{ lib, ... }: {
imports = [ ../alloy ];

# Only change from normal Alloy mixin is an overridden config file
environment.etc."alloy/config.alloy".source = lib.mkForce ./config.alloy;

virtualisation.oci-containers.containers.alloy = {
environmentFiles = ["/run/keys/grafana-cloud"];
networking.firewall.allowedTCPPorts = [
9090 # Prometheus
3100 # Loki
4317 # OTLP/gRPC
4318 # OTLP/HTTP
];

environment = {
networking.firewall.allowedUDPPorts = [
4317 # OTLP/gRPC
];

services.alloy = {
extraArgs = "--stability.level public-preview";

environmentFiles = [ "/run/keys/grafana-cloud" ];
extraEnvironment = {
GRAFANA_CLOUD_STACK = "altf4llc";
};
};
Expand Down
20 changes: 10 additions & 10 deletions modules/mixins/alloy/config.alloy
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,21 @@ prometheus.relabel "instance" {
}

// Export system metrics
prometheus.exporter.unix "host" {
procfs_path = "/host/proc"
sysfs_path = "/host/sys"
rootfs_path = "/rootfs"
udev_data_path = "/host/run/udev/data"

filesystem {
mount_points_exclude = "^/(sys|proc|dev|host|etc)($$|/)"
}
}
prometheus.exporter.unix "host" {}

// Scrape system metrics
prometheus.scrape "host" {
targets = prometheus.exporter.unix.host.targets
forward_to = [prometheus.relabel.instance.receiver]
}

// Export agent metrics
prometheus.exporter.self "agent" {}

// Scrape agent metrics
prometheus.scrape "agent" {
targets = prometheus.exporter.self.agent.targets
forward_to = [prometheus.relabel.instance.receiver]
}

// vim:ft=hcl
47 changes: 8 additions & 39 deletions modules/mixins/alloy/default.nix
Original file line number Diff line number Diff line change
@@ -1,49 +1,18 @@
{...}: {
# see TODO further down
imports = [../docker];
{ pkgs-nix, pkgs, ... }: {
imports = [ pkgs-nix.nixosModules.alloy ];

environment.etc."alloy/config.alloy" = {
source = ./config.alloy;
mode = "0440";
user = "root";
};

# TODO: Replace this once there's an Alloy package merged into Nixpkgs
# https://github.com/NixOS/nixpkgs/pull/306048
virtualisation.oci-containers.containers.alloy = {
autoStart = true;
image = "grafana/alloy:v1.0.0";

services.alloy = {
enable = true;
package = pkgs-nix.packages.${pkgs.system}.alloy;
openFirewall = true;
configPath = "/etc/alloy";
group = "root";
user = "root";

ports = [
"12345:12345"
];

cmd = [
"run"
"--server.http.listen-addr=0.0.0.0:12345"
"--storage.path=/var/lib/alloy/data"
"--stability.level=public-preview"

# we give a path to the directory so it loads every file, instead of
# one config file. this allows us to add extra configuration in other
# mixins.
"/etc/alloy"
];

volumes = [
# Alloy
"/var/log:/var/log:ro"
"/etc/alloy:/etc/alloy:ro"

"/var/lib/alloy/data"

# Node Exporter
"/proc:/host/proc:ro"
"/sys:/host/sys:ro"
"/run/udev/data:/host/run/udev/data:ro"
"/:/rootfs:ro"
];
};
}
2 changes: 1 addition & 1 deletion modules/mixins/docker/default.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{...}: {
{ ... }: {
virtualisation.docker.enable = true;
virtualisation.oci-containers.backend = "docker";

Expand Down
4 changes: 2 additions & 2 deletions modules/mixins/ecs-agent/default.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{...}: {
{ ... }: {
imports = [
../docker
../alloy
Expand Down Expand Up @@ -27,7 +27,7 @@
"--net=host"
];

environmentFiles = ["/run/keys/ecs.config"];
environmentFiles = [ "/run/keys/ecs.config" ];
environment = {
ECS_ENABLE_PROMETHEUS_METRICS = "true";
ECS_LOGLEVEL = "info";
Expand Down
2 changes: 1 addition & 1 deletion modules/mixins/github-actions/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ in
};
};

users.groups.github-runner = {};
users.groups.github-runner = { };
users.users.github-runner = {
group = "github-runner";
extraGroups = [ "docker" ];
Expand Down
2 changes: 0 additions & 2 deletions modules/profiles/common.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{...}: {
services.cachix-agent.enable = true;

boot.loader.efi.canTouchEfiVariables = true;

services.openssh.enable = true;
Expand Down
Loading