summaryrefslogtreecommitdiffstats
path: root/nixos/tests/opentelemetry-collector.nix
blob: 5498bbdd5fa9b911fe4ff46bdf7adf7c9ab7d14c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
{ 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.debug.verbosity = "detailed";
          receivers.otlp.protocols = {
            http.endpoint = "0.0.0.0:${toString port}";
          };
          service = {
            pipelines.logs = {
              receivers = [ "otlp" ];
              exporters = [ "debug" ];
            };
          };
        };
      };
      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]
    '';
}