Merge pull request #221169 from bouk/bouk/mainpr

opentelemetry-collector: add NixOS module
This commit is contained in:
Domen Kožar 2023-06-21 13:07:50 +01:00 committed by GitHub
commit b37c9b89a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 152 additions and 0 deletions

View File

@ -753,6 +753,7 @@
./services/monitoring/munin.nix
./services/monitoring/nagios.nix
./services/monitoring/netdata.nix
./services/monitoring/opentelemetry-collector.nix
./services/monitoring/parsedmarc.nix
./services/monitoring/prometheus/alertmanager-irc-relay.nix
./services/monitoring/prometheus/alertmanager.nix

View File

@ -0,0 +1,73 @@
{ config, lib, pkgs, ... }:
let
inherit (lib) mkEnableOption mkIf mkOption types getExe;
cfg = config.services.opentelemetry-collector;
opentelemetry-collector = cfg.package;
settingsFormat = pkgs.formats.yaml {};
in {
options.services.opentelemetry-collector = {
enable = mkEnableOption (lib.mdDoc "Opentelemetry Collector");
package = mkOption {
type = types.package;
default = pkgs.opentelemetry-collector;
defaultText = lib.literalExpression "pkgs.opentelemetry-collector";
description = lib.mdDoc "The opentelemetry-collector package to use.";
};
settings = mkOption {
type = settingsFormat.type;
default = {};
description = lib.mdDoc ''
Specify the configuration for Opentelemetry Collector in Nix.
See https://opentelemetry.io/docs/collector/configuration/ for available options.
'';
};
configFile = mkOption {
type = types.nullOr types.path;
default = null;
description = lib.mdDoc ''
Specify a path to a configuration file that Opentelemetry Collector should use.
'';
};
};
config = mkIf cfg.enable {
assertions = [{
assertion = (
(cfg.settings == {}) != (cfg.configFile == null)
);
message = ''
Please specify a configuration for Opentelemetry Collector with either
'services.opentelemetry-collector.settings' or
'services.opentelemetry-collector.configFile'.
'';
}];
systemd.services.opentelemetry-collector = {
description = "Opentelemetry Collector Service Daemon";
wantedBy = [ "multi-user.target" ];
serviceConfig = let
conf = if cfg.configFile == null
then settingsFormat.generate "config.yaml" cfg.settings
else cfg.configFile;
in
{
ExecStart = "${getExe opentelemetry-collector} --config=file:${conf}";
DynamicUser = true;
Restart = "always";
ProtectSystem = "full";
DevicePolicy = "closed";
NoNewPrivileges = true;
WorkingDirectory = "/var/lib/opentelemetry-collector";
StateDirectory = "opentelemetry-collector";
};
};
};
}

View File

@ -560,6 +560,7 @@ in {
openstack-image-metadata = (handleTestOn ["x86_64-linux"] ./openstack-image.nix {}).metadata or {};
openstack-image-userdata = (handleTestOn ["x86_64-linux"] ./openstack-image.nix {}).userdata or {};
opentabletdriver = handleTest ./opentabletdriver.nix {};
opentelemetry-collector = handleTest ./opentelemetry-collector.nix {};
owncast = handleTest ./owncast.nix {};
outline = handleTest ./outline.nix {};
image-contents = handleTest ./image-contents.nix {};

View File

@ -0,0 +1,76 @@
import ./make-test-python.nix ({ pkgs, ...} : let
port = 4318;
in {
name = "opentelemetry-collector";
meta = with pkgs.lib.maintainers; {
maintainers = [ tylerjl ];
};
nodes.machine = { ... }: {
networking.firewall.allowedTCPPorts = [ port ];
services.opentelemetry-collector = {
enable = true;
settings = {
exporters.logging.verbosity = "detailed";
receivers.otlp.protocols.http = {};
service = {
pipelines.logs = {
receivers = [ "otlp" ];
exporters = [ "logging" ];
};
};
};
};
virtualisation.forwardPorts = [{
host.port = port;
guest.port = port;
}];
};
extraPythonPackages = p: [
p.requests
p.types-requests
];
# Send a log event through the OTLP pipeline and check for its
# presence in the collector logs.
testScript = /* python */ ''
import requests
import time
from uuid import uuid4
flag = str(uuid4())
machine.wait_for_unit("opentelemetry-collector.service")
machine.wait_for_open_port(${toString port})
event = {
"resourceLogs": [
{
"resource": {"attributes": []},
"scopeLogs": [
{
"logRecords": [
{
"timeUnixNano": str(time.time_ns()),
"severityNumber": 9,
"severityText": "Info",
"name": "logTest",
"body": {
"stringValue": flag
},
"attributes": []
},
]
}
]
}
]
}
response = requests.post("http://localhost:${toString port}/v1/logs", json=event)
assert response.status_code == 200
assert flag in machine.execute("journalctl -u opentelemetry-collector")[-1]
'';
})

View File

@ -42,5 +42,6 @@ buildGoModule rec {
'';
license = licenses.asl20;
maintainers = with maintainers; [ uri-canva jk ];
mainProgram = "otelcorecol";
};
}