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



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

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



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

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 = Etna::EnvironmentVariables.load_from_env('ETNA', root: opts) { |path, value| load_config_from_env_path(path, value) }

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

#controller_tagsObject



71
72
73
# File 'lib/etna/application.rb', line 71

def controller_tags
  [:controller, :action, :project_name, :user_hash]
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



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

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



165
166
167
# File 'lib/etna/application.rb', line 165

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

#find_descendents(klass) ⇒ Object



177
178
179
180
181
# File 'lib/etna/application.rb', line 177

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

#idObject



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

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

#load_config_from_env_path(path, value) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/etna/application.rb', line 63

def load_config_from_env_path(path, value)
  return nil if path.empty?
  return nil unless path.last.end_with?('_file')
  path.last.sub!(/_file$/, '')

  [path.map(&:to_sym), YAML.load(File.read(value))]
end

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



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/etna/application.rb', line 183

def run_command(config, *args, &block)
  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)
    raise
  end
end

#setup_loggerObject



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

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



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
112
113
# File 'lib/etna/application.rb', line 75

def setup_yabeda
  application = self.id
  ctags = self.controller_tags
  Yabeda.configure do
    default_tag :application, application
    default_tag :project_name, 'unknown'
    default_tag :controller, 'none'
    default_tag :action, 'none'
    default_tag :user_hash, 'unknown'

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

      histogram :perf do
        comment "Time spent inside code path"
        unit :seconds
        tags ctags + [:class_name, :method_name]
        buckets [0.01, 0.1, 0.3, 0.5, 1, 5]
      end

      counter :visits do
        comment "Counts visits to the controller"
        tags ctags
      end

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

  Yabeda.configure!
end

#signObject



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

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

#test?Boolean

Returns:

  • (Boolean)


169
170
171
# File 'lib/etna/application.rb', line 169

def test?
  environment == "test"
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.



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

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