Class: RubyForGrafanaLoki::Client
- Inherits:
-
Object
- Object
- RubyForGrafanaLoki::Client
- Includes:
- Request
- Defined in:
- lib/ruby_for_grafana_loki/client.rb
Instance Attribute Summary collapse
-
#host_name ⇒ Object
Returns the value of attribute host_name.
-
#job_name ⇒ Object
Returns the value of attribute job_name.
-
#source_name ⇒ Object
Returns the value of attribute source_name.
Instance Method Summary collapse
-
#initialize(log_file_path, allowed_logs_type = LOGS_TYPE) ⇒ Client
constructor
A new instance of Client.
- #match_logs_type?(log_line) ⇒ Boolean
- #send_all_logs ⇒ Object
- #send_log(log_message) ⇒ Object
Methods included from Request
Constructor Details
#initialize(log_file_path, allowed_logs_type = LOGS_TYPE) ⇒ Client
Returns a new instance of Client.
11 12 13 14 15 16 17 |
# File 'lib/ruby_for_grafana_loki/client.rb', line 11 def initialize(log_file_path, allowed_logs_type = LOGS_TYPE) @log_file_path = log_file_path @allowed_logs_type = allowed_logs_type @job_name = "job name" @host_name = "host name" @source_name = "source name" end |
Instance Attribute Details
#host_name ⇒ Object
Returns the value of attribute host_name.
8 9 10 |
# File 'lib/ruby_for_grafana_loki/client.rb', line 8 def host_name @host_name end |
#job_name ⇒ Object
Returns the value of attribute job_name.
7 8 9 |
# File 'lib/ruby_for_grafana_loki/client.rb', line 7 def job_name @job_name end |
#source_name ⇒ Object
Returns the value of attribute source_name.
9 10 11 |
# File 'lib/ruby_for_grafana_loki/client.rb', line 9 def source_name @source_name end |
Instance Method Details
#match_logs_type?(log_line) ⇒ Boolean
57 58 59 60 61 |
# File 'lib/ruby_for_grafana_loki/client.rb', line 57 def match_logs_type?(log_line) type = log_line.match(/(ERROR|WARN|FATAL|INFO|DEBUG)/)&.to_s @allowed_logs_type.include?(type) end |
#send_all_logs ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/ruby_for_grafana_loki/client.rb', line 19 def send_all_logs File.open(@log_file_path, 'r') do |file| file.each_line do |line| send_log(line) if match_logs_type?(line) end end end |
#send_log(log_message) ⇒ 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 52 53 54 55 |
# File 'lib/ruby_for_grafana_loki/client.rb', line 27 def send_log() curr_datetime = Time.now.to_i * 1_000_000_000 msg = "On server #{@host_name} detected error" payload = { 'streams' => [ { 'stream' => { 'source' => @source_name, 'job' => @job_name, 'host' => @host_name }, 'values' => [[curr_datetime.to_s, ]], 'entries' => [ { 'ts' => curr_datetime, 'line' => "[WARN] " + msg } ] } ] } json_payload = JSON.generate(payload) uri = '/loki/api/v1/push' post(uri, json_payload) end |