Module: Etna::Application

Included in:
EtnaApp
Defined in:
lib/etna/application.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#loggerObject (readonly)

the application logger is available globally



143
144
145
# File 'lib/etna/application.rb', line 143

def logger
  @logger
end

Class Method Details

.find(klass) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/etna/application.rb', line 22

def self.find(klass)
  namespace = klass.name.split('::').first
  if (namespace_klass = Kernel.const_get(namespace)) && (namespace_klass.respond_to? :instance)
    return namespace_klass.instance
  end

  if @@application
    return @@application.instance
  end

  raise "Could not find application instance from #{namespace}, and not subclass of Application found."
end

.included(other) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/etna/application.rb', line 14

def self.included(other)
  other.include Singleton
  other.include Etna::CommandExecutor
  @@application = other

  other.const_set(:GenerateCompletionScript, Class.new(Etna::GenerateCompletionScript))
end

.instanceObject



45
46
47
# File 'lib/etna/application.rb', line 45

def self.instance
  @instance
end

.register(app) ⇒ Object



41
42
43
# File 'lib/etna/application.rb', line 41

def self.register(app)
  @instance = app
end

Instance Method Details

#config(type, env = environment) ⇒ Object



145
146
147
148
149
150
# File 'lib/etna/application.rb', line 145

def config(type, env = environment)
  return nil if @config.nil?
  return nil if @config[env].nil?
  return nil unless @config[env].is_a?(Hash)
  @config[env][type]
end

#configure(opts) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/etna/application.rb', line 53

def configure(opts)
  @config = opts

  if (rollbar_config = config(:rollbar)) && rollbar_config[:access_token]
    Rollbar.configure do |config|
      config.access_token = rollbar_config[:access_token]
    end
  end
end

#dev_routeObject

Used to find the application in development recorded vcr tests. see spec/vcr.rb



37
38
39
# File 'lib/etna/application.rb', line 37

def dev_route
  "#{self.class.name.split('::').first.downcase}.development.local"
end

#enable_job_metrics!Object

This will cause metrics to persist to a file. NOTE – /tmp/metrics.bin should be a persistent mount when using this. You will still need to export metrics in the text format for the node_exporter on the host machine to export them to prometheus. Ensure that the /tmp/metrics.bin is on a named volume or a bind mount, either is fine.



67
68
69
70
71
72
# File 'lib/etna/application.rb', line 67

def enable_job_metrics!
  require 'prometheus'
  Prometheus::Client.config.data_store = Prometheus::Client::DataStores::DirectFileStore.new({
    dir: "/tmp/metrics.bin"
  })
end

#env_config(env = environment) ⇒ Object



152
153
154
155
156
157
# File 'lib/etna/application.rb', line 152

def env_config(env = environment)
  return nil if @config.nil?
  return nil if @config[env].nil?
  return nil unless @config[env].is_a?(Hash)
  @config[env]
end

#environmentObject



163
164
165
# File 'lib/etna/application.rb', line 163

def environment
  (ENV["#{self.class.name.upcase}_ENV"] || :development).to_sym
end

#find_descendents(klass) ⇒ Object



171
172
173
174
175
# File 'lib/etna/application.rb', line 171

def find_descendents(klass)
  ObjectSpace.each_object(Class).select do |k|
    k < klass
  end
end

#idObject



167
168
169
# File 'lib/etna/application.rb', line 167

def id
  ENV["APP_NAME"] || self.class.name.snake_case.split(/::/).last
end

#initializeObject



49
50
51
# File 'lib/etna/application.rb', line 49

def initialize
  Etna::Application.register(self)
end

#run_command(config, *args, &block) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/etna/application.rb', line 177

def run_command(config, *args, &block)
  application = self.id
  status = 'success'
  start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  cmd, cmd_args, cmd_kwds = find_command(*args)

  begin
    cmd.setup(config)
    if block_given?
      return unless yield [cmd, cmd_args]
    end

    cmd.execute(*cmd.fill_in_missing_params(cmd_args), **cmd_kwds)
  rescue => e
    Rollbar.error(e)
    status = 'failed'
    raise
  ensure
    if Yabeda.configured?
      tags = { command: cmd.class.name, status: status, application: application }
      dur = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start

      Yabeda.etna.last_command_completion.set(tags, Time.now.to_i)
      Yabeda.etna.command_runtime.measure(tags, dur)
      write_job_metrics("run_command")
    end
  end
end

#setup_loggerObject



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/etna/application.rb', line 129

def setup_logger
  @logger = Etna::Logger.new(
    # The name of the log_file, required.
    config(:log_file),
    # Number of old copies of the log to keep.
    config(:log_copies) || 5,
    # How large the log can get before overturning.
    config(:log_size) || 1048576,
  )
  log_level = (config(:log_level) || 'warn').upcase.to_sym
  @logger.level = Logger.const_defined?(log_level) ? Logger.const_get(log_level) : Logger::WARN
end

#setup_yabedaObject



74
75
76
77
78
79
80
81
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
109
110
111
# File 'lib/etna/application.rb', line 74

def setup_yabeda
  application = self.id
  Yabeda.configure do
    default_tag :application, application

    group :etna do
      histogram :response_time do
        comment "Time spent by a controller returning a response"
        unit :seconds
        tags [:controller, :action, :user_hash, :project_name]
        buckets [0.01, 0.1, 0.3, 0.5, 1, 5]
      end

      counter :visits do
        comment "Counts visits to the controller"
        tags [:controller, :action, :user_hash, :project_name]
      end

      counter :rollbar_errors do
        comment "Counts errors detected by and sent to rollbar"
      end

      gauge :last_command_completion do
        comment "Unix time of last time command was completed"
        tags [:command, :status, :application]
      end

      histogram :command_runtime do
        comment "Time spent processing a given command"
        tags [:command, :status, :application]
        unit :seconds
        buckets [0.1, 1, 5, 60, 300, 1500]
      end
    end
  end

  Yabeda.configure!
end

#signObject



159
160
161
# File 'lib/etna/application.rb', line 159

def sign
  @sign ||= Etna::SignService.new(self)
end

#write_job_metrics(name) ⇒ Object

Writes all metrics currently gathered to a text format prometheus file. If /tmp/metrics.prom is bind mounted to the host directed bound to the node_exporter’s file exporter directory, these will be exported to prometheus. Combine this enable_job_metrics! for maximum effect.



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/etna/application.rb', line 116

def write_job_metrics(name)
  node_metrics_dir = "/tmp/metrics.prom"
  ::FileUtils.mkdir_p(node_metrics_dir)

  tmp_file = ::File.join(node_metrics_dir, "#{name}.prom.$$")
  ::File.open(tmp_file, "w") do |f|
    f.write(Prometheus::Client::Formats::Text.marshal(Prometheus::Client.registry))
  end

  require 'fileutils'
  ::FileUtils.mv(tmp_file, ::File.join(node_metrics_dir, "#{name}.prom"))
end