Class: ClearSkies::AWS::CloudWatch::RDSGauge

Inherits:
Gauge
  • Object
show all
Defined in:
lib/clear_skies/aws/cloud_watch/rds_gauge.rb

Instance Method Summary collapse

Methods inherited from Gauge

#aws_metrics, cloudwatch_client, #metrics, register

Constructor Details

#initialize(metric_name, dimension, statistics, description: nil, &block) ⇒ RDSGauge

Returns a new instance of RDSGauge.



6
7
8
# File 'lib/clear_skies/aws/cloud_watch/rds_gauge.rb', line 6

def initialize(metric_name, dimension, statistics, description: nil, &block)
  super("AWS/RDS", metric_name, dimension, statistics, description: description, &block)
end

Instance Method Details

#labels_from_metric(metric) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/clear_skies/aws/cloud_watch/rds_gauge.rb', line 18

def labels_from_metric(metric)
  labels = super(metric)

  if labels.has_key?( "db_instance_identifier") && !(Rails.cache.fetch("#{labels["db_instance_identifier"]}_skip"))
    db = Aws::RDS::DBInstance.new(labels["db_instance_identifier"])

    vpc_id = Rails.cache.fetch("#{labels["db_instance_identifier"]}_vpc_id_") do
      db.db_subnet_group&.vpc_id
    end

    labels["vpc_id"] = vpc_id if vpc_id.present?

    labels.merge!(Rails.cache.fetch("#{labels["db_instance_identifier"]}_tags_", expires_in: 1.hour, race_condition_ttl: 60.seconds) do
      tags(db)
    end)
  end
  labels
rescue Aws::RDS::Errors::DBInstanceNotFound
  Rails.cache.write("#{labels["db_instance_identifier"]}_skip", true, expires_in: 1.hour, race_condition_ttl: 60.seconds)
  return labels
end

#tags(db) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/clear_skies/aws/cloud_watch/rds_gauge.rb', line 10

def tags(db)
  labels = {}
  db.client.list_tags_for_resource(resource_name: db.db_instance_arn).tag_list.each do |tag|
    labels[tag.key.downcase] = tag.value
  end
  labels
end