Class: LogStash::Outputs::Coralogix

Inherits:
Base
  • Object
show all
Defined in:
lib/logstash/outputs/coralogix.rb

Instance Method Summary collapse

Instance Method Details

#configureObject

This method is called before starting.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/logstash/outputs/coralogix.rb', line 62

def configure
  begin
    @loggers = {}

    # Overwrite Coralogix endpoint
    unless endpoint.nil?
      ENV["CORALOGIX_LOG_URL"] = "https://#{endpoint}/api/v1/logs"
      ENV["CORALOGIX_TIME_DELTA_URL"] = "https://#{endpoint}/sdk/v1/time"
    end

    require "centralized_ruby_logger"

    #If config parameters doesn't start with $ then we can configure Coralogix logger now.
    if !config_params["APP_NAME"].start_with?("$") && !config_params["SUB_SYSTEM"].start_with?("$")
      @logger = Coralogix::CoralogixLogger.new config_params["PRIVATE_KEY"], config_params["APP_NAME"], config_params["SUB_SYSTEM"], debug, "Logstash (#{version?})", force_compression, proxy
      @configured = true
    end
  rescue Exception => e
    $stderr.write "Failed to configure: #{e}"
  end
end

#extract(record, key, default) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/logstash/outputs/coralogix.rb', line 84

def extract record, key, default
  begin
    res = record
    return key unless key.start_with?("$")
    key[1..-1].split(".").each do |k|
      res = res.fetch(k, nil)
      return default if res == nil
    end
    return res
  rescue Exception => e
    return default
  end
end

#get_app_sub_name(record) ⇒ Object



99
100
101
102
103
# File 'lib/logstash/outputs/coralogix.rb', line 99

def get_app_sub_name(record)
  app_name = extract(record, config_params["APP_NAME"], DEFAULT_APP_NAME)
  sub_name = extract(record, config_params["SUB_SYSTEM"], DEFAULT_SUB_SYSTEM)
  return app_name, sub_name
end

#get_logger(record) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/logstash/outputs/coralogix.rb', line 105

def get_logger(record)

  return @logger if @configured && !@logger.nil?

  app_name, sub_name = get_app_sub_name(record)

  if !@loggers.key?("#{app_name}.#{sub_name}") || @loggers.fetch("#{app_name}.#{sub_name}", nil).nil?
    @loggers["#{app_name}.#{sub_name}"] = Coralogix::CoralogixLogger.new config_params["PRIVATE_KEY"], app_name, sub_name, debug, "Logstash (#{version?})", force_compression, proxy
  end

  return @loggers["#{app_name}.#{sub_name}"]
end

#multi_receive(events) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/logstash/outputs/coralogix.rb', line 27

public def multi_receive(events)
  events.each do |record|
    record = record.to_hash

    logger = get_logger(record)

    log_record = log_key_name != nil ? record.fetch(log_key_name, record) : record
    log_record = (is_json ? log_record.to_json : log_record) rescue log_record
    log_record = log_record.to_s.empty? ? record : log_record

    timestamp = record.fetch(timestamp_key_name, nil)
    if (timestamp.nil?)
      logger.debug log_record
    else
      begin
        float_timestamp = DateTime.parse(timestamp.to_s).to_time.to_f * 1000
        logger.debug log_record, nil, timestamp: float_timestamp
      rescue Exception => e
        logger.debug log_record
      end
    end
  end

  return 1
end

#registerObject



23
24
25
# File 'lib/logstash/outputs/coralogix.rb', line 23

public def register
  configure
end

#version?Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
59
# File 'lib/logstash/outputs/coralogix.rb', line 53

def version?
  begin
    Gem.loaded_specs['logstash-output-coralogix'].version.to_s
  rescue Exception => e
    return '1.0.0'
  end
end