Module: StatsD::Instrument::StrictMetaprogramming

Included in:
StatsD::Instrument
Defined in:
lib/statsd/instrument/strict.rb

Instance Method Summary collapse

Instance Method Details

#statsd_count(method, name, sample_rate: nil, tags: nil, no_prefix: false) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/statsd/instrument/strict.rb', line 204

def statsd_count(method, name, sample_rate: nil, tags: nil, no_prefix: false)
  check_method_and_metric_name(method, name)

  # Unfortunately, we have to inline the new method implementation ebcause we have to fix the
  # Stats.increment call to not use the `prefix` argument.

  add_to_method(method, name, :count) do
    define_method(method) do |*args, &block|
      key = StatsD::Instrument.generate_metric_name(name, self, *args)
      StatsD.increment(key, sample_rate: sample_rate, tags: tags, no_prefix: no_prefix)
      super(*args, &block)
    end
  end
end

#statsd_count_if(method, name, sample_rate: nil, tags: nil, no_prefix: false) ⇒ Object



172
173
174
175
176
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
# File 'lib/statsd/instrument/strict.rb', line 172

def statsd_count_if(method, name, sample_rate: nil, tags: nil, no_prefix: false)
  check_method_and_metric_name(method, name)

  # Unfortunately, we have to inline the new method implementation ebcause we have to fix the
  # Stats.increment call to not use the `prefix` argument.

  add_to_method(method, name, :count_if) do
    define_method(method) do |*args, &block|
      begin
        truthiness = result = super(*args, &block)
      rescue
        truthiness = false
        raise
      else
        if block_given?
          begin
            truthiness = yield(result)
          rescue
            truthiness = false
          end
        end
        result
      ensure
        if truthiness
          key = StatsD::Instrument.generate_metric_name(name, self, *args)
          StatsD.increment(key, sample_rate: sample_rate, tags: tags, no_prefix: no_prefix)
        end
      end
    end
  end
end

#statsd_count_success(method, name, sample_rate: nil, tags: nil, no_prefix: false) ⇒ Object



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
# File 'lib/statsd/instrument/strict.rb', line 141

def statsd_count_success(method, name, sample_rate: nil, tags: nil, no_prefix: false)
  check_method_and_metric_name(method, name)

  # Unfortunately, we have to inline the new method implementation ebcause we have to fix the
  # Stats.increment call to not use the `prefix` argument.

  add_to_method(method, name, :count_success) do
    define_method(method) do |*args, &block|
      begin
        truthiness = result = super(*args, &block)
      rescue
        truthiness = false
        raise
      else
        if block_given?
          begin
            truthiness = yield(result)
          rescue
            truthiness = false
          end
        end
        result
      ensure
        suffix = truthiness == false ? 'failure' : 'success'
        key = "#{StatsD::Instrument.generate_metric_name(name, self, *args)}.#{suffix}"
        StatsD.increment(key, sample_rate: sample_rate, tags: tags, no_prefix: no_prefix)
      end
    end
  end
end

#statsd_distribution(method, name, sample_rate: nil, tags: nil, no_prefix: false) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/statsd/instrument/strict.rb', line 125

def statsd_distribution(method, name, sample_rate: nil, tags: nil, no_prefix: false)
  check_method_and_metric_name(method, name)

  # Unfortunately, we have to inline the new method implementation ebcause we have to fix the
  # Stats.distribution call to not use the `prefix` argument.

  add_to_method(method, name, :distribution) do
    define_method(method) do |*args, &block|
      key = StatsD::Instrument.generate_metric_name(name, self, *args)
      StatsD.distribution(key, sample_rate: sample_rate, tags: tags, no_prefix: no_prefix) do
        super(*args, &block)
      end
    end
  end
end

#statsd_measure(method, name, sample_rate: nil, tags: nil, no_prefix: false) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/statsd/instrument/strict.rb', line 110

def statsd_measure(method, name, sample_rate: nil, tags: nil, no_prefix: false)
  check_method_and_metric_name(method, name)

  # Unfortunately, we have to inline the new method implementation ebcause we have to fix the
  # Stats.measure call to not use the `as_dist` and `prefix` arguments.
  add_to_method(method, name, :measure) do
    define_method(method) do |*args, &block|
      key = StatsD::Instrument.generate_metric_name(name, self, *args)
      StatsD.measure(key, sample_rate: sample_rate, tags: tags, no_prefix: no_prefix) do
        super(*args, &block)
      end
    end
  end
end