Skip to content

Commit e53e54a

Browse files
authored
Merge pull request #4 from amineo/master
Add CloudFront stream to Kinesis log format
2 parents 470a10f + 0ec7402 commit e53e54a

File tree

5 files changed

+90
-3
lines changed

5 files changed

+90
-3
lines changed

README.md

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ fs.createReadStream('./somelogfile.gz')
5555

5656
### Options
5757

58-
Only two configuration options are currently in effect: format and version. The parser defaults to `web` to handle the web distribution file format. If logs are from an RTMP distribution, this value should be set to `rtmp`. Currently all CloudFront logs are on version 1.0; should future versions appear, the `version` option will serve as an override.
58+
Only two configuration options are currently in effect: format and version. The parser defaults to `web` to handle the web distribution file format. If logs are streaming from CloudFront to Kinesis the format should be set to `kinesis` as the column headers and order changes. If logs are from an RTMP distribution, this value should be set to `rtmp`. Currently all CloudFront logs are on version 1.0; should future versions appear, the `version` option will serve as an override.
5959

6060
```javascript
6161
const options = {
62-
format: 'web|rtmp',
62+
format: 'web|rtmp|kinesis',
6363
version: '1.0'
6464
};
6565
```
@@ -95,6 +95,51 @@ const options = {
9595
'cs-protocol-version': 'HTTP/1.1' }
9696
```
9797

98+
99+
### CloudFront to Kinesis Distribution Format
100+
```javascript
101+
{ 'timestamp': '1607374321.541',
102+
'c-ip': '127.0.0.1',
103+
'time-to-first-byte': '0.042',
104+
'sc-status': '200',
105+
'sc-bytes': '485',
106+
'cs-method': 'GET',
107+
'cs-protocol': 'http',
108+
'cs-host': 'test.cloudfront.net',
109+
'cs-uri-stem': '/i?hello=1',
110+
'cs-bytes': '745',
111+
'x-edge-location': 'EWR52-C4',
112+
'x-edge-request-id': '6PfZe0cc_AjXUjFuGnL9pGOmFdUx8xR8ZU8nr44JYJWi-DaeJjcxkw==',
113+
'x-host-header': 'test.cloudfront.net',
114+
'time-taken': '0.042',
115+
'cs-protocol-version': 'HTTP/1.1',
116+
'c-ip-version': 'IPv4',
117+
'cs-user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:83.0) Gecko/20100101 Firefox/83.0',
118+
'cs-referer': 'http://localhost:5000/page-2',
119+
'cs-cookie': '-',
120+
'query-params': 'hello=1607374321563',
121+
'x-edge-response-result-type': 'Miss',
122+
'x-forwarded-for': '-',
123+
'ssl-protocol': '-',
124+
'ssl-cipher': '-',
125+
'x-edge-result-type': 'Miss',
126+
'fle-encrypted-fields': '-',
127+
'fle-status': '-',
128+
'sc-content-type': 'image/gif',
129+
'sc-content-len': '35',
130+
'sc-range-start': '-',
131+
'sc-range-end': '-',
132+
'c-port': '49323',
133+
'x-edge-detailed-result-type': 'Miss',
134+
'c-country': 'US',
135+
'cs-accept-encoding': 'gzip, deflate',
136+
'cs-accept': 'image/webp,*/*',
137+
'cache-behavior-path-pattern': '*',
138+
'cs-headers': 'Host:test.cloudfront.net\n...',
139+
'cs-headers-count': '8'
140+
}
141+
```
142+
98143
### RTMP Distribution Format
99144

100145
```javascript

index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,21 @@ const formats = {
2222
'sc-bytes', 'x-cf-status', 'x-cf-client-id', 'cs-uri-stem',
2323
'cs-uri-query', 'c-referrer', 'x-page-url', 'c-user-agent',
2424
'x-sname', 'x-sname-query', 'x-file-ext', 'x-sid'
25+
],
26+
27+
'kinesis_v1.0': [
28+
'timestamp', 'c-ip', 'time-to-first-byte', 'sc-status',
29+
'sc-bytes', 'cs-method', 'cs-protocol', 'cs-host',
30+
'cs-uri-stem', 'cs-bytes', 'x-edge-location', 'x-edge-request-id',
31+
'x-host-header', 'time-taken', 'cs-protocol-version', 'c-ip-version',
32+
'cs-user-agent', 'cs-referer', 'cs-cookie', 'cs-uri-query',
33+
'x-edge-response-result-type', 'x-forwarded-for', 'ssl-protocol', 'ssl-cipher',
34+
'x-edge-result-type', 'fle-encrypted-fields', 'fle-status', 'sc-content-type',
35+
'sc-content-len', 'sc-range-start', 'sc-range-end', 'c-port',
36+
'x-edge-detailed-result-type', 'c-country', 'cs-accept-encoding', 'cs-accept',
37+
'cache-behavior-path-pattern', 'cs-headers', 'cs-header-names', 'cs-headers-count'
2538
]
39+
2640
};
2741

2842
const option_defaults = {

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/kinesis-1-0.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#Version: 1.0
2+
#Fields: timestamp c-ip time-to-first-byte sc-status sc-bytes cs-method cs-protocol cs-host cs-uri-stem cs-bytes x-edge-location x-edge-request-id x-host-header time-taken cs-protocol-version c-ip-version cs-user-agent cs-referer cs-cookie cs-uri-query x-edge-response-result-type x-forwarded-for ssl-protocol ssl-cipher x-edge-result-type fle-encrypted-fields fle-status sc-content-type sc-content-len sc-range-start sc-range-end c-port x-edge-detailed-result-type c-country cs-accept-encoding cs-accept cache-behavior-path-pattern cs-headers cs-header-names cs-headers-count
3+
1607374321.541 127.0.0.1 0.042 200 485 GET http test.cloudfront.net /i?stm=1607374321563&e=pp&url=http%253A%252F%252Flocalhost%253A5000%252Fpage-2&refr=http%253A%252F%252Flocalhost%253A5000%252F&pp_mix=0&pp_max=0&pp_miy=0&pp_may=0&tv=js-2.6.2&tna=cf&aid=site&p=web&tz=America%252FNew_York&lang=en-US&cs=UTF-8&res=3840x1600&cd=24&cookie=1&eid=d778127d-4ddf-4c04-90af-ffccc980eee8&dtm=1607374321561&vp=2450x1431&ds=2450x1431&vid=5&sid=a88ec782-713b-4e0d-b2da-c408e5730834&duid=eaa664f5-8ba9-41e9-9c98-a2ad088ca440&fp=2033130908 745 EWR52-C4 6PfZe0cc_AjXUjFuGnL9pGOmFdUx8xR8ZU8nr44JYJWi-DaeJjcxkw== test.cloudfront.net 0.042 HTTP/1.1 IPv4 Mozilla/5.0%20(Macintosh;%20Intel%20Mac%20OS%20X%2010.15;%20rv:83.0)%20Gecko/20100101%20Firefox/83.0 http://localhost:5000/page-2 - stm=1607374321563&e=pp&url=http%253A%252F%252Flocalhost%253A5000%252Fpage-2&refr=http%253A%252F%252Flocalhost%253A5000%252F&pp_mix=0&pp_max=0&pp_miy=0&pp_may=0&tv=js-2.6.2&tna=cf&aid=site&p=web&tz=America%252FNew_York&lang=en-US&cs=UTF-8&res=3840x1600&cd=24&cookie=1&eid=d778127d-4ddf-4c04-90af-ffccc980eee8&dtm=1607374321561&vp=2450x1431&ds=2450x1431&vid=5&sid=a88ec782-713b-4e0d-b2da-c408e5730834&duid=eaa664f5-8ba9-41e9-9c98-a2ad088ca440&fp=2033130908 Miss - - - Miss - - image/gif 35 - - 49323 Miss US gzip,%20deflate image/webp,*/* * Host:test.cloudfront.net%0AUser-Agent:Mozilla/5.0%20(Macintosh;%20Intel%20Mac%20OS%20X%2010.15;%20rv:83.0)%20Gecko/20100101%20Firefox/83.0%0AAccept:image/webp,*/*%0AAccept-Language:en-US,en;q=0.5%0AAccept-Encoding:gzip,%20deflate%0ADNT:1%0AConnection:keep-alive%0AReferer:http://localhost:5000/page-2%0A Host%0AUser-Agent%0AAccept%0AAccept-Language%0AAccept-Encoding%0ADNT%0AConnection%0AReferer%0A 8

test/parse.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const CloudFrontParser = require('../');
1111
const web_example = fs.readFileSync(path.join(__dirname, './fixtures/web-1-0.txt'), 'utf-8');
1212
const web_example1 = fs.readFileSync(path.join(__dirname, './fixtures/web-1-0-2019-12.txt'), 'utf-8');
1313
const rtmp_example = fs.readFileSync(path.join(__dirname, './fixtures/rtmp-1-0.txt'), 'utf-8');
14+
const kinesis_example = fs.readFileSync(path.join(__dirname, './fixtures/kinesis-1-0.txt'), 'utf-8');
1415

1516
describe('parse', function () {
1617

@@ -26,6 +27,11 @@ describe('parse', function () {
2627
CloudFrontParser.parse(rtmp_example, { format: 'rtmp' });
2728
});
2829

30+
it('should parse Kinesis to Cloudfront v1.0 logs without error', function () {
31+
CloudFrontParser.parse(kinesis_example, { format: 'kinesis' });
32+
});
33+
34+
2935
it('should create a single object out of each line of web log, ignoring comments', function () {
3036
const result = CloudFrontParser.parse(web_example, { format: 'web' });
3137
assert.equal(2, result.length);
@@ -36,6 +42,12 @@ describe('parse', function () {
3642
assert.equal(6, result.length);
3743
});
3844

45+
46+
it('should create a single object out of each line of kinesis log, ignoring comments', function () {
47+
const result = CloudFrontParser.parse(kinesis_example, { format: 'kinesis' });
48+
assert.equal(1, result.length);
49+
});
50+
3951
it('should default to web if format unspecified', function () {
4052
const result = CloudFrontParser.parse(web_example);
4153

@@ -77,6 +89,19 @@ describe('parse', function () {
7789
assert.equal('disconnect', result[5]['x-event']);
7890
});
7991

92+
93+
it('should map each kinesis log field into correct result field', function () {
94+
const result = CloudFrontParser.parse(kinesis_example, { format: 'kinesis' });
95+
96+
assert.equal('1607374321.541', result[0]['timestamp']);
97+
assert.equal('127.0.0.1', result[0]['c-ip']);
98+
assert.equal('0.042', result[0]['time-to-first-byte']);
99+
assert.equal('200', result[0]['sc-status']);
100+
assert.equal('Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:83.0) Gecko/20100101 Firefox/83.0', result[0]['cs-user-agent']);
101+
});
102+
103+
104+
80105
it('should correctly decode percent-encoded fields', function () {
81106
const result = CloudFrontParser.parse(web_example, { format: 'web' });
82107
assert.equal('Mozilla/5.0 (iPhone; CPU iPhone OS 10_2_1 like Mac OS X) AppleWebKit/602.4.6 (KHTML, like Gecko) Version/10.0 Mobile/14D27 Safari/602.1', result[0]['cs-user-agent']);

0 commit comments

Comments
 (0)