From 3c87a4fc884cf7c8eaccce89dbe5ecd488040e96 Mon Sep 17 00:00:00 2001 From: Johan Wassberg Date: Fri, 30 Aug 2024 19:57:48 +0200 Subject: [PATCH 1/8] Microservice to count requesting SP --- src/eduid/satosa/scimapi/statsd.py | 56 ++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/eduid/satosa/scimapi/statsd.py diff --git a/src/eduid/satosa/scimapi/statsd.py b/src/eduid/satosa/scimapi/statsd.py new file mode 100644 index 000000000..cef922e9a --- /dev/null +++ b/src/eduid/satosa/scimapi/statsd.py @@ -0,0 +1,56 @@ +import logging +import re +from typing import Any, Mapping + +import satosa.internal +from satosa.micro_services.base import ResponseMicroService + +from eduid.common.config.base import StatsConfigMixin +from eduid.common.stats import init_app_stats + +logger = logging.getLogger(__name__) + + +class requesterCounter(ResponseMicroService): + """ + A class to count the requesting SP. + + Example configuration: + ```yaml + module: statsd.requesterCounter + name: requesterCounter + config: + app_name: idpproxy + stats_port: 8125 + stats_host': monitor-fre-1.eduid.se + + ``` + """ + + def __init__(self, config: Mapping[str, Any], *args: Any, **kwargs: Any): + + super().__init__(*args, **kwargs) + + statsd_config = StatsConfigMixin(**config) + self.stats = init_app_stats(statsd_config) + + def process( + self, context: satosa.context.Context, data: satosa.internal.InternalData + ) -> satosa.internal.InternalData: + try: + requester_entity_id = context.state.state_dict["SATOSA_BASE"]["requester"] + except KeyError: + logger.warn("Unable to determine the entityID for the SP requester") + return super().process(context, data) + + # Graphite is picky about the characters in it's key names + requester_entity_id = re.sub(r"[^a-zwA-ZW0-9]", "_", requester_entity_id) + # For easier readability - only allow one underscore in a row + requester_entity_id = re.sub(r"_{2,}", "_", requester_entity_id) + + graphite_key = f"requester.{requester_entity_id}" + + logger.debug(f"Counting {graphite_key}") + self.stats.count(graphite_key) + + return super().process(context, data) From de8cc52c1c37274aba3a8acb1f001f12ae8e0fe6 Mon Sep 17 00:00:00 2001 From: Johan Wassberg Date: Mon, 2 Sep 2024 08:38:21 +0200 Subject: [PATCH 2/8] Duplicated character Thanks SonarCloud --- src/eduid/satosa/scimapi/statsd.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/eduid/satosa/scimapi/statsd.py b/src/eduid/satosa/scimapi/statsd.py index cef922e9a..624a7c1c4 100644 --- a/src/eduid/satosa/scimapi/statsd.py +++ b/src/eduid/satosa/scimapi/statsd.py @@ -44,7 +44,7 @@ def process( return super().process(context, data) # Graphite is picky about the characters in it's key names - requester_entity_id = re.sub(r"[^a-zwA-ZW0-9]", "_", requester_entity_id) + requester_entity_id = re.sub(r"[^a-zA-Z0-9]", "_", requester_entity_id) # For easier readability - only allow one underscore in a row requester_entity_id = re.sub(r"_{2,}", "_", requester_entity_id) From d9c576415396288baf27096869a055c7ff9c4019 Mon Sep 17 00:00:00 2001 From: Johan Wassberg Date: Mon, 2 Sep 2024 10:08:33 +0200 Subject: [PATCH 3/8] snake case is prefered Thanks Sonar Cloud! --- src/eduid/satosa/scimapi/statsd.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/eduid/satosa/scimapi/statsd.py b/src/eduid/satosa/scimapi/statsd.py index 624a7c1c4..a1f9ec895 100644 --- a/src/eduid/satosa/scimapi/statsd.py +++ b/src/eduid/satosa/scimapi/statsd.py @@ -11,14 +11,14 @@ logger = logging.getLogger(__name__) -class requesterCounter(ResponseMicroService): +class requester_counter(ResponseMicroService): """ A class to count the requesting SP. Example configuration: ```yaml - module: statsd.requesterCounter - name: requesterCounter + module: statsd.requester_counter + name: requester_counter config: app_name: idpproxy stats_port: 8125 From 1c5d4c3b4f88c5fa5263802d5892fa0664c7d5aa Mon Sep 17 00:00:00 2001 From: Johan Wassberg Date: Mon, 2 Sep 2024 12:05:18 +0200 Subject: [PATCH 4/8] Update src/eduid/satosa/scimapi/statsd.py Co-authored-by: Ivan Kanakarakis --- src/eduid/satosa/scimapi/statsd.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/eduid/satosa/scimapi/statsd.py b/src/eduid/satosa/scimapi/statsd.py index a1f9ec895..76c648786 100644 --- a/src/eduid/satosa/scimapi/statsd.py +++ b/src/eduid/satosa/scimapi/statsd.py @@ -37,9 +37,8 @@ def __init__(self, config: Mapping[str, Any], *args: Any, **kwargs: Any): def process( self, context: satosa.context.Context, data: satosa.internal.InternalData ) -> satosa.internal.InternalData: - try: - requester_entity_id = context.state.state_dict["SATOSA_BASE"]["requester"] - except KeyError: + requester_entity_id = data.requester + if not requester_entity_id: logger.warn("Unable to determine the entityID for the SP requester") return super().process(context, data) From d2ae99e49556fb681620ffc653b9f1bc885400b4 Mon Sep 17 00:00:00 2001 From: Johan Wassberg Date: Mon, 2 Sep 2024 12:09:25 +0200 Subject: [PATCH 5/8] Tyop --- src/eduid/satosa/scimapi/statsd.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/eduid/satosa/scimapi/statsd.py b/src/eduid/satosa/scimapi/statsd.py index 76c648786..e87fa6b7e 100644 --- a/src/eduid/satosa/scimapi/statsd.py +++ b/src/eduid/satosa/scimapi/statsd.py @@ -42,7 +42,7 @@ def process( logger.warn("Unable to determine the entityID for the SP requester") return super().process(context, data) - # Graphite is picky about the characters in it's key names + # Graphite is picky about the characters in its key names requester_entity_id = re.sub(r"[^a-zA-Z0-9]", "_", requester_entity_id) # For easier readability - only allow one underscore in a row requester_entity_id = re.sub(r"_{2,}", "_", requester_entity_id) From 3f4cd633164fc4b246e6d1026e69af80ab64c292 Mon Sep 17 00:00:00 2001 From: Johan Wassberg Date: Mon, 2 Sep 2024 12:09:53 +0200 Subject: [PATCH 6/8] Warn is deprecated --- src/eduid/satosa/scimapi/statsd.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/eduid/satosa/scimapi/statsd.py b/src/eduid/satosa/scimapi/statsd.py index e87fa6b7e..5878e17bd 100644 --- a/src/eduid/satosa/scimapi/statsd.py +++ b/src/eduid/satosa/scimapi/statsd.py @@ -39,7 +39,7 @@ def process( ) -> satosa.internal.InternalData: requester_entity_id = data.requester if not requester_entity_id: - logger.warn("Unable to determine the entityID for the SP requester") + logger.warning("Unable to determine the entityID for the SP requester") return super().process(context, data) # Graphite is picky about the characters in its key names From df8483824f07843aca68aaf1f0f94db231f30a5e Mon Sep 17 00:00:00 2001 From: Johan Wassberg Date: Mon, 2 Sep 2024 12:31:02 +0200 Subject: [PATCH 7/8] Miss read SonarCloud Issue CamelCase is recommended by PEP-8 --- src/eduid/satosa/scimapi/statsd.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/eduid/satosa/scimapi/statsd.py b/src/eduid/satosa/scimapi/statsd.py index 5878e17bd..036af4aa2 100644 --- a/src/eduid/satosa/scimapi/statsd.py +++ b/src/eduid/satosa/scimapi/statsd.py @@ -11,14 +11,14 @@ logger = logging.getLogger(__name__) -class requester_counter(ResponseMicroService): +class RequesterCounter(ResponseMicroService): """ A class to count the requesting SP. Example configuration: ```yaml - module: statsd.requester_counter - name: requester_counter + module: statsd.RequesterCounter + name: RequesterCounter config: app_name: idpproxy stats_port: 8125 From 7fc000b55f7b42e5e25e081961379a7ed23c249c Mon Sep 17 00:00:00 2001 From: Johan Wassberg Date: Mon, 2 Sep 2024 12:36:25 +0200 Subject: [PATCH 8/8] Happier IDE(s) --- src/eduid/satosa/scimapi/statsd.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/eduid/satosa/scimapi/statsd.py b/src/eduid/satosa/scimapi/statsd.py index 036af4aa2..a033034d1 100644 --- a/src/eduid/satosa/scimapi/statsd.py +++ b/src/eduid/satosa/scimapi/statsd.py @@ -3,6 +3,8 @@ from typing import Any, Mapping import satosa.internal +from satosa.context import Context +from satosa.internal import InternalData from satosa.micro_services.base import ResponseMicroService from eduid.common.config.base import StatsConfigMixin @@ -34,9 +36,7 @@ def __init__(self, config: Mapping[str, Any], *args: Any, **kwargs: Any): statsd_config = StatsConfigMixin(**config) self.stats = init_app_stats(statsd_config) - def process( - self, context: satosa.context.Context, data: satosa.internal.InternalData - ) -> satosa.internal.InternalData: + def process(self, context: Context, data: InternalData) -> InternalData: requester_entity_id = data.requester if not requester_entity_id: logger.warning("Unable to determine the entityID for the SP requester")