Class: LogStash::Outputs::SumoLogic::Sender

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/logstash/outputs/sumologic/sender.rb

Constant Summary

Constants included from Common

Common::CARBON2, Common::CATEGORY_HEADER, Common::CATEGORY_HEADER_DEFAULT, Common::CATEGORY_HEADER_DEFAULT_STATS, Common::CLIENT_HEADER, Common::CLIENT_HEADER_VALUE, Common::CONTENT_ENCODING, Common::CONTENT_TYPE, Common::CONTENT_TYPE_CARBON2, Common::CONTENT_TYPE_GRAPHITE, Common::CONTENT_TYPE_LOG, Common::DEFAULT_LOG_FORMAT, Common::DEFLATE, Common::GRAPHITE, Common::GZIP, Common::HOST_HEADER, Common::LOG_TO_CONSOLE, Common::METRICS_NAME_PLACEHOLDER, Common::NAME_HEADER, Common::NAME_HEADER_DEFAULT, Common::STATS_TAG, Common::STOP_TAG

Instance Method Summary collapse

Methods included from Common

#blank?, #log_dbg, #log_err, #log_info, #log_warn, #set_logger

Constructor Details

#initialize(client, queue, stats, config) ⇒ Sender

Returns a new instance of Sender.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/logstash/outputs/sumologic/sender.rb', line 18

def initialize(client, queue, stats, config)
  @client = client
  @queue = queue
  @stats = stats
  @stopping = Concurrent::AtomicBoolean.new(false)
  @url = config["url"]
  @sender_max = (config["sender_max"] ||= 1) < 1 ? 1 : config["sender_max"]
  @sleep_before_requeue = config["sleep_before_requeue"] ||= 30
  @stats_enabled = config["stats_enabled"] ||= false
  @iteration_sleep = 0.3

  @tokens = SizedQueue.new(@sender_max)
  @sender_max.times { |t| @tokens << t }

  # Make resend_queue twice as big as sender_max,
  # because if one batch is processed, the next one is already waiting in the thread
  @resend_queue = SizedQueue.new(2*@sender_max)
  @compressor = LogStash::Outputs::SumoLogic::Compressor.new(config)

end

Instance Method Details

#connectObject

def stop



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/logstash/outputs/sumologic/sender.rb', line 82

def connect()
  uri = URI.parse(@url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = @url.downcase().start_with?("https")
  request = Net::HTTP::Get.new(uri.request_uri)
  begin
    res = http.request(request)
    if res.code.to_i != 200
      log_err("ping rejected",
        :url => @url,
        :code => res.code,
        :body => res.body)
      false
    else
      log_info("ping accepted",
        :url => @url)
      true
    end
  rescue Exception => exception
    log_err("ping failed",
      :url => @url,
      :message => exception.message,
      :class => exception.class.name,
      :backtrace => exception.backtrace)
    false
  end
end

#startObject

def initialize



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
66
67
68
69
70
71
72
# File 'lib/logstash/outputs/sumologic/sender.rb', line 39

def start()
  log_info("starting sender...",
    :max => @sender_max, 
    :requeue => @sleep_before_requeue)
  @stopping.make_false()
  @sender_t = Thread.new {
    while @stopping.false?
      begin
        # Resend batch if any in the queue
        batch = @resend_queue.deq(non_block: true)
      rescue ThreadError
        # send new batch otherwise
        begin
          batch = @queue.deq(non_block: true)
        rescue ThreadError
          Stud.stoppable_sleep(@iteration_sleep) { @stopping.true? }
          next
        end
      end
      send_request(batch)
    end # while
    @resend_queue.size.times.map { |queue|
      batch = queue.deq()
      send_request(batch)
    }
    @queue.drain().map { |batch| 
      send_request(batch)
    }
    log_info("waiting while senders finishing...")
    while @tokens.size < @sender_max
      sleep 1
    end # while
  }
end

#stopObject

def start



74
75
76
77
78
79
80
# File 'lib/logstash/outputs/sumologic/sender.rb', line 74

def stop()
  log_info("shutting down sender...")
  @stopping.make_true()
  @queue.enq(Batch.new(Hash.new, STOP_TAG))
  @sender_t.join
  log_info("sender is fully shutted down")
end