Class: Twingly::Metrics::GrafanaCloudReporter
- Inherits:
-
Object
- Object
- Twingly::Metrics::GrafanaCloudReporter
- 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
-
#endpoint ⇒ Object
readonly
Returns the value of attribute endpoint.
-
#interval ⇒ Object
readonly
Returns the value of attribute interval.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#registry ⇒ Object
readonly
Returns the value of attribute registry.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
- #flush ⇒ Object
-
#initialize(endpoint: nil, user: nil, token: nil, **options) ⇒ GrafanaCloudReporter
constructor
rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity.
- #restart ⇒ Object
-
#start ⇒ Object
rubocop:disable Metrics/MethodLength.
- #stop ⇒ Object
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, **) # 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 = [:prefix] @source = [:source] || default_source @logger = [:logger] log_missing_credentials unless credentials_provided? @registry = [:registry] || Twingly::Metrics::Registry.default @interval = [:interval] || 60 @on_error = [:on_error] || proc { |ex| @logger&.error(ex) } @timeout = [:timeout] || 10 @next_time = Time.now.to_f @previous_request_values = Hash.new { |h, k| h[k] = 0 } @percentiles = [:percentiles] || [0.95, 0.999] @flush_metrics_mutex = Mutex.new @running = false @reporter_thread = nil end |
Instance Attribute Details
#endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint.
37 38 39 |
# File 'lib/twingly/metrics/grafana_cloud_reporter.rb', line 37 def endpoint @endpoint end |
#interval ⇒ Object (readonly)
Returns the value of attribute interval.
44 45 46 |
# File 'lib/twingly/metrics/grafana_cloud_reporter.rb', line 44 def interval @interval end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
42 43 44 |
# File 'lib/twingly/metrics/grafana_cloud_reporter.rb', line 42 def logger @logger end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
40 41 42 |
# File 'lib/twingly/metrics/grafana_cloud_reporter.rb', line 40 def prefix @prefix end |
#registry ⇒ Object (readonly)
Returns the value of attribute registry.
43 44 45 |
# File 'lib/twingly/metrics/grafana_cloud_reporter.rb', line 43 def registry @registry end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
41 42 43 |
# File 'lib/twingly/metrics/grafana_cloud_reporter.rb', line 41 def source @source end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
45 46 47 |
# File 'lib/twingly/metrics/grafana_cloud_reporter.rb', line 45 def timeout @timeout end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
39 40 41 |
# File 'lib/twingly/metrics/grafana_cloud_reporter.rb', line 39 def token @token end |
#user ⇒ Object (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
#flush ⇒ Object
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 |
#restart ⇒ Object
101 102 103 104 |
# File 'lib/twingly/metrics/grafana_cloud_reporter.rb', line 101 def restart stop start end |
#start ⇒ Object
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 |
#stop ⇒ Object
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 |