def cloudwatch_messages(self): """ Try to find some CloudWatch messages that might be relevant. """ client = boto3.client('logs') messages = [] try: log_group_name = guess_cloudwatch_log_group(alarm_name=self.name) # CloudWatch wants these parameters specified as seconds since # 1 Jan 1970 00:00:00, so convert to that first. timeframe = self.cloudwatch_timeframe startTime = datetime_to_cloudwatch_ts(timeframe.start) endTime = datetime_to_cloudwatch_ts(timeframe.end) # We only get the first page of results. If there's more than # one page, we have so many errors that not getting them all # in the Slack alarm is the least of our worries! for term in guess_cloudwatch_search_terms(alarm_name=self.name): resp = client.filter_log_events(logGroupName=log_group_name, startTime=startTime, endTime=endTime, filterPattern=term) messages.extend([e['message'] for e in resp['events']]) except Exception as err: print(f'Error in cloudwatch_messages: {err!r}') return messages
def cloudwatch_urls(self): """ Return some CloudWatch URLs that might be useful to check. """ try: log_group_name = guess_cloudwatch_log_group(alarm_name=self.name) timeframe = self.cloudwatch_timeframe return [ build_cloudwatch_url(search_term=search_term, log_group_name=log_group_name, start_date=timeframe.start, end_date=timeframe.end) for search_term in guess_cloudwatch_search_terms( alarm_name=self.name) ] except ValueError as err: print(f'Error in cloudwatch_urls: {err}') return []
def test_unrecognised_log_group_name_is_valueerror(bad_alarm_name): with pytest.raises(ValueError): guess_cloudwatch_log_group(bad_alarm_name)
def test_guess_cloudwatch_log_group(alarm_name, expected_log_group_name): assert guess_cloudwatch_log_group(alarm_name) == expected_log_group_name