Skip to content

Commit 703e191

Browse files
Merge pull request #124 from dynatrace-extensions/feature/ep_status_msg_ok_warning_error
Endpoint status message counts OK/WARNING/ERROR separately.
2 parents f7eddf1 + e7e7043 commit 703e191

File tree

3 files changed

+36
-34
lines changed

3 files changed

+36
-34
lines changed

dynatrace_extension/__about__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
# SPDX-License-Identifier: MIT
44

55

6-
__version__ = "1.7.1"
6+
__version__ = "1.7.2"

dynatrace_extension/sdk/status.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -227,33 +227,34 @@ def build_common_status(self) -> Status:
227227
with self._lock:
228228
# Summarize all statuses
229229
ok_count = 0
230-
nok_count = 0
231-
error_messages = []
232-
has_warning_status = False
230+
warning_count = 0
231+
error_count = 0
232+
messages_to_report = []
233233

234234
for ep_record in self._ep_records.values():
235235
ep_status = ep_record.ep_status
236-
if ep_status.status.is_warning():
237-
has_warning_status = True
238236

239-
if ep_status.status.is_error():
240-
nok_count += 1
241-
error_messages.append(f"{ep_status.endpoint} - {ep_status.status.value} {ep_status.message}")
237+
if ep_status.status.is_warning():
238+
warning_count += 1
239+
messages_to_report.append(f"{ep_status.endpoint} - {ep_status.status.value} {ep_status.message}")
240+
elif ep_status.status.is_error():
241+
error_count += 1
242+
messages_to_report.append(f"{ep_status.endpoint} - {ep_status.status.value} {ep_status.message}")
242243
else:
243244
ok_count += 1
244245

246+
status_msg = f"Endpoints OK: {ok_count} WARNING: {warning_count} ERROR: {error_count}"
247+
245248
# Early return if all OK
246-
if nok_count == 0:
247-
return Status(StatusValue.OK, f"Endpoints OK: {ok_count} NOK: 0")
249+
if error_count == 0 and warning_count == 0:
250+
return Status(StatusValue.OK, status_msg)
248251

249252
# Build final status if some errors present
250-
common_msg = ", ".join(error_messages)
251-
all_endpoints_faulty = ok_count == 0
253+
status_msg += f" Unhealthy endpoints: {', '.join(messages_to_report)}"
252254

253-
if all_endpoints_faulty and not has_warning_status:
255+
if ok_count == 0 and warning_count == 0:
254256
status_value = StatusValue.GENERIC_ERROR
255257
else:
256258
status_value = StatusValue.WARNING
257259

258-
message = f"Endpoints OK: {ok_count} NOK: {nok_count} NOK_reported_errors: {common_msg}"
259-
return Status(status=status_value, message=message)
260+
return Status(status=status_value, message=status_msg)

tests/sdk/test_status.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ def callback():
305305

306306
status = ext._build_current_status()
307307
self.assertEqual(status.status, StatusValue.OK)
308-
self.assertIn("OK: 3 NOK: 0", status.message)
308+
self.assertIn("OK: 3 WARNING: 0 ERROR: 0", status.message)
309309

310310
def test_endpoint_status_some_faulty_endpoints(self):
311311
ext = Extension()
@@ -334,8 +334,8 @@ def callback():
334334
status = ext._build_current_status()
335335
self.assertEqual(status.status, StatusValue.WARNING)
336336
self.assertIn(
337-
"OK: 1 NOK: 2 NOK_reported_errors: 4.5.6.7:80 - DEVICE_CONNECTION_ERROR "
338-
"Invalid authorization scheme 2, 6.7.8.9:80 - DEVICE_CONNECTION_ERROR Invalid authorization scheme 3",
337+
"Endpoints OK: 1 WARNING: 0 ERROR: 2 Unhealthy endpoints: "
338+
"4.5.6.7:80 - DEVICE_CONNECTION_ERROR Invalid authorization scheme 2, 6.7.8.9:80 - DEVICE_CONNECTION_ERROR Invalid authorization scheme 3",
339339
status.message,
340340
)
341341

@@ -368,9 +368,8 @@ def callback():
368368
status = ext._build_current_status()
369369
self.assertEqual(status.status, StatusValue.GENERIC_ERROR)
370370
self.assertIn(
371-
"OK: 0 NOK: 3 NOK_reported_errors: 1.2.3.4:80 - AUTHENTICATION_ERROR "
372-
"Invalid authorization scheme 4, 4.5.6.7:80 - DEVICE_CONNECTION_ERROR "
373-
"Invalid authorization scheme 5, 6.7.8.9:80 - DEVICE_CONNECTION_ERROR Invalid authorization scheme 6",
371+
"Endpoints OK: 0 WARNING: 0 ERROR: 3 Unhealthy endpoints: 1.2.3.4:80 - AUTHENTICATION_ERROR Invalid authorization scheme 4, "
372+
"4.5.6.7:80 - DEVICE_CONNECTION_ERROR Invalid authorization scheme 5, 6.7.8.9:80 - DEVICE_CONNECTION_ERROR Invalid authorization scheme 6",
374373
status.message,
375374
)
376375

@@ -391,7 +390,7 @@ def callback():
391390

392391
status = ext._build_current_status()
393392
self.assertEqual(status.status, StatusValue.OK)
394-
self.assertNotIn("OK: 0 NOK: 0", status.message)
393+
self.assertNotIn("Endpoints", status.message)
395394

396395
def test_endpoint_status_single_warning(self):
397396
ext = Extension()
@@ -414,7 +413,8 @@ def callback():
414413
status = ext._build_current_status()
415414
self.assertEqual(status.status, StatusValue.WARNING)
416415
self.assertIn(
417-
"OK: 0 NOK: 1 NOK_reported_errors: 1.2.3.4:80 - WARNING Invalid authorization scheme A", status.message
416+
"Endpoints OK: 0 WARNING: 1 ERROR: 0 Unhealthy endpoints: 1.2.3.4:80 - WARNING Invalid authorization scheme A",
417+
status.message,
418418
)
419419

420420
def test_endpoint_status_single_error(self):
@@ -438,7 +438,7 @@ def callback():
438438
status = ext._build_current_status()
439439
self.assertEqual(status.status, StatusValue.GENERIC_ERROR)
440440
self.assertIn(
441-
"OK: 0 NOK: 1 NOK_reported_errors: 1.2.3.4:80 - AUTHENTICATION_ERROR Invalid authorization scheme",
441+
"OK: 0 WARNING: 0 ERROR: 1 Unhealthy endpoints: 1.2.3.4:80 - AUTHENTICATION_ERROR Invalid authorization scheme",
442442
status.message,
443443
)
444444

@@ -467,7 +467,7 @@ def callback_ep_status_2():
467467

468468
status = ext._build_current_status()
469469
self.assertEqual(status.status, StatusValue.OK)
470-
self.assertIn("OK: 3 NOK: 0", status.message)
470+
self.assertIn("OK: 3 WARNING: 0 ERROR: 0", status.message)
471471

472472
def test_endpoint_merge_error(self):
473473
def callback_ep_status_1():
@@ -494,7 +494,7 @@ def callback_ep_status_2():
494494
status = ext._build_current_status()
495495
self.assertEqual(status.status, StatusValue.GENERIC_ERROR)
496496
self.assertIn(
497-
"OK: 0 NOK: 2 NOK_reported_errors: EP_HINT_1 - AUTHENTICATION_ERROR EP1 MSG, EP_HINT_2 - INVALID_CONFIG_ERROR EP2 MSG",
497+
"OK: 0 WARNING: 0 ERROR: 2 Unhealthy endpoints: EP_HINT_1 - AUTHENTICATION_ERROR EP1 MSG, EP_HINT_2 - INVALID_CONFIG_ERROR EP2 MSG",
498498
status.message,
499499
)
500500

@@ -522,7 +522,7 @@ def callback_ep_status_2():
522522

523523
status = ext._build_current_status()
524524
self.assertEqual(status.status, StatusValue.WARNING)
525-
self.assertIn("OK: 1 NOK: 1 NOK_reported_errors: EP_HINT_2 - WARNING EP2 MSG", status.message)
525+
self.assertIn("OK: 1 WARNING: 1 ERROR: 0 Unhealthy endpoints: EP_HINT_2 - WARNING EP2 MSG", status.message)
526526

527527
def test_overall_status_error(self):
528528
def callback_ep_status():
@@ -554,7 +554,7 @@ def callback_status():
554554
status = ext._build_current_status()
555555
self.assertEqual(status.status, StatusValue.GENERIC_ERROR)
556556
self.assertIn(
557-
"Endpoints OK: 0 NOK: 1 NOK_reported_errors: EP_HINT - UNKNOWN_ERROR EP MSG"
557+
"Endpoints OK: 0 WARNING: 0 ERROR: 1 Unhealthy endpoints: EP_HINT - UNKNOWN_ERROR EP MSG"
558558
"\ncallback_multistatus: GENERIC_ERROR - MULTI MSG\ncallback_status: EEC_CONNECTION_ERROR - STATUS MSG",
559559
status.message,
560560
)
@@ -588,7 +588,7 @@ def callback_status():
588588

589589
status = ext._build_current_status()
590590
self.assertEqual(status.status, StatusValue.OK)
591-
self.assertIn("Endpoints OK: 1 NOK: 0\ncallback_status: OK - STATUS MSG", status.message)
591+
self.assertIn("Endpoints OK: 1 WARNING: 0 ERROR: 0\ncallback_status: OK - STATUS MSG", status.message)
592592

593593
def test_overall_status_warning_1(self):
594594
def callback_ep_status():
@@ -620,7 +620,7 @@ def callback_status():
620620
status = ext._build_current_status()
621621
self.assertEqual(status.status, StatusValue.WARNING)
622622
self.assertIn(
623-
"Endpoints OK: 0 NOK: 1 NOK_reported_errors: EP_HINT - WARNING EP MSG\ncallback_multistatus: OK - MULTI MSG",
623+
"Endpoints OK: 0 WARNING: 1 ERROR: 0 Unhealthy endpoints: EP_HINT - WARNING EP MSG\ncallback_multistatus: OK - MULTI MSG",
624624
status.message,
625625
)
626626

@@ -654,8 +654,9 @@ def callback_status():
654654
status = ext._build_current_status()
655655
self.assertEqual(status.status, StatusValue.WARNING)
656656
self.assertIn(
657-
"Endpoints OK: 0 NOK: 1 NOK_reported_errors: EP_HINT - INVALID_CONFIG_ERROR "
658-
"\ncallback_multistatus: GENERIC_ERROR - \ncallback_status: WARNING - ",
657+
"Endpoints OK: 0 WARNING: 0 ERROR: 1 Unhealthy endpoints: EP_HINT - INVALID_CONFIG_ERROR "
658+
"\ncallback_multistatus: GENERIC_ERROR - "
659+
"\ncallback_status: WARNING - ",
659660
status.message,
660661
)
661662

@@ -711,7 +712,7 @@ def scheduler_thread_impl(ext: Extension):
711712
self.assertEqual(status.status, StatusValue.GENERIC_ERROR)
712713
self.assertIn(
713714
(
714-
"Endpoints OK: 0 NOK: 2 NOK_reported_errors: "
715+
"Endpoints OK: 0 WARNING: 0 ERROR: 2 Unhealthy endpoints: "
715716
"skipped_callback - GENERIC_ERROR skipped_callback_msg, regular_callback - UNKNOWN_ERROR regular_callback_msg"
716717
),
717718
status.message,

0 commit comments

Comments
 (0)