Class: Twingly::Metrics::GrafanaCloudReporter

Inherits:
Object
  • Object
show all
Defined in:
lib/twingly/metrics/grafana_cloud_reporter.rb

Overview

rubocop:disable Metrics/ClassLength

Defined Under Namespace

Classes: Error, RequestFailedError, UnsupportedMetricError

Constant Summary collapse

METRIC_TYPE_TO_BUILD_METHOD =
{
  Twingly::Metrics::Meter   => :build_meter_metric,
  Twingly::Metrics::Counter => :build_counter_metric,
  Twingly::Metrics::Gauge   => :build_gauge_metric,
  Twingly::Metrics::Timer   => :build_timer_metric,
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint: nil, user: nil, token: nil, **options) ⇒ GrafanaCloudReporter

rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/twingly/metrics/grafana_cloud_reporter.rb', line 47

def initialize(endpoint: nil, user: nil, token: nil, **options) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  @endpoint = endpoint || ENV.fetch("GRAFANA_METRICS_ENDPOINT") { nil }
  @user     = user     || ENV.fetch("GRAFANA_METRICS_USER")     { nil }
  @token    = token    || ENV.fetch("GRAFANA_METRICS_TOKEN")    { nil }

  @prefix = options[:prefix]
  @source = options[:source] || default_source
  @logger = options[:logger]

  log_missing_credentials unless credentials_provided?

  @registry = options[:registry] || Twingly::Metrics::Registry.default
  @interval = options[:interval] || 60
  @on_error = options[:on_error] || proc { |ex| @logger&.error(ex) }
  @timeout  = options[:timeout]  || 10

  @next_time = Time.now.to_f

  @previous_request_values = Hash.new { |h, k| h[k] = 0 }

  @percentiles = options[:percentiles] || [0.95, 0.999]

  @flush_metrics_mutex = Mutex.new
  @running             = false
  @reporter_thread     = nil
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



37
38
39
# File 'lib/twingly/metrics/grafana_cloud_reporter.rb', line 37

def endpoint
  @endpoint
end

#intervalObject (readonly)

Returns the value of attribute interval.



44
45
46
# File 'lib/twingly/metrics/grafana_cloud_reporter.rb', line 44

def interval
  @interval
end

#loggerObject (readonly)

Returns the value of attribute logger.



42
43
44
# File 'lib/twingly/metrics/grafana_cloud_reporter.rb', line 42

def logger
  @logger
end

#prefixObject (readonly)

Returns the value of attribute prefix.



40
41
42
# File 'lib/twingly/metrics/grafana_cloud_reporter.rb', line 40

def prefix
  @prefix
end

#registryObject (readonly)

Returns the value of attribute registry.



43
44
45
# File 'lib/twingly/metrics/grafana_cloud_reporter.rb', line 43

def registry
  @registry
end

#sourceObject (readonly)

Returns the value of attribute source.



41
42
43
# File 'lib/twingly/metrics/grafana_cloud_reporter.rb', line 41

def source
  @source
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



45
46
47
# File 'lib/twingly/metrics/grafana_cloud_reporter.rb', line 45

def timeout
  @timeout
end

#tokenObject (readonly)

Returns the value of attribute token.



39
40
41
# File 'lib/twingly/metrics/grafana_cloud_reporter.rb', line 39

def token
  @token
end

#userObject (readonly)

Returns the value of attribute user.



38
39
40
# File 'lib/twingly/metrics/grafana_cloud_reporter.rb', line 38

def user
  @user
end

Instance Method Details

#flushObject



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/twingly/metrics/grafana_cloud_reporter.rb', line 106

def flush
  return unless credentials_provided?

  @flush_metrics_mutex.synchronize do
    request_data = build_metrics_data_for_request

    retry_failing_request do
      make_request(request_data)
    end
  end
rescue StandardError => e
  @on_error.call(e)
end

#restartObject



101
102
103
104
# File 'lib/twingly/metrics/grafana_cloud_reporter.rb', line 101

def restart
  stop
  start
end

#startObject

rubocop:disable Metrics/MethodLength



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/twingly/metrics/grafana_cloud_reporter.rb', line 74

def start # rubocop:disable Metrics/MethodLength
  return unless credentials_provided?
  return if @reporter_thread&.alive?

  @running = true
  @reporter_thread = Thread.new do
    while @running
      sleep_until_next_flush

      Thread.new do
        flush
      end
    end
  end
end

#stopObject



90
91
92
93
94
95
96
97
98
99
# File 'lib/twingly/metrics/grafana_cloud_reporter.rb', line 90

def stop
  return unless credentials_provided?

  @running = false

  return unless @reporter_thread

  @reporter_thread.join
  @reporter_thread = nil
end