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

- old
+ new

@@ -17,11 +17,11 @@ element.each_pair { |k, v| @map[k] = v } } - @placeholder_expander = PlaceholderExpander.new + @placeholder_expander = PlaceholderExpander.new(log) # get metadata first and then setup a refresh thread @ec2_metadata = Hash.new set_metadata set_tag @@ -51,17 +51,17 @@ ec2_metadata['mac'] = get_metadata('mac') begin ec2_metadata['vpc_id'] = get_metadata("network/interfaces/macs/#{ec2_metadata['mac']}/vpc-id") rescue ec2_metadata['vpc_id'] = nil - $log.info "ec2-metadata: 'vpc_id' is undefined #{ec2_metadata['instance_id']} is not in VPC}" + log.info "ec2-metadata: 'vpc_id' is undefined #{ec2_metadata['instance_id']} is not in VPC}" end begin 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}" + log.info "ec2-metadata: 'subnet_id' is undefined because #{ec2_metadata['instance_id']} is not in VPC}" end @ec2_metadata.merge!(ec2_metadata) end def get_dynamic_data(f) @@ -118,10 +118,14 @@ [new_tag, new_record] end class PlaceholderExpander + def initialize(log) + @log = log + end + # referenced https://github.com/fluent/fluent-plugin-rewrite-tag-filter # referenced https://github.com/sonots/fluent-plugin-record-reformer attr_reader :placeholders def prepare_placeholders(_record, tag, tag_parts, ec2_metadata) @@ -142,10 +146,10 @@ @placeholders = placeholders end def expand(str) str.gsub(/(\${[a-z_:\-]+(\[-?[0-9]+\])?}|__[A-Z_]+__)/) { - $log.warn "ec2-metadata: unknown placeholder `#{$1}` found in a tag `#{@placeholders['${tag}']}`" unless @placeholders.include?($1) + @log.warn "ec2-metadata: unknown placeholder `#{$1}` found in a tag `#{@placeholders['${tag}']}`" unless @placeholders.include?($1) @placeholders[$1] } end end end