-
-
Notifications
You must be signed in to change notification settings - Fork 14.3k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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: { | ||
|
||
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; | ||
}; | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
{ lib, pkgs, ... }: | ||
|
||
with lib; | ||
|
||
{ | ||
|
||
mkCommonOptions = { name, defaults }: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
''; | ||
|
||
serviceConfig = { | ||
ExecStart = "${cfg.package}/bin/${cfg.executable} -c ${beatConfigJSON}"; | ||
}; | ||
}; | ||
}; | ||
|
||
} |
There was a problem hiding this comment.
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.