Module: RubyAemAws::CloudwatchClient

Included in:
MetricVerifier
Defined in:
lib/ruby_aem_aws/client/cloudwatch.rb

Overview

Mixin for checking that an instance has associated CloudWatch metrics.

Instance Method Summary collapse

Instance Method Details

#get_alarm(alarm_name) ⇒ Object

Returns Cloudwatch client describe_alarms response.

Parameters:

  • alarm_name

    Cloudwatch alarm name

Returns:

  • Cloudwatch client describe_alarms response



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ruby_aem_aws/client/cloudwatch.rb', line 20

def get_alarm(alarm_name)
  alarm_filter = filter_for_cloudwatch_alarm(alarm_name)

  response = cloud_watch_client.describe_alarms(alarm_filter)

  until response.next_token.nil?
    next_token = { next_token: response.next_token }
    filter.update(next_token)
    response = cloud_watch_client.describe_alarms(alarm_filter)
  end

  response
end

#get_log_event(loggroup_name, log_stream_name, log_message) ⇒ Object

Returns Cloudwatch log client filter_log_events response.

Parameters:

  • loggroup_name

    Cloudwatch loggroup name

  • log_stream_name

    Cloudwatch log stream name

  • log_message

    Log message to filter for

Returns:

  • Cloudwatch log client filter_log_events response



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ruby_aem_aws/client/cloudwatch.rb', line 38

def get_log_event(loggroup_name, log_stream_name, log_message)
  # Initialise response as an empty response having no events and no next token
  # This is needed to handle the scenario when initial filter log events returns
  # a response with nil next token, ensuring the clients of this method to
  # be able to identify any empty response events.
  response = { events: [], next_token: nil }

  filter = filter_for_cloudwatch_log_event(loggroup_name, log_stream_name, log_message)
  curr_response = cloud_watch_log_client.filter_log_events(filter)

  # Since late 2021 (circa aws-sdk-cloudwatchlog 1.45.0), the last response
  # is always empty (empty response events and nil next token).
  # Previous to late 2021, the last response used to contain the filtered events
  # with nil next token.
  until curr_response.next_token.nil?
    next_token = { next_token: curr_response.next_token }
    filter.update(next_token)
    # empty events should be ignored
    response = curr_response unless curr_response.events.empty?
    curr_response = cloud_watch_log_client.filter_log_events(filter)
  end

  response
end

#get_log_streams(loggroup_name, log_stream_name) ⇒ Object

Returns Cloudwatch log client describe_log_streams response.

Parameters:

  • loggroup_name

    Cloudwatch loggroup name

  • log_stream_name

    Cloudwatch log stream name

Returns:

  • Cloudwatch log client describe_log_streams response



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ruby_aem_aws/client/cloudwatch.rb', line 66

def get_log_streams(loggroup_name, log_stream_name)
  filter = filter_for_cloudwatch_log_stream(loggroup_name, log_stream_name)

  response = cloud_watch_log_client.describe_log_streams(filter)

  until response.next_token.nil?
    next_token = { next_token: response.next_token }
    filter.update(next_token)
    response = cloud_watch_client.describe_log_streams(filter)
  end

  response
end

#get_metrics(namespace, metric_name, dimension) ⇒ Object

Returns Cloudwatch client list_metrics response.

Parameters:

  • namespace

    Cloudwatch namespace name

  • metric_name

    Cloudwatch metric name

  • dimension

    Cloudwatch dimension filter

Returns:

  • Cloudwatch client list_metrics response



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ruby_aem_aws/client/cloudwatch.rb', line 84

def get_metrics(namespace, metric_name, dimension)
  filter = filter_for_cloudwatch_metric(namespace, metric_name)
  filter.update(dimension)

  response = cloud_watch_client.list_metrics(filter)

  until response.next_token.nil?
    next_token = { next_token: response.next_token }
    filter.update(next_token)
    response = cloud_watch_client.list_metrics(filter)
  end

  response
end