summaryrefslogtreecommitdiffstats
path: root/nixos/tests/vector/nginx-clickhouse.nix
blob: 38dc0c7991c9d2443a7e4dd0b74616f961808118 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
{ lib, pkgs, ... }:

{
  name = "vector-nginx-clickhouse";
  meta.maintainers = [ pkgs.lib.maintainers.happysalada ];

  nodes = {
    clickhouse =
      { config, pkgs, ... }:
      {
        virtualisation.memorySize = 4096;

        # Clickhouse module can't listen on a non-loopback IP.
        networking.firewall.allowedTCPPorts = [ 6000 ];
        services.clickhouse.enable = true;

        # Exercise Vector sink->source for now.
        services.vector = {
          enable = true;

          settings = {
            sources = {
              vector_source = {
                type = "vector";
                address = "[::]:6000";
              };
            };

            sinks = {
              clickhouse = {
                type = "clickhouse";
                inputs = [ "vector_source" ];
                endpoint = "http://localhost:8123";
                database = "nginxdb";
                table = "access_logs";
                skip_unknown_fields = true;
              };
            };
          };
        };
      };

    nginx =
      { config, pkgs, ... }:
      {
        services.nginx = {
          enable = true;
          virtualHosts.localhost = { };
        };

        services.vector = {
          enable = true;

          settings = {
            sources = {
              nginx_logs = {
                type = "file";
                include = [ "/var/log/nginx/access.log" ];
                read_from = "end";
              };
            };

            sinks = {
              vector_sink = {
                type = "vector";
                inputs = [ "nginx_logs" ];
                address = "clickhouse:6000";
              };
            };
          };
        };

        systemd.services.vector.serviceConfig = {
          SupplementaryGroups = [ "nginx" ];
        };
      };
  };

  testScript =
    let
      # work around quote/substitution complexity by Nix, Perl, bash and SQL.
      databaseDDL = pkgs.writeText "database.sql" "CREATE DATABASE IF NOT EXISTS nginxdb";

      tableDDL = pkgs.writeText "table.sql" ''
        CREATE TABLE IF NOT EXISTS  nginxdb.access_logs (
          message String
        )
        ENGINE = MergeTree()
        ORDER BY tuple()
      '';

      # Graciously taken from https://clickhouse.com/docs/en/integrations/vector
      tableView = pkgs.writeText "table-view.sql" ''
        CREATE MATERIALIZED VIEW nginxdb.access_logs_view
        (
          RemoteAddr String,
          Client String,
          RemoteUser String,
          TimeLocal DateTime,
          RequestMethod String,
          Request String,
          HttpVersion String,
          Status Int32,
          BytesSent Int64,
          UserAgent String
        )
        ENGINE = MergeTree()
        ORDER BY RemoteAddr
        POPULATE AS
        WITH
         splitByWhitespace(message) as split,
         splitByRegexp('\S \d+ "([^"]*)"', message) as referer
        SELECT
          split[1] AS RemoteAddr,
          split[2] AS Client,
          split[3] AS RemoteUser,
          parseDateTimeBestEffort(replaceOne(trim(LEADING '[' FROM split[4]), ':', ' ')) AS TimeLocal,
          trim(LEADING '"' FROM split[6]) AS RequestMethod,
          split[7] AS Request,
          trim(TRAILING '"' FROM split[8]) AS HttpVersion,
          split[9] AS Status,
          split[10] AS BytesSent,
          trim(BOTH '"' from referer[2]) AS UserAgent
        FROM
          (SELECT message FROM nginxdb.access_logs)
      '';

      selectQuery = pkgs.writeText "select.sql" "SELECT * from nginxdb.access_logs_view";
    in
    ''
      clickhouse.wait_for_unit("clickhouse")
      clickhouse.wait_for_open_port(8123)

      clickhouse.wait_until_succeeds(
        "journalctl -o cat -u clickhouse.service | grep 'Started ClickHouse server'"
      )

      clickhouse.wait_for_unit("vector")
      clickhouse.wait_for_open_port(6000)

      clickhouse.succeed(
        "cat ${databaseDDL} | clickhouse-client"
      )

      clickhouse.succeed(
        "cat ${tableDDL} | clickhouse-client"
      )

      clickhouse.succeed(
        "cat ${tableView} | clickhouse-client"
      )

      nginx.wait_for_unit("nginx")
      nginx.wait_for_open_port(80)
      nginx.wait_for_unit("vector")
      nginx.wait_until_succeeds(
        "journalctl -o cat -u vector.service | grep 'Starting file server'"
      )

      nginx.succeed("curl http://localhost/")
      nginx.succeed("curl http://localhost/")

      nginx.wait_for_file("/var/log/nginx/access.log")
      nginx.wait_until_succeeds(
        "journalctl -o cat -u vector.service | grep 'Found new file to watch. file=/var/log/nginx/access.log'"
      )

      clickhouse.wait_until_succeeds(
        "cat ${selectQuery} | clickhouse-client | grep 'curl'"
      )
    '';
}