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



132
133
134
# File 'lib/etna/application.rb', line 132

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



134
135
136
137
138
139
# File 'lib/etna/application.rb', line 134

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

#env_config(env = environment) ⇒ Object



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

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



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

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

#find_descendents(klass) ⇒ Object



160
161
162
163
164
# File 'lib/etna/application.rb', line 160

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

#idObject



156
157
158
# File 'lib/etna/application.rb', line 156

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



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/etna/application.rb', line 166

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 defined?(Yabeda) && 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.#{cmd.class.name}")
    end
  end
end

#setup_loggerObject



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

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



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
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/etna/application.rb', line 63

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



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

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.



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

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