Class: Fluent::SNSOutput

Inherits:
Output
  • Object
show all
Includes:
SetTagKeyMixin, SetTimeKeyMixin
Defined in:
lib/fluent/plugin/out_sns.rb

Instance Method Summary collapse

Instance Method Details

#configure(conf) ⇒ Object



32
33
34
# File 'lib/fluent/plugin/out_sns.rb', line 32

def configure(conf)
  super
end

#emit(tag, es, chain) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/fluent/plugin/out_sns.rb', line 71

def emit(tag, es, chain)
  chain.next
  es.each {|time,record|
    record['time'] = Time.at(time).localtime
    body = get_body(record).to_s.force_encoding('UTF-8')
    subject = get_subject(record).to_s.force_encoding('UTF-8').gsub(/(\r\n|\r|\n)/, '')
    message_attributes = get_message_attributes(record)

    @topic.publish({
      message: body,
      subject: subject,
      message_attributes: message_attributes,
    })
  }
end

#get_body(record) ⇒ Object



94
95
96
97
98
99
# File 'lib/fluent/plugin/out_sns.rb', line 94

def get_body(record)
  unless @body_template.nil?
    return @body_template.result(binding)
  end
  record[@sns_body_key] || @sns_body || record.to_json
end

#get_message_attributes(record) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/fluent/plugin/out_sns.rb', line 101

def get_message_attributes(record)
  message_attributes = {}

  if @sns_message_attributes_keys
    @sns_message_attributes_keys.each_pair do |attribute, key|
      value = record[key]
      if value
        message_attributes[attribute] = {
          data_type: "String",
          string_value: value,
        }
      end
    end
  elsif @sns_message_attributes
    @sns_message_attributes.each_pair do |attribute, value|
      message_attributes[attribute] = {
        data_type: "String",
        string_value: value,
      }
    end
  end
  return message_attributes
end

#get_subject(record) ⇒ Object



87
88
89
90
91
92
# File 'lib/fluent/plugin/out_sns.rb', line 87

def get_subject(record)
  unless @subject_template.nil?
    return @subject_template.result(binding)
  end
  subject = record[@sns_subject_key] || @sns_subject || 'Fluentd-Notification'
end

#shutdownObject



67
68
69
# File 'lib/fluent/plugin/out_sns.rb', line 67

def shutdown
  super
end

#startObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/fluent/plugin/out_sns.rb', line 36

def start
  super
  options = {}
  options[:region] = @sns_region
  options[:http_proxy] = @proxy
  if @aws_key_id && @aws_sec_key
    options[:credentials] = Aws::Credentials.new(@aws_key_id, @aws_sec_key)
  end
  Aws.config.update(options)

  @sns = Aws::SNS::Resource.new
  @topic = @sns.topics.find{|topic| @sns_topic_name == topic.arn.split(":")[-1]}
  if @topic.nil?
    raise ConfigError, "No topic found for topic name #{@sns_topic_name}."
  end

  @subject_template = nil
  unless @sns_subject_template.nil?
    template_file = open(@sns_subject_template)
    @subject_template = ERB.new(template_file.read)
    template_file.close
  end

  @body_template = nil
  unless @sns_body_template.nil?
    template_file = open(@sns_body_template)
    @body_template = ERB.new(template_file.read)
    template_file.close
  end
end