lib/fluent/plugin/ec2_metadata.rb in fluent-plugin-ec2-metadata-0.0.14 vs lib/fluent/plugin/ec2_metadata.rb in fluent-plugin-ec2-metadata-0.0.15

- old
+ new

@@ -20,27 +20,29 @@ } @placeholder_expander = PlaceholderExpander.new(log) # get metadata first and then setup a refresh thread - @ec2_metadata = Hash.new - set_metadata - set_tag + @ec2_metadata = get_metadata_and_tags @refresh_thread = Thread.new { while true sleep @metadata_refresh_seconds - set_metadata - set_tag + @ec2_metadata = get_metadata_and_tags end } end private - def set_metadata() - ec2_metadata = {} + def get_metadata_and_tags + metadata = {} + set_metadata(metadata) + set_tag(metadata) + metadata + end + def set_metadata(ec2_metadata) instance_identity = Oj.load(get_dynamic_data("instance-identity/document")) ec2_metadata['account_id'] = instance_identity["accountId"] ec2_metadata['image_id'] = instance_identity["imageId"] ec2_metadata['instance_id'] = get_metadata('instance-id') @@ -59,11 +61,11 @@ ec2_metadata['subnet_id'] = get_metadata("network/interfaces/macs/#{ec2_metadata['mac']}/subnet-id") rescue ec2_metadata['subnet_id'] = nil log.info "ec2-metadata: 'subnet_id' is undefined because #{ec2_metadata['instance_id']} is not in VPC}" end - @ec2_metadata.merge!(ec2_metadata) + ec2_metadata end def get_dynamic_data(f) res = Net::HTTP.get_response("169.254.169.254", "/latest/dynamic/#{f}") raise Fluent::ConfigError, "ec2-dynamic-data: failed to get #{f}" unless res.is_a?(Net::HTTPSuccess) @@ -74,30 +76,30 @@ res = Net::HTTP.get_response("169.254.169.254", "/latest/meta-data/#{f}") raise Fluent::ConfigError, "ec2-metadata: failed to get #{f}" unless res.is_a?(Net::HTTPSuccess) res.body end - def set_tag() + def set_tag(ec2_metadata) if @map.values.any? { |v| v.match(/^\${tagset_/) } || @output_tag =~ /\${tagset_/ if @aws_key_id and @aws_sec_key ec2 = Aws::EC2::Client.new( - region: @ec2_metadata['region'], + region: ec2_metadata['region'], access_key_id: @aws_key_id, secret_access_key: @aws_sec_key, ) else ec2 = Aws::EC2::Client.new( - region: @ec2_metadata['region'], + region: ec2_metadata['region'], ) end - response = ec2.describe_instances({ :instance_ids => [@ec2_metadata['instance_id']] }) + response = ec2.describe_instances({ :instance_ids => [ec2_metadata['instance_id']] }) instance = response.reservations[0].instances[0] raise Fluent::ConfigError, "ec2-metadata: failed to get instance data #{response.pretty_inspect}" if instance.nil? instance.tags.each { |tag| - @ec2_metadata["tagset_#{tag.key.downcase}"] = tag.value + ec2_metadata["tagset_#{tag.key.downcase}"] = tag.value } end end def modify_record(record, tag, tag_parts)