Method: Sentry::Metrics.timing

Defined in:
lib/sentry/metrics.rb

.timing(key, unit: "second", tags: {}, timestamp: nil, &block) ⇒ Object


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/sentry/metrics.rb', line 37

def timing(key, unit: "second", tags: {}, timestamp: nil, &block)
  return unless block_given?
  return yield unless DURATION_UNITS.include?(unit)

  result, value = Sentry.with_child_span(op: OP_NAME, description: key, origin: SPAN_ORIGIN) do |span|
    tags.each { |k, v| span.set_tag(k, v.is_a?(Array) ? v.join(", ") : v.to_s) } if span

    start = Timing.send(unit.to_sym)
    result = yield
    value = Timing.send(unit.to_sym) - start

    [result, value]
  end

  Sentry.metrics_aggregator&.add(:d, key, value, unit: unit, tags: tags, timestamp: timestamp)
  result
end