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

nixos/elastic-apm-server: init #57995

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions nixos/modules/module-list.nix
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@
./services/logging/rsyslogd.nix
./services/logging/syslog-ng.nix
./services/logging/syslogd.nix
./services/logging/beats/apm-server.nix
./services/mail/clamsmtp.nix
./services/mail/davmail.nix
./services/mail/dkimproxy-out.nix
Expand Down
79 changes: 79 additions & 0 deletions nixos/modules/services/logging/beats/apm-server.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{ config, lib, pkgs, ... }:

with lib;

let

beatslib = import ./lib.nix { inherit lib pkgs; };

mkApmServerConfig = cfg: {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to define functions when you only use them for one argument, just define the result directly.


apm-server.host = "${cfg.listenAddress}:${toString cfg.port}";

};


in

{
###### interface

options = {

services.beats.apm-server = recursiveUpdate
(beatslib.mkCommonOptions { name = "apm-server"; defaults = { package = pkgs.elastic-apm-server; }; })
{

listenAddress = mkOption {
description = "Address on which to listen.";
type = types.str;
default = "localhost";
};

port = mkOption {
description = "Port on which to listen.";
type = types.int;
default = 8200;
};

elasticsearch.indices = mkOption {
description = "Array of index selector rules";
type = with types; listOf attrs;
default = [
{
index = "apm-%{[beat.version]}-sourcemap";
when.contains.processor.event = "sourcemap";
}
{
index = "apm-%{[beat.version]}-error-%{+yyyy.MM.dd}";
when.contains.processor.event = "error";
}
{
index = "apm-%{[beat.version]}-transaction-%{+yyyy.MM.dd}";
when.contains.processor.event = "transaction";
}
{
index = "apm-%{[beat.version]}-span-%{+yyyy.MM.dd}";
when.contains.processor.event = "span";
}
{
index = "apm-%{[beat.version]}-metric-%{+yyyy.MM.dd}";
when.contains.processor.event = "metric";
}
{
index = "apm-%{[beat.version]}-onboarding-%{+yyyy.MM.dd}";
when.contains.processor.event = "onboarding";
}
];
};

};

};

config = beatslib.mkNixosConfig {
mkBeatConfig = mkApmServerConfig;
cfg = config.services.beats.apm-server;
};

}
145 changes: 145 additions & 0 deletions nixos/modules/services/logging/beats/lib.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
{ lib, pkgs, ... }:

with lib;

{

mkCommonOptions = { name, defaults }: {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless I'm missing something here, you don't need to abstract this with a library file. Just write an option that you can set multiple times wherever you need it. Something like

{
  options.services.beats = mkOption {
    type = types.attrsOf (types.submodule ({ name, ... }: {
      options = {
        enable = true;
        # ...
      };
    }));
  };
}

And then set services.beats.apm-server.enable = true; and such where you need an instance. This way you also won't duplicate all these options.


enable = mkEnableOption "Enable ${name}.";

package = mkOption {
description = "${name} package to use.";
type = types.package;
default = defaults.package;
};

executable = mkOption {
description = "Name of the executable within <option>package</option> to run.";
type = types.str;
default = defaults.executable or name;
};

name = mkOption {
description = "Name of the beat";
type = types.str;
default = defaults.name or name;
};

tags = mkOption {
description = "Tags to place on the shipped log messages";
type = types.listOf types.str;
default = [];
};

stateDir = mkOption {
description = "The state directory.";
type = types.str;
default = defaults.stateDir or "/var/lib/${name}";
};

loglevel = mkOption {
description = "Logging verbosity level.";
type = types.enum [ "debug" "info" "warning" "error" "fatal" ];
default = "warning";
};

extraConfig = mkOption {
description = "Additional config to be added to the beats config.json.";
type = types.attrs;
default = {};
};

elasticsearch = {
hosts = mkOption {
description = "Elasticsearch hosts";
type = with types; listOf str;
default = [ "localhost:9200" ];
};

username = mkOption {
description = "Username for elasticsearch basic auth.";
type = types.nullOr types.str;
default = null;
};

password = mkOption {
description = "Password for elasticsearch basic auth.";
type = types.nullOr types.str;
default = null;
};
};

kibana = {
host = mkOption {
description = "Host where kibana is reachable.";
type = types.str;
default = "localhost:5601";
};
};

};

mkNixosConfig = { cfg, mkBeatConfig }: let

mkCommonBeatConfig = cfg: {
inherit (cfg) tags;

path = {
data = "${cfg.stateDir}/data";
logs = "${cfg.stateDir}/logs";
};

logging.level = cfg.loglevel;

output = { inherit (cfg) elasticsearch; };

setup.template = {
fields = "${cfg.package}/share/fields.yml";
settings.index = {
number_of_shards = 1;
codec = "best_compression";
};
};

setup.kibana = {
inherit (cfg.kibana) host;
};

setup.dashboards = {
enabled = true;
directory ="${cfg.package}/share/kibana";
};
};

beatConfig = foldl' recursiveUpdate {} [
(mkCommonBeatConfig cfg)
(mkBeatConfig cfg)
cfg.extraConfig
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you're already doing something like that, I really suggest you do this instead: https://github.com/Infinisil/rfcs/blob/nixos-config/rfcs/0000-nixos-config.md#detailed-design, which will have a bunch of advantages

(ping @aanderse)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have opened an RFC since then for that: NixOS/rfcs#42

];
beatConfigJSON = pkgs.writeText "${cfg.name}.json" (builtins.toJSON beatConfig);

in mkIf cfg.enable {

systemd.services.${cfg.name} = {
wantedBy = [ "multi-user.target" ];

after = let
hasLocalKibana = lib.strings.hasInfix "localhost" cfg.kibana.host;
hasLocalElasticsearch = (findSingle (x: lib.strings.hasInfix "localhost" x) null "multiple" cfg.elasticsearch.hosts) != null;
in []
++ optional hasLocalKibana "kibana.service"
++ optional hasLocalElasticsearch "elasticsearch.service";

preStart = ''
mkdir -p ${beatConfig.path.data}
mkdir -p ${beatConfig.path.logs}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use systemd.tmpfiles for this instead

'';

serviceConfig = {
ExecStart = "${cfg.package}/bin/${cfg.executable} -c ${beatConfigJSON}";
};
};
};

}
14 changes: 14 additions & 0 deletions nixos/tests/elk.nix
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ let
unit_count: 1
'';
};

beats.apm-server = {
enable = true;
extraConfig = {
setup.dashboards = {
always_kibana = true;
retry.enabled = true;
};
};
};
};
};
};
Expand Down Expand Up @@ -125,6 +135,10 @@ let
$one->waitUntilSucceeds("curl --silent --show-error '${esUrl}/_search' -H 'Content-Type: application/json' -d '{\"query\" : { \"match\" : { \"message\" : \"flowers\"}}}' | jq .hits.total | grep -v 0");
$one->waitUntilSucceeds("curl --silent --show-error '${esUrl}/_search' -H 'Content-Type: application/json' -d '{\"query\" : { \"match\" : { \"message\" : \"dragons\"}}}' | jq .hits.total | grep 0");

# See if apm-server is healthy.
$one->waitForUnit("apm-server.service");
$one->waitUntilSucceeds("curl --silent --show-error 'http://localhost:8200/' | grep 'ok'");

# Test elasticsearch-curator.
$one->systemctl("stop logstash");
$one->systemctl("start elasticsearch-curator");
Expand Down