Skip to content

Commit a9eabc5

Browse files
committed
ngx-kasha: release v0.0.1
kasha: Log output to JSON file. kasha: Log output to Kafka topic.
0 parents  commit a9eabc5

File tree

11 files changed

+1701
-0
lines changed

11 files changed

+1701
-0
lines changed

CHANGES

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Changes with ngx-kasha 0.0.1 14 Dec 2016
2+
3+
*) Log output to Kafka topic.
4+
5+
Changes with ngx-kasha 0.0.1 11 Dec 2016
6+
7+
*) The first public version.
8+

COPYRIGHT

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
-----------------------------------------------------------------------------
2+
NGINX License
3+
4+
/*
5+
* Copyright (C) 2002-2016 Igor Sysoev
6+
* Copyright (C) 2011-2016 Nginx, Inc.
7+
* All rights reserved.
8+
*
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions
11+
* are met:
12+
* 1. Redistributions of source code must retain the above copyright
13+
* notice, this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright
15+
* notice, this list of conditions and the following disclaimer in the
16+
* documentation and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28+
* SUCH DAMAGE.
29+
*/
30+
-----------------------------------------------------------------------------

LICENSE

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (C) 2016 Paulo Pacheco
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
* 1. Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* 2. Redistributions in binary form must reproduce the above copyright
11+
* notice, this list of conditions and the following disclaimer in the
12+
* documentation and/or other materials provided with the distribution.
13+
*
14+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17+
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24+
* SUCH DAMAGE.
25+
*/

README.md

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
# ngx-kasha
2+
3+
4+
nginx module for advanced per location logging - aka kasha (🍲)
5+
6+
## Description
7+
8+
This module adds to nginx the ability of advanced JSON logging of HTTP requests per location.
9+
It's possible to log to a destination ouput any request made to a specific nginx location.
10+
The output format is configurable.
11+
12+
### Configuration
13+
14+
Each logging configuration is based on a kasha_recipe. (🍲)
15+
16+
A kasha recipe is a ';' separated list of items to include in the logging preparation.
17+
18+
The left hand side part of item will be the JSON Path for the variable name
19+
The left hand side part can be prefixed with 's:', 'i:' or 'r:', so the JSON encoding type can be controlled.
20+
21+
* 's:' - JSON string ( default )
22+
* 'i:' - JSON integer
23+
* 'r:' - JSON real
24+
25+
26+
The right hand side will be the variable's name or literal value.
27+
For this, known or previously setted variables, can be used by using the '$' before name.
28+
29+
Common HTTP nginx builtin variables like $uri, or any other variable set by other handler modules can be used.
30+
31+
The output is sent to the location specified by the first kasha_recipe argument.
32+
The possible output locations are:
33+
34+
* "file:" - The logging location will be a local filesystem file.
35+
* "kafka:" - The logging location will be a Kafka topic.
36+
37+
#### Example Configuration
38+
39+
40+
##### A simple configuration example
41+
42+
```yaml
43+
kasha_recipe file:/tmp/log '
44+
src.ip $remote_addr;
45+
src.port $remote_port;
46+
dst.ip $server_addr;
47+
dst.port $server_port;
48+
_date $time_iso8601;
49+
r:_real 1.1;
50+
i:_int 2016;
51+
i:_status $status;
52+
_literal root;
53+
comm.proto http;
54+
comm.http.method $request_method;
55+
comm.http.path $uri;
56+
comm.http.host $host;
57+
comm.http.server_name $server_name;
58+
';
59+
```
60+
61+
This will produce the following JSON line to '/tmp/log' file .
62+
To ease reading, it's shown here formatted with newlines.
63+
64+
```json
65+
{
66+
"_date": "2016-12-11T18:06:54+00:00",
67+
"_int": 2016,
68+
"_literal": "root",
69+
"_real": 1.1,
70+
"_status": 200,
71+
"comm": {
72+
"http": {
73+
"host": "localhost",
74+
"method": "HEAD",
75+
"path": "/index.html",
76+
"server_name": "localhost"
77+
},
78+
"proto": "http"
79+
},
80+
"dst": {
81+
"ip": "127.0.0.1",
82+
"port": "80"
83+
},
84+
"src": {
85+
"ip": "127.0.0.1",
86+
"port": "52136"
87+
}
88+
}
89+
```
90+
91+
##### A example using perl handler variables.
92+
93+
```yaml
94+
perl_set $bar '
95+
sub {
96+
my $r = shift;
97+
my $uri = $r->uri;
98+
99+
return "yes" if $uri =~ /^\/bar/;
100+
return "";
101+
}';
102+
103+
kasha_recipe file:/tmp/log '
104+
comm.http.server_name $server_name;
105+
perl.bar $bar;
106+
';
107+
```
108+
109+
A request sent to **/bar** .
110+
111+
```json
112+
{
113+
114+
"comm": {
115+
"http": {
116+
"server_name": "localhost"
117+
}
118+
},
119+
"perl": {
120+
"bar": "yes"
121+
}
122+
}
123+
```
124+
125+
### Directives
126+
127+
---
128+
* Syntax: **kasha_recipe** _location_ { _recipe_ };
129+
* Default: —
130+
* Context: http location
131+
132+
###### _location_ ######
133+
134+
Specifies the location for the output...
135+
136+
The output location type should be prefixed with supported location types. ( **file:** or **kafka:** )
137+
138+
For a **file:** type the value part will be a local file name. e.g. **file:**/tmp/log
139+
140+
For a **kafka:** type the value part will be the topic name. e.g. **kafka:** topic
141+
142+
The kafka output only happens if a list of brokers is defined by **kasha_kafka_brokers** directive.
143+
144+
###### _recipe_ ######
145+
146+
See details above.
147+
148+
---
149+
150+
* Syntax: **"kasha_kafka_partition** _compression_codec_;
151+
* Default: RD_KAFKA_PARTITION_UA
152+
* Context: http local
153+
154+
---
155+
156+
* Syntax: **kasha_kafka_brokers** list of brokers separated by spaces;
157+
* Default: —
158+
* Context: http main
159+
160+
---
161+
162+
* Syntax: **kasha_kafka_client_id** _id_;
163+
* Default: kasha
164+
* Context: http main
165+
166+
---
167+
168+
* Syntax: **"kasha_kafka_compression** _compression_codec_;
169+
* Default: snappy
170+
* Context: http main
171+
172+
---
173+
174+
* Syntax: **"kasha_kafka_log_level** _numeric_log_level_;
175+
* Default: 6
176+
* Context: http main
177+
178+
---
179+
180+
* Syntax: **"kasha_kafka_max_retries** _numeric_;
181+
* Default: 0
182+
* Context: http main
183+
184+
---
185+
186+
* Syntax: **"kasha_kafka_buffer_max_messages** _numeric_;
187+
* Default: 100000
188+
* Context: http main
189+
190+
---
191+
192+
* Syntax: **"kasha_kafka_backoff_ms** _numeric_;
193+
* Default: 10
194+
* Context: http main
195+
196+
197+
198+
### Build
199+
200+
#### Dependencies
201+
202+
* [libjansson](http://www.digip.org/jansson/)
203+
* [librdkafka](https://github.com/edenhill/librdkafka)
204+
205+
For Ubuntu or Debian install development packages.
206+
207+
```bash
208+
$ sudo apt-get install libjansson-dev librdkafka-dev
209+
210+
```
211+
212+
Build as a common nginx module.
213+
214+
```bash
215+
$ ./configure --add-module=/build/ngx-kasha
216+
$ make && make install
217+
218+
```
219+
220+
221+
222+
### Tests and Fair Warning
223+
224+
**THIS IS NOT PRODUCTION** ready.
225+
226+
This was done over the weekend as a proof of concept, and it also lacks unit tests.
227+
228+
So there's no guarantee of success. It most probably blow up when running in real life scenarios.
229+

config

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
ngx_addon_name=ngx_kasha_module
2+
ngx_module_incs=$ngx_addon_dir/src
3+
4+
HTTP_MODULES="$HTTP_MODULES ngx_kasha_module"
5+
6+
CORE_INCS="$CORE_INCS $ngx_module_incs"
7+
NGX_ADDON_SRCS="$NGX_ADDON_SRCS \
8+
$ngx_addon_dir/src/ngx_kasha.c \
9+
$ngx_addon_dir/src/ngx_kasha_kafka.c \
10+
$ngx_addon_dir/src/ngx_kasha_str.c"
11+
CORE_LIBS="$CORE_LIBS -ljansson -lrdkafka"

0 commit comments

Comments
 (0)