forked from signalfx/collectd-spark
/
test_spark_plugin.py
507 lines (431 loc) · 22 KB
/
test_spark_plugin.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
#!/usr/bin/env python
from unittest import TestCase
from mock import MagicMock, Mock, patch
import sample_html
import sample_responses
import spark_plugin
from spark_plugin import MetricRecord, SparkApplicationPlugin, SparkProcessPlugin
def mock_request_response(url, path, *args, **kwargs):
if path == spark_plugin.MASTER_PATH:
return sample_responses.master_metrics
elif path == spark_plugin.WORKER_PATH:
return sample_responses.worker_metrics
elif path == spark_plugin.STANDALONE_STATE_PATH:
return sample_responses.master_standalone_json
elif path == spark_plugin.STANDALONE_APP_PATH:
return sample_html.MockHTMLResponse()
elif path == spark_plugin.MESOS_MASTER_APP_PATH:
return sample_responses.master_mesos_json
elif path == spark_plugin.APPS_ENDPOINT and "jobs" in args:
return sample_responses.running_jobs
elif path == spark_plugin.APPS_ENDPOINT and "stages" in args:
return sample_responses.active_stages
elif path == spark_plugin.APPS_ENDPOINT and "executors" in args:
return sample_responses.executors
elif path == spark_plugin.APPS_ENDPOINT and "streaming/statistics" in args:
return sample_responses.streaming_stats
def get_mock_spark_agent():
mock_spark_agent = Mock()
mock_spark_agent.request_metrics = MagicMock(side_effect=mock_request_response)
mock_spark_agent.rest_request = MagicMock(side_effect=mock_request_response)
return mock_spark_agent
class MockMetricSink(object):
def __init__(self):
self.captured_records = []
def emit(self, metric_record):
self.captured_records.append(metric_record)
class SparkProcessTest(TestCase):
def setUp(self):
self.plugin = SparkProcessPlugin()
self.plugin.spark_agent = get_mock_spark_agent()
self.mock_sink = MockMetricSink()
self.plugin.metric_sink = self.mock_sink
def test_configure_exceptions(self):
config_map = {"Dimensions": "foo=bar,hello=world"}
with self.assertRaises(ValueError):
self.plugin.configure(config_map)
config_map["MasterPort"] = "Non-integer port"
with self.assertRaises(ValueError):
self.plugin.configure(config_map)
config_map["MasterPort"] = "8080.0"
with self.assertRaises(ValueError):
self.plugin.configure(config_map)
del config_map["MasterPort"]
config_map["WorkerPorts"] = "Non-integer port"
with self.assertRaises(ValueError):
self.plugin.configure(config_map)
config_map["WorkerPorts"] = "8080.0"
with self.assertRaises(ValueError):
self.plugin.configure(config_map)
config_map["MetricsURL"] = "localhost"
with self.assertRaises(ValueError):
self.plugin.configure(config_map)
def test_configure_worker_port_none(self):
config_map = {"Dimensions": "foo=bar,hello=world", "MetricsURL": "http://host", "MasterPort": 8080}
expected_global_dim = {"foo": "bar", "hello": "world"}
expected_metric_address = "http://host"
expected_master_port = "8080"
self.plugin.configure(config_map)
self.assertDictEqual(self.plugin.global_dimensions, expected_global_dim)
self.assertEqual(self.plugin.metric_address, expected_metric_address)
self.assertEqual(self.plugin.master_port, expected_master_port)
self.assertEqual(0, len(self.plugin.worker_ports))
def test_configure_master_port_none(self):
config_map = {"Dimensions": "foo=bar,hello=world", "MetricsURL": "http://host", "WorkerPorts": [8081, 8082]}
expected_global_dim = {"foo": "bar", "hello": "world"}
expected_metric_address = "http://host"
expected_worker_port = "8081"
expected_worker_port_2 = "8082"
self.plugin.configure(config_map)
self.assertDictEqual(self.plugin.global_dimensions, expected_global_dim)
self.assertEqual(self.plugin.metric_address, expected_metric_address)
self.assertEqual(self.plugin.worker_ports[0], expected_worker_port)
self.assertEqual(self.plugin.worker_ports[1], expected_worker_port_2)
self.assertIsNone(self.plugin.master_port)
def test_configure(self):
config_map = {
"Dimensions": "foo=bar,hello=world",
"Dimension": "key=value",
"MetricsURL": "http://host",
"MasterPort": 8080,
"WorkerPorts": [8081],
"EnhancedMetrics": "True",
"IncludeMetrics": "metric1,metric2",
}
expected_global_dim = {"foo": "bar", "hello": "world", "key": "value"}
expected_metric_address = "http://host"
expected_master_port = "8080"
expected_worker_port = "8081"
self.plugin.configure(config_map)
self.assertDictEqual(self.plugin.global_dimensions, expected_global_dim)
self.assertEqual(self.plugin.metric_address, expected_metric_address)
self.assertEqual(self.plugin.worker_ports[0], expected_worker_port)
self.assertEqual(self.plugin.master_port, expected_master_port)
assert self.plugin.enhanced_flag
self.assertEqual(2, len(self.plugin.include))
def test_get_metrics(self):
resp = {}
process = ""
expected_resp = []
include = set()
include.add("HiveExternalCatalog.fileCacheHits")
self.plugin.include = include
actual_resp = self.plugin.get_metrics(resp, process)
self.assertListEqual(actual_resp, expected_resp)
resp = {
"gauges": {"fail_metric_name_1": {"value": 0}, "master.workers": {"value": 100}},
"counters": {"fail_metric_name_2": {"count": 0}, "HiveExternalCatalog.fileCacheHits": {"count": 2}},
}
self.plugin.global_dimensions = {"foo": "bar"}
process = "master"
expected_dim = {"foo": "bar", "spark_process": process}
expected_resp = [
MetricRecord("master.workers", "gauge", 100, expected_dim),
MetricRecord("HiveExternalCatalog.fileCacheHits", "counter", 2, expected_dim),
]
actual_resp = self.plugin.get_metrics(resp, process)
self.assertEqual(len(expected_resp), len(actual_resp))
self._validate_metrics(expected_resp, actual_resp)
def test_read_and_post_metrics(self):
self.plugin.metric_address = "http://host"
self.plugin.master_port = "8080"
self.plugin.worker_ports = ["8081"]
include = set()
include.add("HiveExternalCatalog.fileCacheHits")
self.plugin.include = include
self.plugin.read()
exp_mr_1 = MetricRecord("jvm.heap.committed", "gauge", 0.0257, {"spark_process": "master"})
exp_mr_2 = MetricRecord(
"jvm.heap.committed", "gauge", 0.0434, {"spark_process": "worker", "worker_port": "8081"}
)
exp_mr_3 = MetricRecord("jvm.heap.used", "gauge", 26716912, {"spark_process": "master"})
exp_mr_4 = MetricRecord("jvm.heap.used", "gauge", 45102544, {"spark_process": "worker", "worker_port": "8081"})
exp_mr_5 = MetricRecord("HiveExternalCatalog.fileCacheHits", "counter", 2, {"spark_process": "master"})
exp_mr_6 = MetricRecord(
"HiveExternalCatalog.fileCacheHits", "counter", 0, {"spark_process": "worker", "worker_port": "8081"}
)
expected_records = [exp_mr_1, exp_mr_2, exp_mr_3, exp_mr_4, exp_mr_5, exp_mr_6]
self._verify_records_captured(expected_records)
def _validate_metrics(self, expected_resp, actual_resp):
sep = sorted(expected_resp, key=lambda mr: mr.name)
sap = sorted(actual_resp, key=lambda mr: mr.name)
for i in range(len(sep)):
self._validate_single_record(sep[i], sap[i])
def _validate_single_record(self, expected_record, actual_record):
"""
Taken from nginx-plus unit testing
"""
self.assertIsNotNone(actual_record)
self.assertEqual(expected_record.name, actual_record.name)
self.assertEqual(expected_record.type, actual_record.type)
self.assertEqual(expected_record.value, actual_record.value)
self.assertDictEqual(expected_record.dimensions, actual_record.dimensions)
def _verify_records_captured(self, expected_records):
"""
Taken from nginx-plus unit testing
"""
for expected_record in expected_records:
self.assertIsNotNone(
next(
(
record
for record in self.mock_sink.captured_records
if self._compare_records(expected_record, record)
),
None,
),
"Captured records does not contain: {} \
captured records: {}".format(
expected_record.to_string(), [record.to_string() for record in self.mock_sink.captured_records]
),
)
def _compare_records(self, expected_record, actual_record):
"""
Taken from nginx-plus unit testing
"""
try:
self._validate_single_record(expected_record, actual_record)
return True
except Exception:
pass
return False
class SparkApplicationTest(TestCase):
def setUp(self):
self.plugin = SparkApplicationPlugin()
self.plugin.spark_agent = get_mock_spark_agent()
self.mock_sink = MockMetricSink()
self.plugin.metric_sink = self.mock_sink
self.apps = {"app_id": ("app_name", "user", "http://host:port")}
self.key = ("app_name", "user")
self.expected_dim = {"app_name": "app_name", "user": "user"}
self.expected_standalone_dim = {"app_name": "standalone_name", "user": "standalone_user"}
self.expected_mesos_dim = {"app_name": "mesos_name", "user": "mesos_user"}
def test_configure_exceptions(self):
config_map = {"Dimensions": "foo=bar,hello=world"}
with self.assertRaises(ValueError):
self.plugin.configure(config_map)
config_map["Cluster"] = "Standalone"
with self.assertRaises(ValueError):
self.plugin.configure(config_map)
del config_map["Cluster"]
config_map["Master"] = "http://host:port"
with self.assertRaises(ValueError):
self.plugin.configure(config_map)
config_map["Cluster"] = "unsupported_cluster_name"
config_map["Master"] = "http://host:port"
with self.assertRaises(ValueError):
self.plugin.configure(config_map)
config_map["Cluster"] = "Standalone"
config_map["Master"] = "http://host"
with self.assertRaises(ValueError):
self.plugin.configure(config_map)
config_map["Master"] = "host:8080"
with self.assertRaises(ValueError):
self.plugin.configure(config_map)
def test_configure(self):
cluster = "Standalone"
config_map = {
"Dimensions": "foo=bar,hello=world",
"Dimension": "key=value",
"Master": "http://host:8080",
"Cluster": cluster,
"EnhancedMetrics": "False",
"ExcludeMetrics": "metric1,metric2",
}
expected_global_dim = {"foo": "bar", "hello": "world", "key": "value", "cluster": cluster}
expected_master = "http://host:8080"
expected_cluster = cluster
self.plugin.configure(config_map)
self.assertDictEqual(self.plugin.global_dimensions, expected_global_dim)
self.assertEqual(self.plugin.master, expected_master)
self.assertEqual(self.plugin.cluster_mode, expected_cluster)
assert not self.plugin.enhanced_flag
self.assertEqual(2, len(self.plugin.exclude))
cluster = "Mesos"
config_map["Cluster"] = cluster
expected_global_dim["cluster"] = cluster
expected_cluster = cluster
self.plugin.global_dimensions = {}
self.plugin.configure(config_map)
self.assertDictEqual(self.plugin.global_dimensions, expected_global_dim)
self.assertEqual(self.plugin.master, expected_master)
self.assertEqual(self.plugin.cluster_mode, expected_cluster)
def test_running_apps_fail(self):
apps = self.plugin._get_running_apps()
self.assertEqual(0, len(apps))
@patch("spark_plugin.SparkApplicationPlugin._get_standalone_apps")
def test_standalone_running_apps(self, mock_get_apps):
self.plugin.cluster_mode = "Standalone"
self.plugin._get_running_apps()
mock_get_apps.assert_called()
@patch("spark_plugin.SparkApplicationPlugin._get_mesos_apps")
def test_mesos_running_apps(self, mock_get_apps):
self.plugin.cluster_mode = "Mesos"
self.plugin._get_running_apps()
mock_get_apps.assert_called()
def test_get_standalone_apps(self):
expected_apps = {"standalone_id": ("standalone_name", "standalone_user", "http://172.31.15.190:4040")}
actual_apps = self.plugin._get_standalone_apps()
self.assertDictEqual(actual_apps, expected_apps)
self.assertGreater(len(self.plugin.metrics), 0)
self.assertDictEqual(self.plugin.metrics[("standalone_name", "standalone_user")], {})
def test_get_mesos_apps(self):
expected_apps = {"mesos_id": ("mesos_name", "mesos_user", "http://10.0.2.155:4040")}
actual_apps = self.plugin._get_mesos_apps()
self.assertDictEqual(actual_apps, expected_apps)
self.assertGreater(len(self.plugin.metrics), 0)
self.assertDictEqual(self.plugin.metrics[("mesos_name", "mesos_user")], {})
def test_get_streaming_metrics(self):
self.plugin.metrics[self.key] = {}
self.plugin._get_streaming_metrics(self.apps)
exp_mr_1 = MetricRecord("spark.streaming.avg_input_rate", "gauge", 0.0, self.expected_dim)
exp_mr_2 = MetricRecord("spark.streaming.avg_scheduling_delay", "gauge", 4, self.expected_dim)
exp_mr_3 = MetricRecord("spark.streaming.avg_processing_time", "gauge", 93, self.expected_dim)
exp_mr_4 = MetricRecord("spark.streaming.avg_total_delay", "gauge", 97, self.expected_dim)
expected_records = [exp_mr_1, exp_mr_2, exp_mr_3, exp_mr_4]
actual_records = list(self.plugin.metrics[self.key].values())
self._validate_metrics(expected_records, actual_records)
def test_get_job_metrics(self):
self.plugin.metrics[self.key] = {}
self.plugin._get_job_metrics(self.apps)
exp_mr_1 = MetricRecord("spark.job.num_active_tasks", "gauge", 1, self.expected_dim)
exp_mr_2 = MetricRecord("spark.job.num_active_stages", "gauge", 2, self.expected_dim)
exp_mr_3 = MetricRecord("spark.num_running_jobs", "gauge", 1, self.expected_dim)
expected_records = [exp_mr_1, exp_mr_2, exp_mr_3]
actual_records = list(self.plugin.metrics[self.key].values())
self._validate_metrics(expected_records, actual_records)
def test_get_stage_metrics(self):
self.plugin.metrics[self.key] = {}
self.plugin._get_stage_metrics(self.apps)
exp_mr_1 = MetricRecord("spark.stage.input_bytes", "gauge", 1553, self.expected_dim)
exp_mr_2 = MetricRecord("spark.stage.input_records", "gauge", 5, self.expected_dim)
exp_mr_3 = MetricRecord("spark.num_active_stages", "gauge", 2, self.expected_dim)
expected_records = [exp_mr_1, exp_mr_2, exp_mr_3]
actual_records = list(self.plugin.metrics[self.key].values())
self._validate_metrics(expected_records, actual_records)
def test_get_executor_metrics(self):
self.plugin.metrics[self.key] = {}
self.plugin._get_executor_metrics(self.apps)
exp_mr_1 = MetricRecord("spark.driver.memory_used", "counter", 30750, self.expected_dim)
exp_mr_2 = MetricRecord("spark.driver.disk_used", "counter", 1155, self.expected_dim)
exp_mr_3 = MetricRecord("spark.executor.memory_used", "counter", 34735, self.expected_dim)
exp_mr_4 = MetricRecord("spark.executor.disk_used", "counter", 1973, self.expected_dim)
exp_mr_5 = MetricRecord("spark.executor.count", "gauge", 3, self.expected_dim)
expected_records = [exp_mr_1, exp_mr_2, exp_mr_3, exp_mr_4, exp_mr_5]
actual_records = list(self.plugin.metrics[self.key].values())
self._validate_metrics(expected_records, actual_records)
def test_mesos_read_and_post_metrics(self):
exp_mr_1 = MetricRecord("spark.streaming.avg_input_rate", "gauge", 0.0, self.expected_standalone_dim)
exp_mr_2 = MetricRecord("spark.streaming.avg_scheduling_delay", "gauge", 4, self.expected_standalone_dim)
exp_mr_3 = MetricRecord("spark.streaming.avg_processing_time", "gauge", 93, self.expected_standalone_dim)
exp_mr_4 = MetricRecord("spark.streaming.avg_total_delay", "gauge", 97, self.expected_standalone_dim)
exp_mr_5 = MetricRecord("spark.job.num_active_tasks", "gauge", 1, self.expected_standalone_dim)
exp_mr_6 = MetricRecord("spark.job.num_active_stages", "gauge", 2, self.expected_standalone_dim)
exp_mr_7 = MetricRecord("spark.num_running_jobs", "gauge", 1, self.expected_standalone_dim)
exp_mr_8 = MetricRecord("spark.stage.input_bytes", "gauge", 1553, self.expected_standalone_dim)
exp_mr_9 = MetricRecord("spark.stage.input_records", "gauge", 5, self.expected_standalone_dim)
exp_mr_10 = MetricRecord("spark.num_active_stages", "gauge", 2, self.expected_standalone_dim)
exp_mr_11 = MetricRecord("spark.driver.memory_used", "counter", 30750, self.expected_standalone_dim)
exp_mr_12 = MetricRecord("spark.driver.disk_used", "counter", 1155, self.expected_standalone_dim)
exp_mr_13 = MetricRecord("spark.executor.memory_used", "counter", 34735, self.expected_standalone_dim)
exp_mr_14 = MetricRecord("spark.executor.disk_used", "counter", 1973, self.expected_standalone_dim)
exp_mr_15 = MetricRecord("spark.executor.count", "gauge", 3, self.expected_standalone_dim)
expected_records = [
exp_mr_1,
exp_mr_2,
exp_mr_3,
exp_mr_4,
exp_mr_5,
exp_mr_6,
exp_mr_7,
exp_mr_8,
exp_mr_9,
exp_mr_10,
exp_mr_11,
exp_mr_12,
exp_mr_13,
exp_mr_14,
exp_mr_15,
]
self.plugin.cluster_mode = "Standalone"
self.plugin.read()
self._verify_records_captured(expected_records)
def test_standalone_read_and_post_metrics(self):
exp_mr_1 = MetricRecord("spark.streaming.avg_input_rate", "gauge", 0.0, self.expected_mesos_dim)
exp_mr_2 = MetricRecord("spark.streaming.avg_scheduling_delay", "gauge", 4, self.expected_mesos_dim)
exp_mr_3 = MetricRecord("spark.streaming.avg_processing_time", "gauge", 93, self.expected_mesos_dim)
exp_mr_4 = MetricRecord("spark.streaming.avg_total_delay", "gauge", 97, self.expected_mesos_dim)
exp_mr_5 = MetricRecord("spark.job.num_active_tasks", "gauge", 1, self.expected_mesos_dim)
exp_mr_6 = MetricRecord("spark.job.num_active_stages", "gauge", 2, self.expected_mesos_dim)
exp_mr_7 = MetricRecord("spark.num_running_jobs", "gauge", 1, self.expected_mesos_dim)
exp_mr_8 = MetricRecord("spark.stage.input_bytes", "gauge", 1553, self.expected_mesos_dim)
exp_mr_9 = MetricRecord("spark.stage.input_records", "gauge", 5, self.expected_mesos_dim)
exp_mr_10 = MetricRecord("spark.num_active_stages", "gauge", 2, self.expected_mesos_dim)
exp_mr_11 = MetricRecord("spark.driver.memory_used", "counter", 30750, self.expected_mesos_dim)
exp_mr_12 = MetricRecord("spark.driver.disk_used", "counter", 1155, self.expected_mesos_dim)
exp_mr_13 = MetricRecord("spark.executor.memory_used", "counter", 34735, self.expected_mesos_dim)
exp_mr_14 = MetricRecord("spark.executor.disk_used", "counter", 1973, self.expected_mesos_dim)
exp_mr_15 = MetricRecord("spark.executor.count", "gauge", 3, self.expected_mesos_dim)
expected_records = [
exp_mr_1,
exp_mr_2,
exp_mr_3,
exp_mr_4,
exp_mr_5,
exp_mr_6,
exp_mr_7,
exp_mr_8,
exp_mr_9,
exp_mr_10,
exp_mr_11,
exp_mr_12,
exp_mr_13,
exp_mr_14,
exp_mr_15,
]
self.plugin.cluster_mode = "Mesos"
self.plugin.read()
self._verify_records_captured(expected_records)
def _validate_metrics(self, expected_resp, actual_resp):
sep = sorted(expected_resp, key=lambda mr: mr.name)
sap = sorted(actual_resp, key=lambda mr: mr.name)
for i in range(len(sep)):
self._validate_single_record(sep[i], sap[i])
def _validate_single_record(self, expected_record, actual_record):
"""
Taken from nginx-plus unit testing
"""
self.assertIsNotNone(actual_record)
self.assertEqual(expected_record.name, actual_record.name)
self.assertEqual(expected_record.type, actual_record.type)
self.assertEqual(expected_record.value, actual_record.value)
self.assertDictEqual(expected_record.dimensions, actual_record.dimensions)
def _verify_records_captured(self, expected_records):
"""
Taken from nginx-plus unit testing
"""
for expected_record in expected_records:
self.assertIsNotNone(
next(
(
record
for record in self.mock_sink.captured_records
if self._compare_records(expected_record, record)
),
None,
),
"Captured records does not contain: {} \
captured records: {}".format(
expected_record.to_string(), [record.to_string() for record in self.mock_sink.captured_records]
),
)
def _compare_records(self, expected_record, actual_record):
"""
Taken from nginx-plus unit testing
"""
try:
self._validate_single_record(expected_record, actual_record)
return True
except Exception:
pass
return False