Class: Speedshop::Cloudwatch::Reporter

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/speedshop/cloudwatch/reporter.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeReporter

Returns a new instance of Reporter.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/speedshop/cloudwatch/reporter.rb', line 11

def initialize
  @mutex = Mutex.new
  @condition_variable = ConditionVariable.new
  @queue = []
  @collectors = []
  @thread = nil
  @pid = Process.pid
  @running = false
  @dropped_since_last_flush = 0
  @last_overflow_log = nil
end

Class Method Details

.resetObject



121
122
123
124
125
126
127
128
# File 'lib/speedshop/cloudwatch/reporter.rb', line 121

def self.reset
  if instance_variable_defined?(:@singleton__instance__)
    reporter = instance_variable_get(:@singleton__instance__)
    reporter&.stop! if reporter&.started?
    reporter&.clear_all
  end
  instance_variable_set(:@singleton__instance__, nil)
end

Instance Method Details

#clear_allObject



105
106
107
108
109
110
# File 'lib/speedshop/cloudwatch/reporter.rb', line 105

def clear_all
  @mutex.synchronize do
    @queue.clear
    @collectors.clear
  end
end

#flush_now!Object

Force immediate metrics collection and flush (for testing) This bypasses the normal interval-based flushing



114
115
116
117
118
119
# File 'lib/speedshop/cloudwatch/reporter.rb', line 114

def flush_now!
  return unless @running

  collect_metrics
  flush_metrics
end

#report(metric:, value: nil, statistic_values: nil, dimensions: {}, integration: nil) ⇒ Object



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
# File 'lib/speedshop/cloudwatch/reporter.rb', line 71

def report(metric:, value: nil, statistic_values: nil, dimensions: {}, integration: nil)
  return unless config.environment_enabled?

  metric_name = metric.to_sym
  int = integration || find_integration_for_metric(metric_name)
  return unless int
  return unless metric_allowed?(int, metric_name)

  metric_object = METRICS[int]&.find { |m| m.name == metric_name }
  ns = config.namespaces[int]
  unit = metric_object&.unit || "None"

  dimensions_array = dimensions.map { |k, v| {name: k.to_s, value: v.to_s} }
  all_dimensions = dimensions_array + custom_dimensions

  datum = {metric_name: metric_name.to_s, namespace: ns, unit: unit,
           dimensions: all_dimensions, timestamp: Time.now}
  if statistic_values
    datum[:statistic_values] = statistic_values
  else
    datum[:value] = value
  end

  @mutex.synchronize do
    if @queue.size >= config.queue_max_size
      @queue.shift
      @dropped_since_last_flush += 1
    end
    @queue << datum
  end

  start! unless started?
end

#start!Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/speedshop/cloudwatch/reporter.rb', line 23

def start!
  return if !config.environment_enabled? || started?

  @mutex.synchronize do
    return if started?

    initialize_collectors
    if forked?
      @collectors.clear
      @queue.clear
    end

    Speedshop::Cloudwatch.log_info("Starting metric reporter (collectors: #{@collectors.map(&:class).join(", ")})")
    @running = true
    @thread = Thread.new do
      Thread.current.thread_variable_set(:fork_safe, true)
      Thread.current.name = "scw_reporter"
      run_loop
    end
  end
end

#started?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/speedshop/cloudwatch/reporter.rb', line 45

def started?
  @running && @thread&.alive?
end

#stop!Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/speedshop/cloudwatch/reporter.rb', line 49

def stop!
  thread_to_join = nil
  @mutex.synchronize do
    return unless @running
    Speedshop::Cloudwatch.log_info("Stopping metric reporter")
    @running = false
    @condition_variable.signal
    thread_to_join = @thread
    @thread = @pid = nil
    @collectors.clear
  end

  return unless thread_to_join

  result = thread_to_join.join(2)
  if result.nil?
    Speedshop::Cloudwatch.log_info("Reporter thread did not finish within 2s timeout")
  else
    Speedshop::Cloudwatch.log_info("Reporter thread stopped gracefully")
  end
end