Class: Cosmos::TextLogMicroservice

Inherits:
Microservice show all
Defined in:
lib/cosmos/microservices/text_log_microservice.rb

Instance Attribute Summary

Attributes inherited from Microservice

#count, #custom, #error, #microservice_status_thread, #name, #scope, #state

Instance Method Summary collapse

Methods inherited from Microservice

#as_json, run, #shutdown

Constructor Details

#initialize(name) ⇒ TextLogMicroservice

Returns a new instance of TextLogMicroservice.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cosmos/microservices/text_log_microservice.rb', line 25

def initialize(name)
  super(name)
  @config['options'].each do |option|
    case option[0].upcase
    when 'CYCLE_TIME' # Maximum time between log files
      @cycle_time = option[1].to_i
    when 'CYCLE_SIZE' # Maximum size of a log file
      @cycle_size = option[1].to_i
    else
      Logger.error("Unknown option passed to microservice #{@name}: #{option}")
    end
  end

  # These settings limit the log file to 10 minutes or 50MB of data, whichever comes first
  @cycle_time = 600 unless @cycle_time # 10 minutes
  @cycle_size = 50_000_000 unless @cycle_size # ~50 MB
end

Instance Method Details

#log_data(tlws, topic, msg_id, msg_hash, redis) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/cosmos/microservices/text_log_microservice.rb', line 68

def log_data(tlws, topic, msg_id, msg_hash, redis)
  start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  keys = msg_hash.keys
  keys.delete("time")
  entry = keys.reduce("") { |data, key| data + "#{key}: #{msg_hash[key]}\t" }
  tlws[topic].write(msg_hash["time"].to_i, entry, msg_id)
  @count += 1
  diff = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start # seconds as a float
  @metric.add_sample(name: "log_duration_seconds", value: diff, labels: {})
rescue => err
  @error = err
  Logger.error("#{@name} error: #{err.formatted}")
end

#runObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cosmos/microservices/text_log_microservice.rb', line 43

def run
  tlws = setup_tlws
  while true
    break if @cancel_thread

    Topic.read_topics(@topics) do |topic, msg_id, msg_hash, redis|
      break if @cancel_thread

      log_data(tlws, topic, msg_id, msg_hash, redis)
    end
  end
end

#setup_tlwsObject



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/cosmos/microservices/text_log_microservice.rb', line 56

def setup_tlws
  tlws = {}
  @topics.each do |topic|
    topic_split = topic.gsub(/{|}/, '').split("__") # Remove the redis hashtag curly braces
    scope = topic_split[0]
    log_name = topic_split[1]
    remote_log_directory = "#{scope}/text_logs/#{log_name}"
    tlws[topic] = TextLogWriter.new(remote_log_directory, true, @cycle_time, @cycle_size, redis_topic: topic)
  end
  return tlws
end