Class: LogStash::Outputs::Loki
- Inherits:
-
Base
- Object
- Base
- LogStash::Outputs::Loki
- Includes:
- Loki
- Defined in:
- lib/logstash/outputs/loki.rb
Instance Attribute Summary collapse
-
#batches ⇒ Object
readonly
Returns the value of attribute batches.
Instance Method Summary collapse
-
#add_entry_to_batch(e, tenant = 'default') ⇒ Object
Add an entry to the current batch returns false if the batch is full and the entry can’t be added.
- #batch(tenant = 'default') ⇒ Object
-
#batch_size ⇒ Object
‘Maximum batch size to accrue before pushing to loki.
-
#batch_wait ⇒ Object
‘Interval in seconds to wait before pushing a batch of records to loki.
-
#ca_cert ⇒ Object
‘TLS’.
-
#cert ⇒ Object
‘Client certificate’.
- #close ⇒ Object
-
#insecure_skip_verify ⇒ Object
‘Disable server certificate verification’.
- #is_batch_expired(tenant = 'default') ⇒ Object
- #load_ssl ⇒ Object
- #loki_http_request(payload, tenant = 'default') ⇒ Object
- #max_batch_size ⇒ Object
- #max_batch_wait ⇒ Object
-
#max_delay ⇒ Object
‘Backoff configuration.
-
#message_field ⇒ Object
‘Log line field to pick from logstash.
-
#min_delay ⇒ Object
‘Backoff configuration.
- #receive(event) ⇒ Object
- #register ⇒ Object
-
#retries ⇒ Object
‘Backoff configuration.
- #send(batch, tenant = 'default') ⇒ Object
- #send_batch_for_tenant(tenant) ⇒ Object
-
#single ⇒ Object
‘A single instance of the Output will be shared among the pipeline worker threads’.
- #ssl_cert? ⇒ Boolean
- #ssl_opts(uri) ⇒ Object
-
#tenant_id ⇒ Object
‘Loki Tenant ID’.
-
#url ⇒ Object
‘Loki URL’.
-
#username ⇒ Object
‘BasicAuth credentials’.
- #validate_ssl_key ⇒ Object
Methods included from Loki
Instance Attribute Details
#batches ⇒ Object (readonly)
Returns the value of attribute batches.
56 57 58 |
# File 'lib/logstash/outputs/loki.rb', line 56 def batches @batches end |
Instance Method Details
#add_entry_to_batch(e, tenant = 'default') ⇒ Object
Add an entry to the current batch returns false if the batch is full and the entry can’t be added.
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/logstash/outputs/loki.rb', line 193 def add_entry_to_batch(e, tenant = 'default') line = e.entry['line'] # we don't want to send empty lines. return true if line.to_s.strip.empty? tenant = 'default' if tenant.nil? or tenant.empty? if @batches.nil? @batches = Hash.new end if !@batches.key?(tenant) @batches[tenant] = Batch.new(e) return true end if @batches[tenant].size_bytes_after(line) > @batch_size return false end @batches[tenant].add(e) return true end |
#batch(tenant = 'default') ⇒ Object
91 92 93 94 95 96 |
# File 'lib/logstash/outputs/loki.rb', line 91 def batch(tenant = 'default') return nil if @batches.nil? return @batches[tenant] if !tenant.nil? && !tenant.empty? && @batches.key?(tenant) return @batches['default'] if @batches.key?('default') return nil end |
#batch_size ⇒ Object
‘Maximum batch size to accrue before pushing to loki. Defaults to 102400 bytes’
39 |
# File 'lib/logstash/outputs/loki.rb', line 39 config :batch_size, :validate => :number, :default => 102400, :required => false |
#batch_wait ⇒ Object
‘Interval in seconds to wait before pushing a batch of records to loki. Defaults to 1 second’
42 |
# File 'lib/logstash/outputs/loki.rb', line 42 config :batch_wait, :validate => :number, :default => 1, :required => false |
#ca_cert ⇒ Object
‘TLS’
30 |
# File 'lib/logstash/outputs/loki.rb', line 30 config :ca_cert, :validate => :path, :required => false |
#cert ⇒ Object
‘Client certificate’
26 |
# File 'lib/logstash/outputs/loki.rb', line 26 config :cert, :validate => :path, :required => false |
#close ⇒ Object
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/logstash/outputs/loki.rb', line 232 def close @entries.close @mutex.synchronize do @stop = true end @batch_wait_thread.join @batch_size_thread.join # if by any chance we still have a forming batch, we need to send it. @batches.keys.each { |tenant| send_batch_for_tenant(tenant) } @batches.clear() @batches = nil end |
#insecure_skip_verify ⇒ Object
‘Disable server certificate verification’
33 |
# File 'lib/logstash/outputs/loki.rb', line 33 config :insecure_skip_verify, :validate => :boolean, :default => false, :required => false |
#is_batch_expired(tenant = 'default') ⇒ Object
217 218 219 220 |
# File 'lib/logstash/outputs/loki.rb', line 217 def is_batch_expired(tenant = 'default') tenant = 'default' if tenant.nil? or tenant.empty? return !@batches.nil? && @batches.key?(tenant) && @batches[tenant].age() >= @batch_wait end |
#load_ssl ⇒ Object
152 153 154 155 |
# File 'lib/logstash/outputs/loki.rb', line 152 def load_ssl @cert = OpenSSL::X509::Certificate.new(File.read(@cert)) if @cert @key = OpenSSL::PKey.read(File.read(@key)) if @key end |
#loki_http_request(payload, tenant = 'default') ⇒ Object
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/logstash/outputs/loki.rb', line 258 def loki_http_request(payload, tenant = 'default') req = Net::HTTP::Post.new( @uri.request_uri ) req.add_field('Content-Type', 'application/json') if !tenant.nil? && !tenant.empty? && !tenant.eql?('default') req.add_field('X-Scope-OrgID', tenant) elsif !@tenant_id.nil? && !@tenant_id.empty? req.add_field('X-Scope-OrgID', @tenant_id) end req['User-Agent']= 'loki-logstash' req.basic_auth(@username, @password) if @username req.body = payload opts = ssl_opts(@uri) @logger.debug("sending #{req.body.length} bytes to loki. tenant #{tenant}") retry_count = 0 delay = @min_delay begin res = Net::HTTP.start(@uri.host, @uri.port, **opts) { |http| http.request(req) } return res if !res.nil? && res.code.to_i != 429 && res.code.to_i.div(100) != 5 raise StandardError.new res rescue StandardError => e retry_count += 1 @logger.warn("Failed to send batch, attempt: #{retry_count}/#{@retries}", :error_inspect => e.inspect, :error => e) if retry_count < @retries sleep delay if delay * 2 <= @max_delay delay = delay * 2 else delay = @max_delay end retry else @logger.error("Failed to send batch", :error_inspect => e.inspect, :error => e) return res end end end |
#max_batch_size ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/logstash/outputs/loki.rb', line 98 def max_batch_size loop do @mutex.synchronize do return if @stop end e = @entries.deq return if e.nil? tenant = nil tenant = e.labels['tenant'] if !e.labels.nil? && e.labels.key?('tenant') tenant = 'default' if tenant.nil? or tenant.empty? @mutex.synchronize do if !add_entry_to_batch(e, tenant) @logger.debug("Max batch_size is reached. Sending batch to loki. Tenant #{tenant}") send_batch_for_tenant(tenant) @batches[tenant] = Batch.new(e) end end end end |
#max_batch_wait ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/logstash/outputs/loki.rb', line 121 def max_batch_wait # minimum wait frequency is 10 milliseconds min_wait_checkfrequency = 1/100 max_wait_checkfrequency = @batch_wait if max_wait_checkfrequency < min_wait_checkfrequency max_wait_checkfrequency = min_wait_checkfrequency end loop do @mutex.synchronize do return if @stop end sleep(max_wait_checkfrequency) @mutex.synchronize do @batches.keys.clone.each { |tenant| if is_batch_expired(tenant) @logger.debug("Max batch_wait time is reached. Sending batch to loki. Tenant #{tenant}") send_batch_for_tenant(tenant) @batches.delete(tenant) end } end end end |
#max_delay ⇒ Object
‘Backoff configuration. Maximum backoff time between retries. Default 300s’
51 |
# File 'lib/logstash/outputs/loki.rb', line 51 config :max_delay, :validate => :number, :default => 300, :required => false |
#message_field ⇒ Object
‘Log line field to pick from logstash. Defaults to “message”’
45 |
# File 'lib/logstash/outputs/loki.rb', line 45 config :message_field, :validate => :string, :default => "message", :required => false |
#min_delay ⇒ Object
‘Backoff configuration. Initial backoff time between retries. Default 1s’
48 |
# File 'lib/logstash/outputs/loki.rb', line 48 config :min_delay, :validate => :number, :default => 1, :required => false |
#receive(event) ⇒ Object
228 229 230 |
# File 'lib/logstash/outputs/loki.rb', line 228 def receive(event) @entries << Entry.new(event, @message_field) end |
#register ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/logstash/outputs/loki.rb', line 58 def register @uri = URI.parse(@url) unless @uri.is_a?(URI::HTTP) || @uri.is_a?(URI::HTTPS) raise LogStash::ConfigurationError, "url parameter must be valid HTTP, currently '#{@url}'" end if @min_delay > @max_delay raise LogStash::ConfigurationError, "Min delay should be less than Max delay, currently 'Min delay is #{@min_delay} and Max delay is #{@max_delay}'" end @logger.info("Loki output plugin", :class => self.class.name) # initialize Queue and Mutex @entries = Queue.new @mutex = Mutex.new @stop = false # create nil batch object. @batches = Hash.new # validate certs if ssl_cert? load_ssl validate_ssl_key end # start batch_max_wait and batch_max_size threads @batch_wait_thread = Thread.new{max_batch_wait()} @batch_size_thread = Thread.new{max_batch_size()} end |
#retries ⇒ Object
‘Backoff configuration. Maximum number of retries to do’
54 |
# File 'lib/logstash/outputs/loki.rb', line 54 config :retries, :validate => :number, :default => 10, :required => false |
#send(batch, tenant = 'default') ⇒ Object
248 249 250 251 252 253 254 255 256 |
# File 'lib/logstash/outputs/loki.rb', line 248 def send(batch, tenant = 'default') payload = batch.to_json res = loki_http_request(payload, tenant) if res.is_a?(Net::HTTPSuccess) @logger.debug("Successfully pushed data to loki") else @logger.debug("failed payload", :payload => payload) end end |
#send_batch_for_tenant(tenant) ⇒ Object
222 223 224 |
# File 'lib/logstash/outputs/loki.rb', line 222 def send_batch_for_tenant(tenant) send(batch(tenant), tenant) end |
#single ⇒ Object
‘A single instance of the Output will be shared among the pipeline worker threads’
16 |
# File 'lib/logstash/outputs/loki.rb', line 16 concurrency :single |
#ssl_cert? ⇒ Boolean
148 149 150 |
# File 'lib/logstash/outputs/loki.rb', line 148 def ssl_cert? !@key.nil? && !@cert.nil? end |
#ssl_opts(uri) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/logstash/outputs/loki.rb', line 163 def ssl_opts(uri) opts = { use_ssl: uri.scheme == 'https' } # disable server certificate verification if @insecure_skip_verify opts = opts.merge( verify_mode: OpenSSL::SSL::VERIFY_NONE ) end if !@cert.nil? && !@key.nil? opts = opts.merge( verify_mode: OpenSSL::SSL::VERIFY_PEER, cert: @cert, key: @key ) end unless @ca_cert.nil? opts = opts.merge( ca_file: @ca_cert ) end opts end |
#tenant_id ⇒ Object
‘Loki Tenant ID’
36 |
# File 'lib/logstash/outputs/loki.rb', line 36 config :tenant_id, :validate => :string, :required => false |
#url ⇒ Object
‘Loki URL’
19 |
# File 'lib/logstash/outputs/loki.rb', line 19 config :url, :validate => :string, :required => true |
#username ⇒ Object
‘BasicAuth credentials’
22 |
# File 'lib/logstash/outputs/loki.rb', line 22 config :username, :validate => :string, :required => false |
#validate_ssl_key ⇒ Object
157 158 159 160 161 |
# File 'lib/logstash/outputs/loki.rb', line 157 def validate_ssl_key if !@key.is_a?(OpenSSL::PKey::RSA) && !@key.is_a?(OpenSSL::PKey::DSA) raise LogStash::ConfigurationError, "Unsupported private key type '#{@key.class}''" end end |