about summary refs log tree commit diff
path: root/nixpkgs/nixos/tests/opentelemetry-collector.nix
blob: 9a56a22ca47ebf61b725de577030a9d40cd500d6 (plain) (blame)
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
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]
  '';
})