-
Notifications
You must be signed in to change notification settings - Fork 8
/
overall.py
836 lines (731 loc) · 39.6 KB
/
overall.py
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
# -*- coding: utf-8 -*-
from hamcrest import *
from amplify.agent.collectors.nginx.accesslog import NginxAccessLogsCollector
from test.base import NginxCollectorTestCase
__author__ = "Mike Belov"
__copyright__ = "Copyright (C) Nginx, Inc. All rights reserved."
__license__ = ""
__maintainer__ = "Mike Belov"
__email__ = "dedm@nginx.com"
class LogsOverallTestCase(NginxCollectorTestCase):
def test_combined(self):
lines = [
'178.23.225.78 - - [18/Jun/2015:17:22:25 +0000] "GET /img/docker.png HTTP/1.1" 304 0 ' +
'"http://ec2-54-78-3-178.eu-west-1.compute.amazonaws.com:4000/" ' +
'"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) ' +
'Chrome/43.0.2357.124 Safari/537.36"',
'178.23.225.78 - - [18/Jun/2015:17:22:25 +0000] "GET /api/inventory/objects/ HTTP/1.1" 200 1093 ' +
'"http://ec2-54-78-3-178.eu-west-1.compute.amazonaws.com:4000/" ' +
'"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) ' +
'Chrome/43.0.2357.124 Safari/537.36"',
'127.0.0.1 - - [18/Jun/2015:17:22:33 +0000] "POST /1.0/589fjinijenfirjf/meta/ HTTP/1.1" ' +
'202 2 "-" "python-requests/2.2.1 CPython/2.7.6 Linux/3.13.0-48-generic"',
'52.6.158.18 - - [18/Jun/2015:17:22:40 +0000] "GET /#/objects HTTP/1.1" 416 84 ' +
'"-" "Slackbot-LinkExpanding 1.0 (+https://api.slack.com/robots)"'
]
collector = NginxAccessLogsCollector(object=self.fake_object, tail=lines)
collector.collect()
# check
metrics = self.fake_object.statsd.flush()['metrics']
assert_that(metrics, has_item('counter'))
# counters
counter = metrics['counter']
for key in ('C|nginx.http.method.get', 'C|nginx.http.request.body_bytes_sent', 'C|nginx.http.status.3xx',
'C|nginx.http.status.2xx','C|nginx.http.method.post', 'C|nginx.http.v1_1',
'C|nginx.http.status.4xx'):
assert_that(counter, has_key(key))
# values
assert_that(counter['C|nginx.http.method.get'][0][1], equal_to(3))
assert_that(counter['C|nginx.http.status.2xx'][0][1], equal_to(2))
assert_that(counter['C|nginx.http.v1_1'][0][1], equal_to(4))
assert_that(counter['C|nginx.http.request.body_bytes_sent'][0][1], equal_to(84 + 2 + 1093 + 0))
# check zero values
for counter_name, counter_key in collector.counters.iteritems():
if counter_key in collector.parser.keys:
assert_that(counter, has_key('C|%s' % counter_name))
if counter_name not in (
'nginx.http.method.get',
'nginx.http.method.post',
'nginx.http.status.2xx',
'nginx.http.status.3xx',
'nginx.http.status.4xx',
'nginx.http.v1_1',
'nginx.http.request.body_bytes_sent'
):
assert_that(counter['C|%s' % counter_name][0][1], equal_to(0))
elif counter_key is not None:
if counter_key not in collector.parser.request_variables:
assert_that(counter, not_(has_key('C|%s' % counter_name)))
def test_extended(self):
log_format = '$remote_addr - $remote_user [$time_local] ' + \
'"$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" ' + \
'rt=$request_time ut="$upstream_response_time" cs=$upstream_cache_status'
lines = [
'1.2.3.4 - - [22/Jan/2010:19:34:21 +0300] "GET /foo/ HTTP/1.1" 200 11078 ' +
'"http://www.rambler.ru/" "Mozilla/5.0 (Windows; U; Windows NT 5.1" rt=0.010 ut="2.001, 0.345" cs=MISS',
'1.2.3.4 - - [22/Jan/2010:20:34:21 +0300] "GET /foo/ HTTP/1.1" 300 1078 ' +
'"http://www.rambler.ru/" "Mozilla/5.0 (Windows; U; Windows NT 5.1" rt=0.010 ut="2.002" cs=HIT',
]
collector = NginxAccessLogsCollector(object=self.fake_object, log_format=log_format, tail=lines)
collector.collect()
# check
metrics = self.fake_object.statsd.flush()['metrics']
assert_that(metrics, has_item('counter'))
assert_that(metrics, has_item('timer'))
# counter keys
counter = metrics['counter']
for key in ['C|nginx.http.method.get', 'C|nginx.http.v1_1', 'C|nginx.upstream.next.count',
'C|nginx.upstream.request.count', 'C|nginx.http.status.3xx', 'C|nginx.cache.miss',
'C|nginx.http.status.2xx', 'C|nginx.http.request.body_bytes_sent', 'C|nginx.cache.hit']:
assert_that(counter, has_key(key))
# timer keys
timer = metrics['timer']
for key in ['G|nginx.upstream.response.time.pctl95', 'C|nginx.upstream.response.time.count',
'C|nginx.http.request.time.count', 'G|nginx.http.request.time',
'G|nginx.http.request.time.pctl95', 'G|nginx.http.request.time.median',
'G|nginx.http.request.time.max', 'G|nginx.upstream.response.time',
'G|nginx.upstream.response.time.median', 'G|nginx.upstream.response.time.max']:
assert_that(timer, has_key(key))
# values
assert_that(counter['C|nginx.http.method.get'][0][1], equal_to(2))
assert_that(counter['C|nginx.upstream.request.count'][0][1], equal_to(2))
assert_that(counter['C|nginx.upstream.next.count'][0][1], equal_to(1))
assert_that(timer['G|nginx.upstream.response.time.max'][0][1], equal_to(2.001+0.345))
# check zero values
for counter_name, counter_key in collector.counters.iteritems():
if counter_key in collector.parser.keys:
assert_that(counter, has_key('C|%s' % counter_name))
if counter_name not in (
'nginx.http.status.2xx',
'nginx.http.status.3xx',
'nginx.http.method.get',
'nginx.upstream.request.count',
'nginx.upstream.next.count',
'nginx.upstream.response.time.max',
'nginx.http.v1_1',
'nginx.cache.miss',
'nginx.cache.hit',
'nginx.http.request.body_bytes_sent'
):
assert_that(counter['C|%s' % counter_name][0][1], equal_to(0))
elif counter_key is not None:
if counter_key not in collector.parser.request_variables:
assert_that(counter, not_(has_key('C|%s' % counter_name)))
def test_extend_duplicates(self):
"""
Test a log format that defines duplicate variables.
"""
log_format = '$remote_addr - $remote_addr - $remote_addr - $remote_user [$time_local] ' + \
'"$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" ' + \
'rt=$request_time ut="$upstream_response_time" cs=$upstream_cache_status'
lines = [
'1.2.3.4 - 1.2.3.4 - 1.2.3.4 - - [22/Jan/2010:19:34:21 +0300] "GET /foo/ HTTP/1.1" 200 11078 ' +
'"http://www.rambler.ru/" "Mozilla/5.0 (Windows; U; Windows NT 5.1" rt=0.010 ut="2.001, 0.345" cs=MISS',
'1.2.3.4 - 1.2.3.4 - 1.2.3.4 - - [22/Jan/2010:20:34:21 +0300] "GET /foo/ HTTP/1.1" 300 1078 ' +
'"http://www.rambler.ru/" "Mozilla/5.0 (Windows; U; Windows NT 5.1" rt=0.010 ut="2.002" cs=HIT',
]
collector = NginxAccessLogsCollector(object=self.fake_object, log_format=log_format, tail=lines)
collector.collect()
# check
metrics = self.fake_object.statsd.flush()['metrics']
assert_that(metrics, has_item('counter'))
assert_that(metrics, has_item('timer'))
# counter keys
counter = metrics['counter']
for key in ['C|nginx.http.method.get', 'C|nginx.http.v1_1', 'C|nginx.upstream.next.count',
'C|nginx.upstream.request.count', 'C|nginx.http.status.3xx', 'C|nginx.cache.miss',
'C|nginx.http.status.2xx', 'C|nginx.http.request.body_bytes_sent', 'C|nginx.cache.hit']:
assert_that(counter, has_key(key))
# timer keys
timer = metrics['timer']
for key in ['G|nginx.upstream.response.time.pctl95', 'C|nginx.upstream.response.time.count',
'C|nginx.http.request.time.count', 'G|nginx.http.request.time',
'G|nginx.http.request.time.pctl95', 'G|nginx.http.request.time.median',
'G|nginx.http.request.time.max', 'G|nginx.upstream.response.time',
'G|nginx.upstream.response.time.median', 'G|nginx.upstream.response.time.max']:
assert_that(timer, has_key(key))
# values
assert_that(counter['C|nginx.http.method.get'][0][1], equal_to(2))
assert_that(counter['C|nginx.upstream.request.count'][0][1], equal_to(2))
assert_that(counter['C|nginx.upstream.next.count'][0][1], equal_to(1))
assert_that(timer['G|nginx.upstream.response.time.max'][0][1], equal_to(2.001+0.345))
# check zero values
for counter_name, counter_key in collector.counters.iteritems():
if counter_key in collector.parser.keys:
assert_that(counter, has_key('C|%s' % counter_name))
if counter_name not in (
'nginx.http.status.2xx',
'nginx.http.status.3xx',
'nginx.http.method.get',
'nginx.upstream.request.count',
'nginx.upstream.next.count',
'nginx.upstream.response.time.max',
'nginx.http.v1_1',
'nginx.cache.miss',
'nginx.cache.hit',
'nginx.http.request.body_bytes_sent'
):
assert_that(counter['C|%s' % counter_name][0][1], equal_to(0))
elif counter_key is not None:
if counter_key not in collector.parser.request_variables:
assert_that(counter, not_(has_key('C|%s' % counter_name)))
def test_extend_duplicates_reported(self):
"""
Test the specific reported bug format.
"""
log_format = '$remote_addr - $remote_user [$time_local] ' + \
'"$request" $status $body_bytes_sent ' + \
'"$http_referer" "$http_user_agent" ' + \
'$request_length $body_bytes_sent'
lines = [
'188.165.1.1 - - [17/Nov/2015:22:07:42 +0100] ' +
'"GET /2014/09/quicktipp-phpmyadmin-update-script/?pk_campaign=feed&pk_kwd=quicktipp-' +
'phpmyadmin-update-script HTTP/1.1" ' +
'200 41110 "http://www.google.co.uk/url?sa=t&source=web&cd=1" "Mozilla/5.0 (Windows NT 6.1; WOW64) ' +
'AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.92 Safari/535.2" 327 41110',
'192.168.100.200 - - [17/Nov/2015:22:09:26 +0100] "POST /wp-cron.php?doing_wp_cron=1447794566.' +
'5160338878631591796875 HTTP/1.0" 200 0 "-" "WordPress/4.3.1; http://my.domain.at.private.com" 281 0'
]
collector = NginxAccessLogsCollector(object=self.fake_object, log_format=log_format, tail=lines)
collector.collect()
# check
metrics = self.fake_object.statsd.flush()['metrics']
assert_that(metrics, has_item('counter'))
# counter keys
counter = metrics['counter']
for key in ['C|nginx.http.method.post', 'C|nginx.http.method.get', 'C|nginx.http.status.2xx',
'C|nginx.http.v1_1', 'C|nginx.http.request.body_bytes_sent',
'C|nginx.http.v1_0']:
assert_that(counter, has_key(key))
# average keys
averages = metrics['average']
assert_that(averages, has_key('G|nginx.http.request.length'))
# values
assert_that(counter['C|nginx.http.method.post'][0][1], equal_to(1))
assert_that(counter['C|nginx.http.method.get'][0][1], equal_to(1))
assert_that(counter['C|nginx.http.status.2xx'][0][1], equal_to(2))
assert_that(counter['C|nginx.http.request.body_bytes_sent'][0][1], equal_to(41110))
assert_that(averages['G|nginx.http.request.length'][0][1], equal_to((327+281)/2))
# check zero values
for counter_name, counter_key in collector.counters.iteritems():
if counter_key in collector.parser.keys:
assert_that(counter, has_key('C|%s' % counter_name))
if counter_name not in (
'nginx.http.status.2xx',
'nginx.http.method.post',
'nginx.http.method.get',
'nginx.http.request.length',
'nginx.http.request.body_bytes_sent',
'nginx.http.v1_1',
'nginx.http.v1_0'
):
assert_that(counter['C|%s' % counter_name][0][1], equal_to(0))
elif counter_key is not None:
if counter_key not in collector.parser.request_variables:
assert_that(counter, not_(has_key('C|%s' % counter_name)))
def test_variables_with_numbers(self):
"""
Account for nginx variables with numbers in them
"""
log_format = '$remote_addr [$time_local] $status $geoip_country_code ' + \
'$geoip_country_code3 "$geoip_country_name"'
lines = [
'10.10.10.102 [29/Jun/2016:23:31:07 +0000] 200 US USA "United States"',
'10.10.10.46 [29/Jun/2016:23:34:33 +0000] 200 CA CAN "Canada"',
'10.10.10.189 [29/Jun/2016:23:34:42 +0000] 200 IE IRL "Ireland"',
'10.10.10.194 [29/Jun/2016:23:35:02 +0000] 200 NL NLD "Netherlands"',
'10.10.10.198 [29/Jun/2016:23:37:08 +0000] 200 FR FRA "France"',
'10.10.10.232 [29/Jun/2016:23:37:49 +0000] 200 SG SGP "Singapore"',
'10.10.10.100 [29/Jun/2016:23:38:19 +0000] 200 ID IDN "Indonesia"'
]
collector = NginxAccessLogsCollector(object=self.fake_object, log_format=log_format, tail=lines)
collector.collect()
# Make sure that variable name with number is properly formatted...
keys = collector.parser.keys
assert_that(keys, has_item('geoip_country_code'))
assert_that(keys, has_item('geoip_country_code3'))
# check
metrics = self.fake_object.statsd.flush()['metrics']
assert_that(metrics, has_item('counter'))
# check some values
counter = metrics['counter']
assert_that(counter['C|nginx.http.status.2xx'][0][1], equal_to(7))
def test_cache_states(self):
log_format = '$remote_addr - $remote_user [$time_local] ' + \
'"$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" ' + \
'rt=$request_time ut="$upstream_response_time" cs=$upstream_cache_status'
lines = [
'1.2.3.4 - - [22/Jan/2010:19:34:21 +0300] "GET /foo/ HTTP/1.1" 200 11078 ' +
'"http://www.yandex.ru/" "Mozilla/5.0 (Windows; U; Windows NT 5.1" rt=0.010 ut="2.001, 0.345" cs=MISS',
'1.2.3.4 - - [22/Jan/2010:20:34:21 +0300] "GET /foo/ HTTP/1.1" 300 1078 ' +
'"http://www.rambler.ru/" "Mozilla/5.0 (Windows; U; Windows NT 5.1" rt=0.010 ut="2.002" cs=HIT',
'1.2.3.4 - - [22/Jan/2010:20:34:21 +0300] "GET /foo/ HTTP/1.1" 400 1078 ' +
'"http://www.mail.ru/" "Mozilla/5.0 (Windows; U; Windows NT 5.1" rt=0.010 ut="2.002" cs=BLAHBLAHFOO',
]
collector = NginxAccessLogsCollector(object=self.fake_object, log_format=log_format, tail=lines)
collector.collect()
# check
metrics = self.fake_object.statsd.flush()['metrics']
assert_that(metrics, has_item('counter'))
assert_that(metrics, has_item('timer'))
# counter keys
counter = metrics['counter']
for key in ['C|nginx.http.method.get', 'C|nginx.http.v1_1', 'C|nginx.upstream.next.count',
'C|nginx.upstream.request.count', 'C|nginx.http.status.3xx', 'C|nginx.cache.miss',
'C|nginx.http.status.2xx', 'C|nginx.http.request.body_bytes_sent', 'C|nginx.cache.hit']:
assert_that(counter, has_key(key))
# timer keys
timer = metrics['timer']
for key in ['G|nginx.upstream.response.time.pctl95', 'C|nginx.upstream.response.time.count',
'C|nginx.http.request.time.count', 'G|nginx.http.request.time',
'G|nginx.http.request.time.pctl95', 'G|nginx.http.request.time.median',
'G|nginx.http.request.time.max', 'G|nginx.upstream.response.time',
'G|nginx.upstream.response.time.median', 'G|nginx.upstream.response.time.max']:
assert_that(timer, has_key(key))
# values
assert_that(counter['C|nginx.http.method.get'][0][1], equal_to(3))
assert_that(counter['C|nginx.upstream.request.count'][0][1], equal_to(3))
assert_that(counter['C|nginx.cache.miss'][0][1], equal_to(1))
assert_that(counter['C|nginx.cache.hit'][0][1], equal_to(1))
def test_separate_metrics_for_4xx_5xx(self):
lines = [
'178.23.225.78 - - [18/Jun/2015:17:22:25 +0000] "GET /img/docker.png HTTP/1.1" 400 0 ' +
'"http://ec2-54-78-3-178.eu-west-1.compute.amazonaws.com:4000/" ' +
'"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) ' +
'Chrome/43.0.2357.124 Safari/537.36"',
'178.23.225.78 - - [18/Jun/2015:17:22:25 +0000] "GET /api/inventory/objects/ HTTP/1.1" 403 1093 ' +
'"http://ec2-54-78-3-178.eu-west-1.compute.amazonaws.com:4000/" ' +
'"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) ' +
'Chrome/43.0.2357.124 Safari/537.36"',
'127.0.0.1 - - [18/Jun/2015:17:22:33 +0000] "POST /1.0/589fjinijenfirjf/meta/ HTTP/1.1" ' +
'202 2 "-" "python-requests/2.2.1 CPython/2.7.6 Linux/3.13.0-48-generic"',
'52.6.158.18 - - [18/Jun/2015:17:24:40 +0000] "GET /#/objects HTTP/1.1" 502 84 ' +
'"-" "Slackbot-LinkExpanding 1.0 (+https://api.slack.com/robots)"',
'52.6.158.18 - - [18/Jun/2015:17:22:40 +0000] "GET /#/objects HTTP/1.1" 503 84 ' +
'"-" "Slackbot-LinkExpanding 1.0 (+https://api.slack.com/robots)"'
]
collector = NginxAccessLogsCollector(object=self.fake_object, tail=lines)
collector.collect()
# check
metrics = self.fake_object.statsd.flush()['metrics']
assert_that(metrics, has_item('counter'))
# counters
counter = metrics['counter']
for key in ('C|nginx.http.method.get', 'C|nginx.http.request.body_bytes_sent', 'C|nginx.http.status.3xx',
'C|nginx.http.status.2xx','C|nginx.http.method.post', 'C|nginx.http.v1_1',
'C|nginx.http.status.4xx', 'C|nginx.http.status.5xx', 'C|nginx.http.status.403',
'C|nginx.http.status.502', 'C|nginx.http.status.503'):
assert_that(counter, has_key(key))
# values
assert_that(counter['C|nginx.http.status.4xx'][0][1], equal_to(2))
assert_that(counter['C|nginx.http.status.5xx'][0][1], equal_to(2))
assert_that(counter['C|nginx.http.status.403'][0][1], equal_to(1))
assert_that(counter['C|nginx.http.status.502'][0][1], equal_to(1))
assert_that(counter['C|nginx.http.status.503'][0][1], equal_to(1))
# check zero values for new metrics
for counter_name, counter_key in collector.counters.iteritems():
if counter_key in collector.parser.keys:
assert_that(counter, has_key('C|%s' % counter_name))
if counter_name not in (
'nginx.http.method.get',
'nginx.http.method.post',
'nginx.http.status.2xx',
'nginx.http.status.3xx',
'nginx.http.status.4xx',
'nginx.http.status.5xx',
'nginx.http.status.403',
'nginx.http.status.502',
'nginx.http.status.503',
'nginx.http.v1_1',
'nginx.http.request.body_bytes_sent'
):
assert_that(counter['C|%s' % counter_name][0][1], equal_to(0))
elif counter_key is not None:
if counter_key not in collector.parser.request_variables:
assert_that(counter, not_(has_key('C|%s' % counter_name)))
def test_upstream_status(self):
log_format = '$remote_addr - $remote_user [$time_local] ' + \
'"$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" ' + \
'rt=$request_time us="$upstream_status" ut="$upstream_response_time" cs=$upstream_cache_status'
lines = [
'1.2.3.4 - - [22/Jan/2010:19:34:21 +0300] "GET /foo/ HTTP/1.1" 200 11078 ' +
'"http://www.rambler.ru/" "Mozilla/5.0 (Windows; U; Windows NT 5.1" rt=0.010 us="200" ut="2.001, 0.345" cs=MISS',
'1.2.3.4 - - [22/Jan/2010:20:34:21 +0300] "GET /foo/ HTTP/1.1" 300 1078 ' +
'"http://www.rambler.ru/" "Mozilla/5.0 (Windows; U; Windows NT 5.1" rt=0.010 us="500" ut="2.002" cs=HIT',
]
collector = NginxAccessLogsCollector(object=self.fake_object,
log_format=log_format, tail=lines)
collector.collect()
# check
metrics = self.fake_object.statsd.flush()['metrics']
assert_that(metrics, has_item('counter'))
assert_that(metrics, has_item('timer'))
# counter keys
counter = metrics['counter']
for key in ['C|nginx.http.method.get',
'C|nginx.http.v1_1',
'C|nginx.upstream.next.count',
'C|nginx.upstream.request.count',
'C|nginx.http.status.3xx',
'C|nginx.cache.miss',
'C|nginx.http.status.2xx',
'C|nginx.http.request.body_bytes_sent',
'C|nginx.cache.hit',
'C|nginx.upstream.status.2xx',
'C|nginx.upstream.status.5xx']:
assert_that(counter, has_key(key))
# timer keys
timer = metrics['timer']
for key in ['G|nginx.upstream.response.time.pctl95',
'C|nginx.upstream.response.time.count',
'C|nginx.http.request.time.count',
'G|nginx.http.request.time',
'G|nginx.http.request.time.pctl95',
'G|nginx.http.request.time.median',
'G|nginx.http.request.time.max',
'G|nginx.upstream.response.time',
'G|nginx.upstream.response.time.median',
'G|nginx.upstream.response.time.max']:
assert_that(timer, has_key(key))
# values
assert_that(counter['C|nginx.http.method.get'][0][1], equal_to(2))
assert_that(counter['C|nginx.upstream.request.count'][0][1],
equal_to(2))
assert_that(counter['C|nginx.upstream.status.2xx'][0][1],
equal_to(1))
assert_that(counter['C|nginx.upstream.status.5xx'][0][1],
equal_to(1))
assert_that(counter['C|nginx.upstream.next.count'][0][1], equal_to(1))
assert_that(timer['G|nginx.upstream.response.time.max'][0][1],
equal_to(2.001 + 0.345))
# check zero values
for counter_name, counter_key in collector.counters.iteritems():
if counter_key in collector.parser.keys:
assert_that(counter, has_key('C|%s' % counter_name))
if counter_name not in (
'nginx.http.status.2xx',
'nginx.http.status.3xx',
'nginx.http.method.get',
'nginx.upstream.request.count',
'nginx.upstream.next.count',
'nginx.upstream.response.time.max',
'nginx.http.v1_1',
'nginx.cache.miss',
'nginx.cache.hit',
'nginx.http.request.body_bytes_sent',
'nginx.upstream.status.2xx',
'nginx.upstream.status.5xx'
):
assert_that(counter['C|%s' % counter_name][0][1],
equal_to(0))
elif counter_key is not None:
if counter_key not in collector.parser.request_variables:
assert_that(counter, not_(has_key('C|%s' % counter_name)))
def test_upstream_status_multi_value(self):
log_format = '$remote_addr - $remote_user [$time_local] ' + \
'"$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" ' + \
'rt=$request_time us="$upstream_status" ut="$upstream_response_time" cs=$upstream_cache_status'
lines = [
'1.2.3.4 - - [22/Jan/2010:19:34:21 +0300] "GET /foo/ HTTP/1.1" 200 11078 ' +
'"http://www.rambler.ru/" "Mozilla/5.0 (Windows; U; Windows NT 5.1" rt=0.010 us="200" ut="2.001, 0.345" cs=MISS',
'1.2.3.4 - - [22/Jan/2010:20:34:21 +0300] "GET /foo/ HTTP/1.1" 300 1078 ' +
'"http://www.rambler.ru/" "Mozilla/5.0 (Windows; U; Windows NT 5.1" rt=0.010 us="502, 504" ut="2.002" cs=HIT',
]
collector = NginxAccessLogsCollector(object=self.fake_object,
log_format=log_format, tail=lines)
collector.collect()
# check
metrics = self.fake_object.statsd.flush()['metrics']
assert_that(metrics, has_item('counter'))
assert_that(metrics, has_item('timer'))
# counter keys
counter = metrics['counter']
for key in ['C|nginx.http.method.get',
'C|nginx.http.v1_1',
'C|nginx.upstream.next.count',
'C|nginx.upstream.request.count',
'C|nginx.http.status.3xx',
'C|nginx.cache.miss',
'C|nginx.http.status.2xx',
'C|nginx.http.request.body_bytes_sent',
'C|nginx.cache.hit',
'C|nginx.upstream.status.2xx',
'C|nginx.upstream.status.5xx']:
assert_that(counter, has_key(key))
# timer keys
timer = metrics['timer']
for key in ['G|nginx.upstream.response.time.pctl95',
'C|nginx.upstream.response.time.count',
'C|nginx.http.request.time.count',
'G|nginx.http.request.time',
'G|nginx.http.request.time.pctl95',
'G|nginx.http.request.time.median',
'G|nginx.http.request.time.max',
'G|nginx.upstream.response.time',
'G|nginx.upstream.response.time.median',
'G|nginx.upstream.response.time.max']:
assert_that(timer, has_key(key))
# values
assert_that(counter['C|nginx.http.method.get'][0][1], equal_to(2))
assert_that(counter['C|nginx.upstream.request.count'][0][1],
equal_to(2))
assert_that(counter['C|nginx.upstream.status.2xx'][0][1],
equal_to(1))
assert_that(counter['C|nginx.upstream.status.5xx'][0][1],
equal_to(2))
assert_that(counter['C|nginx.upstream.next.count'][0][1], equal_to(1))
assert_that(timer['G|nginx.upstream.response.time.max'][0][1],
equal_to(2.001 + 0.345))
# check zero values
for counter_name, counter_key in collector.counters.iteritems():
if counter_key in collector.parser.keys:
assert_that(counter, has_key('C|%s' % counter_name))
if counter_name not in (
'nginx.http.status.2xx',
'nginx.http.status.3xx',
'nginx.http.method.get',
'nginx.upstream.request.count',
'nginx.upstream.next.count',
'nginx.upstream.response.time.max',
'nginx.http.v1_1',
'nginx.cache.miss',
'nginx.cache.hit',
'nginx.http.request.body_bytes_sent',
'nginx.upstream.status.2xx',
'nginx.upstream.status.5xx'
):
assert_that(counter['C|%s' % counter_name][0][1],
equal_to(0))
elif counter_key is not None:
if counter_key not in collector.parser.request_variables:
assert_that(counter, not_(has_key('C|%s' % counter_name)))
# Note: This test case is same as the above, except the log_format & log generated are in multi line
def test_collector_multi_line_log_format(self):
log_format = '''$remote_addr - $remote_user [$time_local]
"$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"
rt=$request_time us="$upstream_status" ut="$upstream_response_time" cs=$upstream_cache_status'''
lines = [
'1.2.3.4 - - [22/Jan/2010:19:34:21 +0300]',
' "GET /foo/ HTTP/1.1" 200 11078 "http://www.rambler.ru/" "Mozilla/5.0 (Windows; U; Windows NT 5.1"',
' rt=0.010 us="200" ut="2.001, 0.345" cs=MISS',
'1.2.3.4 - - [22/Jan/2010:20:34:21 +0300]',
' "GET /foo/ HTTP/1.1" 300 1078 "http://www.rambler.ru/" "Mozilla/5.0 (Windows; U; Windows NT 5.1"',
' rt=0.010 us="502, 504" ut="2.002" cs=HIT'
]
collector = NginxAccessLogsCollector(object=self.fake_object,
log_format=log_format, tail=lines)
collector.collect()
# check
metrics = self.fake_object.statsd.flush()['metrics']
assert_that(metrics, has_item('counter'))
assert_that(metrics, has_item('timer'))
# counter keys
counter = metrics['counter']
for key in ['C|nginx.http.method.get',
'C|nginx.http.v1_1',
'C|nginx.upstream.next.count',
'C|nginx.upstream.request.count',
'C|nginx.http.status.3xx',
'C|nginx.cache.miss',
'C|nginx.http.status.2xx',
'C|nginx.http.request.body_bytes_sent',
'C|nginx.cache.hit',
'C|nginx.upstream.status.2xx',
'C|nginx.upstream.status.5xx']:
assert_that(counter, has_key(key))
# timer keys
timer = metrics['timer']
for key in ['G|nginx.upstream.response.time.pctl95',
'C|nginx.upstream.response.time.count',
'C|nginx.http.request.time.count',
'G|nginx.http.request.time',
'G|nginx.http.request.time.pctl95',
'G|nginx.http.request.time.median',
'G|nginx.http.request.time.max',
'G|nginx.upstream.response.time',
'G|nginx.upstream.response.time.median',
'G|nginx.upstream.response.time.max']:
assert_that(timer, has_key(key))
# values
assert_that(counter['C|nginx.http.method.get'][0][1], equal_to(2))
assert_that(counter['C|nginx.upstream.request.count'][0][1],
equal_to(2))
assert_that(counter['C|nginx.upstream.status.2xx'][0][1],
equal_to(1))
assert_that(counter['C|nginx.upstream.status.5xx'][0][1],
equal_to(2))
assert_that(counter['C|nginx.upstream.next.count'][0][1], equal_to(1))
assert_that(timer['G|nginx.upstream.response.time.max'][0][1],
equal_to(2.001 + 0.345))
# check zero values
for counter_name, counter_key in collector.counters.iteritems():
if counter_key in collector.parser.keys:
assert_that(counter, has_key('C|%s' % counter_name))
if counter_name not in (
'nginx.http.status.2xx',
'nginx.http.status.3xx',
'nginx.http.method.get',
'nginx.upstream.request.count',
'nginx.upstream.next.count',
'nginx.upstream.response.time.max',
'nginx.http.v1_1',
'nginx.cache.miss',
'nginx.cache.hit',
'nginx.http.request.body_bytes_sent',
'nginx.upstream.status.2xx',
'nginx.upstream.status.5xx'
):
assert_that(counter['C|%s' % counter_name][0][1],
equal_to(0))
elif counter_key is not None:
if counter_key not in collector.parser.request_variables:
assert_that(counter, not_(has_key('C|%s' % counter_name)))
def test_empty_time_variable(self):
log_format = (
'{'
'"remote_addr":"$remote_addr",'
'"remote_user":"$remote_user",'
'"time_local":"$time_local",'
'"request_method":"$request_method",'
'"request_uri":"$request_uri",'
'"status":"$status",'
'"bytes_sent":$bytes_sent,'
'"http_referer":"$http_referer",'
'"http_user_agent":"$http_user_agent",'
'"http_cookie":"$http_cookie",'
'"sent_http_cookie":"$sent_http_cookie",'
'"http_x-forwarded-for":"$http_x_forwarded_for",'
'"geoip_city":"",'
'"geoip_region":"",'
'"geoip_city_country_code":"",'
'"http_x_reqid":"$http_x_reqid",'
'"request_id":"$request_id",'
'"http_cache_control":"$http_cache_control",'
'"sent_http_cache_control":"$sent_http_cache_control",'
'"host":"$host",'
'"sent_http_x_cache_info":"$sent_http_x_cache_info",'
'"http_accept_language":"$http_accept_language",'
'"sent_http_lf_error":"$sent_http_lf_error",'
'"http_device_id":"$http_device_id",'
'"http_device_type":"$http_device_type",'
'"upstream_connect_time":"$upstream_connect_time",'
'"upstream_header_time":"$upstream_header_time",'
'"upstream_response_time":"$upstream_response_time",'
'"request_time":"$request_time",'
'"server_name":"$server_name",'
'"http_vsapptype":"$http_vsapptype",'
'"http_vsappversion":"$http_vsappversion",'
'"http_headers":"$http_headers",'
'"sent_http_headers":"$sent_http_headers",'
'"upstream_cache_status":"$upstream_cache_status",'
'"request_body_masked":"$request_body_masked", '
'"http_content_type":"$http_content_type"'
'}'
)
lines = [
(
'{'
'"remote_addr":"11.111.11.111",'
'"remote_user":"",'
'"time_local":"21/Sep/2018:20:41:39 +0000",'
'"request_method":"GET",'
'"request_uri":"/some/request/uri/",'
'"status":"200",'
'"bytes_sent":410,'
'"http_referer":"https://example.com",'
'"http_user_agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:62.0) Gecko/20100101 Firefox/62.0",'
'"http_cookie":"",'
'"sent_http_cookie":"",'
'"http_x-forwarded-for":"",'
'"geoip_city":"",'
'"geoip_region":"",'
'"geoip_city_country_code":"",'
'"http_x_reqid":"",'
'"request_id":"abcdef0123456789",'
'"http_cache_control":"",'
'"sent_http_cache_control":"no-cache",'
'"host":"example.com",'
'"sent_http_x_cache_info":"",'
'"http_accept_language":"en-US,en;q=0.5",'
'"sent_http_lf_error":"",'
'"http_device_id":"",'
'"http_device_type":"",'
'"upstream_connect_time":"",'
'"upstream_header_time":"",'
'"upstream_response_time":"",'
'"request_time":"0.000",'
'"server_name":"_",'
'"http_vsapptype":"",'
'"http_vsappversion":"",'
'"http_headers":"",'
'"sent_http_headers":"",'
'"upstream_cache_status":"",'
'"request_body_masked":"", '
'"http_content_type":""'
'}'
)
]
collector = NginxAccessLogsCollector(object=self.fake_object, log_format=log_format, tail=lines)
collector.collect()
# check
metrics = self.fake_object.statsd.flush()['metrics']
assert_that(metrics, has_item('counter'))
assert_that(metrics, has_item('timer'))
counter = metrics['counter']
timer = metrics['timer']
assert_that(counter, has_items(
'C|nginx.cache.bypass',
'C|nginx.cache.expired',
'C|nginx.cache.hit',
'C|nginx.cache.miss',
'C|nginx.cache.revalidated',
'C|nginx.cache.stale',
'C|nginx.cache.updating',
'C|nginx.http.method.delete',
'C|nginx.http.method.get',
'C|nginx.http.method.head',
'C|nginx.http.method.options',
'C|nginx.http.method.other',
'C|nginx.http.method.post',
'C|nginx.http.method.put',
'C|nginx.http.request.bytes_sent',
'C|nginx.http.status.1xx',
'C|nginx.http.status.2xx',
'C|nginx.http.status.3xx',
'C|nginx.http.status.403',
'C|nginx.http.status.404',
'C|nginx.http.status.4xx',
'C|nginx.http.status.500',
'C|nginx.http.status.502',
'C|nginx.http.status.503',
'C|nginx.http.status.504',
'C|nginx.http.status.5xx',
'C|nginx.http.status.discarded',
'C|nginx.upstream.next.count',
'C|nginx.upstream.request.count'
))
assert_that(timer, has_items(
'G|nginx.http.request.time',
'C|nginx.http.request.time.count',
'G|nginx.http.request.time.max',
'G|nginx.http.request.time.median',
'G|nginx.http.request.time.pctl95'
))
# values
assert_that(counter['C|nginx.http.method.get'][0][1], equal_to(1))
assert_that(counter['C|nginx.http.status.2xx'][0][1], equal_to(1))
assert_that(counter['C|nginx.http.request.bytes_sent'][0][1], equal_to(410))
assert_that(timer['C|nginx.http.request.time.count'][0][1], equal_to(1))
# check zero values
for counter_name, counter_key in collector.counters.iteritems():
if counter_key in collector.parser.keys:
assert_that(counter, has_key('C|%s' % counter_name))
if counter_name not in (
'nginx.http.method.get',
'nginx.http.status.2xx',
'nginx.http.request.bytes_sent'
):
assert_that(counter['C|%s' % counter_name][0][1], equal_to(0))
elif counter_key is not None:
if counter_key not in collector.parser.request_variables:
assert_that(counter, not_(has_key('C|%s' % counter_name)))