From 0d65ef2b0d4fb1eabf4ee523644771199d76187b Mon Sep 17 00:00:00 2001 From: Joshua Zenn Date: Wed, 6 Apr 2022 10:45:20 -0400 Subject: [PATCH 1/3] #47 Elastic APM exporter --- CHANGELOG.md | 3 +- .../metrics/ElasticAPMMetricsExporter.py | 34 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 watergrid/metrics/ElasticAPMMetricsExporter.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 1369e20..d065609 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,9 +3,10 @@ ## [Unreleased] ### Added +- Built-in Elastic APM metrics exporter. ### Changed -- Bumped redis dependency to 4.2.2. +- Bumped `redis` dependency to 4.2.2. ### Deprecated diff --git a/watergrid/metrics/ElasticAPMMetricsExporter.py b/watergrid/metrics/ElasticAPMMetricsExporter.py new file mode 100644 index 0000000..7b7e0ac --- /dev/null +++ b/watergrid/metrics/ElasticAPMMetricsExporter.py @@ -0,0 +1,34 @@ +from watergrid.metrics.MetricsExporter import MetricsExporter + + +class ElasticAPMMetricsExporter(MetricsExporter): + def __init__(self, app_name: str, apm_server_url: str, apm_secret_token: str): + super().__init__() + self.__client = Client(service_name=app_name, server_url=apm_server_url, + secret_token=apm_secret_token) + instrument() + self.__pipeline_failed = False + self.__pipeline_name = None + + def start_pipeline(self, pipeline_name: str): + self.__client.begin_transaction(transaction_type='script') + self.__pipeline_name = pipeline_name + + def end_pipeline(self): + if self.__pipeline_failed: + elasticapm.set_transaction_outcome(OUTCOME.FAILURE) + self.__client.end_transaction(self.__pipeline_name, 'failure') + self.__pipeline_failed = False + else: + elasticapm.set_transaction_outcome(OUTCOME.SUCCESS) + self.__client.end_transaction(self.__pipeline_name, 'success') + + def start_step(self, step_name: str): + pass # This is handled by the instrumentation functions of Elastic APM. + + def end_step(self): + pass # This is handled by the instrumentation functions of Elastic APM. + + def capture_exception(self, e: Exception): + self.__client.capture_exception(exc_info=(type(e), e, e.__traceback__), handled=True) + self.__pipeline_failed = True From de865fbf598b0ca99dacfffdf49a0baa216a8c1e Mon Sep 17 00:00:00 2001 From: Joshua Zenn Date: Wed, 6 Apr 2022 10:58:19 -0400 Subject: [PATCH 2/3] #47 Linter fixes --- watergrid/metrics/ElasticAPMMetricsExporter.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/watergrid/metrics/ElasticAPMMetricsExporter.py b/watergrid/metrics/ElasticAPMMetricsExporter.py index 7b7e0ac..b8bdd54 100644 --- a/watergrid/metrics/ElasticAPMMetricsExporter.py +++ b/watergrid/metrics/ElasticAPMMetricsExporter.py @@ -4,24 +4,27 @@ class ElasticAPMMetricsExporter(MetricsExporter): def __init__(self, app_name: str, apm_server_url: str, apm_secret_token: str): super().__init__() - self.__client = Client(service_name=app_name, server_url=apm_server_url, - secret_token=apm_secret_token) + self.__client = Client( + service_name=app_name, + server_url=apm_server_url, + secret_token=apm_secret_token + ) instrument() self.__pipeline_failed = False self.__pipeline_name = None def start_pipeline(self, pipeline_name: str): - self.__client.begin_transaction(transaction_type='script') + self.__client.begin_transaction(transaction_type="script") self.__pipeline_name = pipeline_name def end_pipeline(self): if self.__pipeline_failed: elasticapm.set_transaction_outcome(OUTCOME.FAILURE) - self.__client.end_transaction(self.__pipeline_name, 'failure') + self.__client.end_transaction(self.__pipeline_name, "failure") self.__pipeline_failed = False else: elasticapm.set_transaction_outcome(OUTCOME.SUCCESS) - self.__client.end_transaction(self.__pipeline_name, 'success') + self.__client.end_transaction(self.__pipeline_name, "success") def start_step(self, step_name: str): pass # This is handled by the instrumentation functions of Elastic APM. @@ -30,5 +33,7 @@ def end_step(self): pass # This is handled by the instrumentation functions of Elastic APM. def capture_exception(self, e: Exception): - self.__client.capture_exception(exc_info=(type(e), e, e.__traceback__), handled=True) + self.__client.capture_exception( + exc_info=(type(e), e, e.__traceback__), handled=True + ) self.__pipeline_failed = True From e843444f73c963814286e2c37a4ce6ee3cdb8149 Mon Sep 17 00:00:00 2001 From: Joshua Zenn Date: Fri, 8 Apr 2022 10:29:34 -0400 Subject: [PATCH 3/3] #47 Linter fixes --- watergrid/metrics/ElasticAPMMetricsExporter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/watergrid/metrics/ElasticAPMMetricsExporter.py b/watergrid/metrics/ElasticAPMMetricsExporter.py index b8bdd54..7787fbc 100644 --- a/watergrid/metrics/ElasticAPMMetricsExporter.py +++ b/watergrid/metrics/ElasticAPMMetricsExporter.py @@ -7,7 +7,7 @@ def __init__(self, app_name: str, apm_server_url: str, apm_secret_token: str): self.__client = Client( service_name=app_name, server_url=apm_server_url, - secret_token=apm_secret_token + secret_token=apm_secret_token, ) instrument() self.__pipeline_failed = False