Class: LogStash::Outputs::Sns

Inherits:
Base show all
Includes:
PluginMixins::AwsConfig
Defined in:
lib/logstash/outputs/sns.rb

Overview

SNS output.

Send events to Amazon’s Simple Notification Service, a hosted pub/sub framework. It supports subscribers of type email, HTTP/S, SMS, and SQS.

For further documentation about the service see:

http://docs.amazonwebservices.com/sns/latest/api/

This plugin looks for the following fields on events it receives:

* `sns` - If no ARN is found in the configuration file, this will be used as
the ARN to publish.
* `sns_subject` - The subject line that should be used.
Optional. The "%{host}" will be used if not present and truncated at
`MAX_SUBJECT_SIZE_IN_CHARACTERS`.
* `sns_message` - The message that should be
sent. Optional. The event serialzed as JSON will be used if not present and
with the @message truncated so that the length of the JSON fits in
`MAX_MESSAGE_SIZE_IN_BYTES`.

Constant Summary collapse

MAX_SUBJECT_SIZE_IN_CHARACTERS =
100
MAX_MESSAGE_SIZE_IN_BYTES =
32768

Constants included from PluginMixins::AwsConfig

PluginMixins::AwsConfig::US_EAST_1

Constants included from Config::Mixin

Config::Mixin::CONFIGSORT

Instance Attribute Summary

Attributes included from Config::Mixin

#config, #original_params

Attributes inherited from Plugin

#logger, #params

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PluginMixins::AwsConfig

#aws_options_hash, included, #setup_aws_config

Methods inherited from Base

#handle, #handle_worker, #initialize, #worker_setup, #workers_not_supported

Methods included from Config::Mixin

#config_init, included

Methods inherited from Plugin

#eql?, #finished, #finished?, #hash, #initialize, #inspect, lookup, #reload, #running?, #shutdown, #teardown, #terminating?, #to_s

Constructor Details

This class inherits a constructor from LogStash::Outputs::Base

Class Method Details

.format_message(event) ⇒ Object



114
115
116
117
118
119
120
121
122
123
# File 'lib/logstash/outputs/sns.rb', line 114

def self.format_message(event)
  message =  "Date: #{event["@timestamp"]}\n"
  message << "Source: #{event["source"]}\n"
  message << "Tags: #{event["tags"].join(', ')}\n"
  message << "Fields: #{event.to_hash.inspect}\n"
  message << "Message: #{event["message"]}"

  # TODO: Utilize `byteslice` in JRuby 1.7: http://jira.codehaus.org/browse/JRUBY-5547
  message.slice(0, MAX_MESSAGE_SIZE_IN_BYTES)
end

.json_message(event) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/logstash/outputs/sns.rb', line 101

def self.json_message(event)
  json      = event.to_json
  json_size = json.bytesize

  # Truncate only the message if the JSON structure is too large.
  if json_size > MAX_MESSAGE_SIZE_IN_BYTES
    # TODO: Utilize `byteslice` in JRuby 1.7: http://jira.codehaus.org/browse/JRUBY-5547
    event["message"] = event["message"].slice(0, (event["message"].bytesize - (json_size - MAX_MESSAGE_SIZE_IN_BYTES)))
  end

  event.to_json
end

Instance Method Details

#aws_service_endpoint(region) ⇒ Object



51
52
53
54
55
# File 'lib/logstash/outputs/sns.rb', line 51

def aws_service_endpoint(region)
  return {
      :sns_endpoint => "sns.#{region}.amazonaws.com"
  }
end

#receive(event) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/logstash/outputs/sns.rb', line 71

def receive(event)
  return unless output?(event)

  arn     = Array(event["sns"]).first || @arn

  raise "An SNS ARN required." unless arn

  message = Array(event["sns_message"]).first
  subject = Array(event["sns_subject"]).first || event.source

  # Ensure message doesn't exceed the maximum size.
  if message
    # TODO: Utilize `byteslice` in JRuby 1.7: http://jira.codehaus.org/browse/JRUBY-5547
    message = message.slice(0, MAX_MESSAGE_SIZE_IN_BYTES)
  else
    if @format == "plain"
      message = self.class.format_message(event)
    else
      message = self.class.json_message(event)
    end
  end

  # Log event.
  @logger.debug("Sending event to SNS topic [#{arn}] with subject [#{subject}] and message:")
  message.split("\n").each { |line| @logger.debug(line) }

  # Publish the message.
  @sns.topics[arn].publish(message, :subject => subject.slice(0, MAX_SUBJECT_SIZE_IN_CHARACTERS))
end

#registerObject



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/logstash/outputs/sns.rb', line 58

def register
  require "aws-sdk"

  @sns = AWS::SNS.new(aws_options_hash)

  # Try to publish a "Logstash booted" message to the ARN provided to
  # cause an error ASAP if the credentials are bad.
  if @publish_boot_message_arn
    @sns.topics[@publish_boot_message_arn].publish("Logstash successfully booted", :subject => "Logstash booted")
  end
end