Class: MnUtilsLogging::SiteAction

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/mn_utils_gem/site_action.rb

Instance Method Summary collapse

Constructor Details

#initializeSiteAction



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
56
57
58
59
60
61
62
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/mn_utils_gem/site_action.rb', line 29

def initialize
  # add new site actions here under the relevant group
  # NO DUPLICATES please - a site action can only exist under one group
  @_site_actions_and_groups = {
      test: [
          :test_event_1,
          :test_event_2
      ],
      reg: [
          :reg_success_via_email,
          :reg_success_via_google,
          :reg_success_via_facebook,
          :reg_attempt_invalid_email,
          :reg_attempt_invalid_password,
          :reg_attempt_invalid_username,
          :reg_conf_link_email_success,
          :reg_conf_link_email_fail,
          :reg_welcome_email_success,
          :reg_welcome_email_fail,
          :reg_conf_remind_email_success,
          :reg_conf_remind_email_fail,
          :bn_reg_email_success,
          :bn_reg_email_fail,
          :bn_reg_activation_email_success,
          :bn_reg_activation_email_fail,
          :insight_welcome_email_success,
          :insight_welcome_email_fail,
          :bn_reg_success,
          :bn_reg_fail
      ],
      auth: [
          :login_success_via_email,
          :login_success_via_google,
          :login_success_via_facebook,
          :login_attempt_invalid_email,
          :login_attempt_nonexistent_email,
          :login_attempt_invalid_password,
          :login_attempt_incorrect_password
      ],
      account: [
          :email_change_conf_link_email_success,
          :email_change_conf_link_email_fail,
          :pwd_reset_link_email_success,
          :pwd_reset_link_email_fail,
          :dereg_rqst_email_success,
          :dereg_rqst_email_fail,
          :email_change_conf_email_success,
          :email_change_conf_email_fail
      ],
      pm: [
          :pm_notif_email_success,
          :pm_notif_email_fail
      ],
      talk: [
          :watch_thread_notif_email_success,
          :watch_thread_notif_email_fail,
          :media_rqst_welcome_email_success,
          :media_rqst_welcome_email_fail,
          :mention_notif_email_success,
          :mention_notif_email_fail
      ],
      voting: [
          :aibu_vote_success,
          :aibu_thread_email_success,
          :aibu_thread_email_fail
      ],
      admin: [
          :admin_email_success,
          :admin_email_fail,
      ],
      email: [
          :send_email_success,
          :send_email_fail
      ],
      misc: [
          :unknown_email_success,
          :unknown_email_fail
      ]
  }.freeze

  # check for duplicates, and throw a tantrum if any are found
  tmp_map = {}
  @_site_actions_and_groups.each do |k, arr|
    arr.each do |v|
      raise ArgumentError, "Site action #{v} is declared more than once" \
          if tmp_map.key? v
      tmp_map[v] = true
    end
  end

  # create a reverse map of all site actions and their corresponding group
  # for quick lookups by the code
  @_site_action_group_map = Hash[*(@_site_actions_and_groups.map {|k, arr| arr.map {|v| [v, k]}}.flatten)].freeze

  # setup the logger
  @logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
end

Instance Method Details

#log(message, site_action, payload = {}) ⇒ Object

Raises:

  • (ArgumentError)


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/mn_utils_gem/site_action.rb', line 127

def log(message, site_action, payload = {})

  # validate the parameters
  raise ArgumentError, 'message cannot be blank' \
    if message.blank?
  raise ArgumentError, 'site_action must be a symbol' \
    unless site_action.is_a?(Symbol)
  raise ArgumentError, 'payload must be a hash' \
    unless payload.is_a?(Hash)
  raise ArgumentError, 'site_action value is not in allowed list' \
    unless @_site_action_group_map.key? site_action

  # validate the payload hash
  payload.each do |key, value|
    raise ArgumentError, "payload key #{key} must be a symbol" \
      unless key.is_a?(Symbol)
    key_string = key.to_s
    raise ArgumentError, "payload key #{key} must begin with an underscore" \
      unless key_string.chars.first == '_'
    raise ArgumentError, "payload key #{key} must be at least 2 characters long" \
      unless key_string.length >= 2
    raise ArgumentError, "payload key cannot be _id" \
      if key_string == '_id'
    raise ArgumentError, "payload value for key #{key} must be a string or symbol" \
      unless value.is_a?(String) || value.is_a?(Symbol)
  end

  # validate required environment variables if we are in production
  if ENV.key?( 'CLOUDWATCH_ROOT_NAMESPACE') \
      || ENV.key?('GRAYLOG_GELF_UDP_HOST') \
      || ENV.key?('GRAYLOG_GELF_UDP_PORT')
    raise ArgumentError, "ENV['CLOUDWATCH_ROOT_NAMESPACE'] is required" \
      unless ENV.key? 'CLOUDWATCH_ROOT_NAMESPACE'
    raise ArgumentError, "ENV['GRAYLOG_GELF_UDP_HOST'] is required" \
      unless ENV.key? 'GRAYLOG_GELF_UDP_HOST'
    raise ArgumentError, "ENV['GRAYLOG_GELF_UDP_PORT'] is required" \
      unless ENV.key? 'GRAYLOG_GELF_UDP_PORT'
    raise ArgumentError, "ENV['SITE_HOSTNAME'] is required" \
      unless ENV.key? 'SITE_HOSTNAME'
    raise ArgumentError, "ENV['SRV_CODE'] is required" \
      unless ENV.key? 'SRV_CODE'
  end

  site_action_group = @_site_action_group_map[site_action]

  # setup the full payload
  full_payload = payload.dup
  full_payload[:short_message] = message
  full_payload[:_site_action_group] = site_action_group
  full_payload[:_site_action] = site_action

  # add other data to the payload if available
  full_payload[:_srv_code] = ENV['SRV_CODE'] if ENV.key? 'SRV_CODE'
  full_payload[:_site_hostname] = ENV['SITE_HOSTNAME'] if ENV.key? 'SITE_HOSTNAME'
  full_payload[:_request_id] ||= RequestStore.store[:request_id] if RequestStore.store[:request_id]
  full_payload[:_remote_ip] = RequestStore.store[:remote_ip] if RequestStore.store[:remote_ip]

  # send it off
  send_to_graylog full_payload
  send_to_cloudwatch(site_action_group, site_action, full_payload[:_site_hostname])
end

#log_metric_only(site_action) ⇒ Object

Raises:

  • (ArgumentError)


189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/mn_utils_gem/site_action.rb', line 189

def log_metric_only(site_action)
  
  # validate the parameters
  raise ArgumentError, 'site_action must be a symbol' \
    unless site_action.is_a?(Symbol)
  raise ArgumentError, 'site_action value is not in allowed list' \
    unless @_site_action_group_map.key? site_action

  # validate required environment variables if we are in production
  if ENV.key?( 'CLOUDWATCH_ROOT_NAMESPACE')
    raise ArgumentError, "ENV['SITE_HOSTNAME'] is required" \
      unless ENV.key? 'SITE_HOSTNAME'
  end

  site_action_group = @_site_action_group_map[site_action]

  send_to_cloudwatch(site_action_group, site_action, ENV['SITE_HOSTNAME'])
end