Skip to content

Commit

Permalink
begins moving out subquery logging out from Subquery and into searchQ…
Browse files Browse the repository at this point in the history
…uery class (to reduce number of log posts)
  • Loading branch information
kim committed Oct 21, 2023
1 parent e7f4293 commit 373eff5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 27 deletions.
31 changes: 31 additions & 0 deletions SearchAPI/CMR/Query.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from SearchAPI.CMR.SubQuery import CMRSubQuery
from flask import request

import boto3


class CMRQuery:
def __init__(self, req_fields, params=None, max_results=None):
Expand All @@ -17,6 +19,7 @@ def __init__(self, req_fields, params=None, max_results=None):
self.req_fields = req_fields
self.page_size = cfg['cmr_page_size']
self.params = params
self.subquery_times = []

provider = request.args.get("cmr_provider")
if provider == None:
Expand Down Expand Up @@ -45,6 +48,7 @@ def __init__(self, req_fields, params=None, max_results=None):
)
for query in subquery_list_from(self.params)
]


logging.debug('New CMRQuery object ready to go')

Expand Down Expand Up @@ -94,6 +98,33 @@ def max_results_reached(self):
self.max_results is not None and
self.result_counter >= self.max_results
)



def log_subquery_time(self, subqueries):
try:
if request.asf_config['cloudwatch_metrics']:
logging.debug('Logging subquery run time to cloudwatch metrics')
cloudwatch = boto3.client('cloudwatch')
cloudwatch.put_metric_data(
MetricData = [
{
'MetricName': 'SubqueryRuntime',
'Dimensions': [
{
'Name': 'maturity',
'Value': request.asf_base_maturity
}
],
'Unit': 'Seconds',
'Values': self.queryTimes
}
],
Namespace = 'SearchAPI'
)
except Exception as e:
logging.exception(f'Failure during subquery run time logging: {e}')



def subquery_list_from(params):
Expand Down
30 changes: 3 additions & 27 deletions SearchAPI/CMR/SubQuery.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from SearchAPI.asf_env import get_config
from SearchAPI.CMR.Translate import parse_cmr_response
from SearchAPI.CMR.Exceptions import CMRError
import boto3

class CMRSubQuery:
def __init__(self, req_fields, params, extra_params):
Expand All @@ -18,6 +17,7 @@ def __init__(self, req_fields, params, extra_params):
self.req_fields = req_fields
self.hits = 0
self.results = []
self.query_times = []

self.params = self.combine_params(self.params, self.extra_params)

Expand Down Expand Up @@ -168,9 +168,9 @@ def get_page(self, session):

if query_duration > 10:
self.log_slow_cmr_response(session, response, query_duration)

self.log_subquery_time(query_duration)

self.query_times.append(query_duration)

if response.status_code != 200:
self.log_bad_cmr_response(
attempt, max_retry, response, session
Expand Down Expand Up @@ -224,27 +224,3 @@ def log_bad_cmr_response(self, attempt, max_retry, response, session):
logging.error('Headers sent to CMR:')
logging.error(session.headers)
logging.error(f'Error body: {response.text}')

def log_subquery_time(self, elapsed_time):
try:
if request.asf_config['cloudwatch_metrics']:
logging.debug('Logging subquery run time to cloudwatch metrics')
cloudwatch = boto3.client('cloudwatch')
cloudwatch.put_metric_data(
MetricData = [
{
'MetricName': 'SubqueryRuntime',
'Dimensions': [
{
'Name': 'maturity',
'Value': request.asf_base_maturity
}
],
'Unit': 'Seconds',
'Value': elapsed_time
}
],
Namespace = 'SearchAPI'
)
except Exception as e:
logging.exception(f'Failure during subquery run time logging: {e}')

0 comments on commit 373eff5

Please sign in to comment.