Skip to content

Using return or raising exception causes metric collection to be skipped in distribution/measure #133

@Bringer128

Description

@Bringer128

Ruby interpreter version: 2.4.4p296
The statsd-instrument version. v2.3.0.beta5
The StatsD backend you are using. UDPBackend

Using StatsD.distribution or StatsD.measure it is possible to skip the metric collection. This can happen if an exception is raised or if you return from the block.

An example of it working successfully:

def expensive_method
  StatsD.measure('my_metric') do
    result = expensive_operation
    result.success
  end
end

An example of it breaking:

def expensive_method_broken_metrics
  StatsD.measure('my_metric') do
    result = expensive_operation
    return true if result.success # Skips metric collection
    false
  end
end

The fix would be something like:

def measure(key, value = nil, *metric_options, &block)
  value, metric_options = parse_options(value, metric_options)
  type = (!metric_options.empty? && metric_options.first[:as_dist] ? :d : :ms)

  result = nil
  value  = 1000 * StatsD::Instrument.duration { result = block.call } if block_given?
ensure # Add this
  metric = collect_metric(type, key, value, metric_options)
  result = metric unless block_given?
  return result # Make sure this value is returned from measure
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions