Class: CloudwatchQuery::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudwatch_query/client.rb

Constant Summary collapse

DEFAULT_POLL_INTERVAL =
1
DEFAULT_TIMEOUT =
60

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(region: nil, profile: nil) ⇒ Client

Returns a new instance of Client.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/cloudwatch_query/client.rb', line 12

def initialize(region: nil, profile: nil)
  config = CloudwatchQuery.configuration
  client_options = {
    region: region || config.region
  }

  # AWS SDK automatically picks up credentials from:
  # 1. Environment variables (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
  # 2. Shared credentials file (~/.aws/credentials) - set by saml2aws
  # 3. EC2 instance metadata
  @aws_client = Aws::CloudWatchLogs::Client.new(client_options)
end

Instance Attribute Details

#aws_client ⇒ Object (readonly)

Returns the value of attribute aws_client.



10
11
12
# File 'lib/cloudwatch_query/client.rb', line 10

def aws_client
  @aws_client
end

Instance Method Details

#execute(query_string:, log_group_names:, start_time:, end_time:, limit:) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cloudwatch_query/client.rb', line 25

def execute(query_string:, log_group_names:, start_time:, end_time:, limit:)
  query_id = start_query(
    query_string: query_string,
    log_group_names: log_group_names,
    start_time: start_time,
    end_time: end_time,
    limit: limit
  )

  poll_results(query_id)
rescue Aws::CloudWatchLogs::Errors::ServiceError => e
  handle_aws_error(e)
end

#list_log_groups(prefix: nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cloudwatch_query/client.rb', line 39

def list_log_groups(prefix: nil)
  options = {}
  options[:log_group_name_prefix] = prefix if prefix

  groups = []
  aws_client.describe_log_groups(options).each_page do |page|
    groups.concat(page.log_groups.map(&:log_group_name))
  end
  groups
rescue Aws::CloudWatchLogs::Errors::ServiceError => e
  handle_aws_error(e)
end