File: metrics.rb

package info (click to toggle)
ruby-sentry-ruby-core 5.28.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 672 kB
  • sloc: ruby: 6,118; makefile: 8; sh: 4
file content (68 lines) | stat: -rw-r--r-- 2,401 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
# frozen_string_literal: true

require "sentry/metrics/metric"
require "sentry/metrics/counter_metric"
require "sentry/metrics/distribution_metric"
require "sentry/metrics/gauge_metric"
require "sentry/metrics/set_metric"
require "sentry/metrics/timing"
require "sentry/metrics/aggregator"

module Sentry
  module Metrics
    DURATION_UNITS = %w[nanosecond microsecond millisecond second minute hour day week]
    INFORMATION_UNITS = %w[bit byte kilobyte kibibyte megabyte mebibyte gigabyte gibibyte terabyte tebibyte petabyte pebibyte exabyte exbibyte]
    FRACTIONAL_UNITS = %w[ratio percent]

    OP_NAME = "metric.timing"
    SPAN_ORIGIN = "auto.metric.timing"

    class << self
      def increment(key, value = 1.0, unit: "none", tags: {}, timestamp: nil)
        log_deprecation
        Sentry.metrics_aggregator&.add(:c, key, value, unit: unit, tags: tags, timestamp: timestamp)
      end

      def distribution(key, value, unit: "none", tags: {}, timestamp: nil)
        log_deprecation
        Sentry.metrics_aggregator&.add(:d, key, value, unit: unit, tags: tags, timestamp: timestamp)
      end

      def set(key, value, unit: "none", tags: {}, timestamp: nil)
        log_deprecation
        Sentry.metrics_aggregator&.add(:s, key, value, unit: unit, tags: tags, timestamp: timestamp)
      end

      def gauge(key, value, unit: "none", tags: {}, timestamp: nil)
        log_deprecation
        Sentry.metrics_aggregator&.add(:g, key, value, unit: unit, tags: tags, timestamp: timestamp)
      end

      def timing(key, unit: "second", tags: {}, timestamp: nil, &block)
        log_deprecation

        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

      def log_deprecation
        Sentry.sdk_logger.warn(LOGGER_PROGNAME) do
          "`Sentry::Metrics` is now deprecated and will be removed in the next major."
        end
      end
    end
  end
end