Class: Flare::MetricSubmitter
- Inherits:
-
Object
- Object
- Flare::MetricSubmitter
- Defined in:
- lib/flare/metric_submitter.rb
Overview
Submits metrics to the Flare metrics service via HTTP. Handles gzip compression, retries with exponential backoff, and error handling.
Defined Under Namespace
Classes: ClientError, SubmissionError
Constant Summary collapse
- SCHEMA_VERSION =
"V1"- GZIP_ENCODING =
"gzip"- USER_AGENT =
"Flare Ruby/#{Flare::VERSION}"- DEFAULT_OPEN_TIMEOUT =
Default timeouts (in seconds)
2- DEFAULT_READ_TIMEOUT =
5- DEFAULT_WRITE_TIMEOUT =
5- MAX_RETRIES =
Max retries before giving up
3
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#backoff_policy ⇒ Object
readonly
Returns the value of attribute backoff_policy.
-
#endpoint ⇒ Object
readonly
Returns the value of attribute endpoint.
Instance Method Summary collapse
-
#initialize(endpoint:, api_key:, project: nil, environment: nil, backoff_policy: nil, open_timeout: nil, read_timeout: nil, write_timeout: nil) ⇒ MetricSubmitter
constructor
A new instance of MetricSubmitter.
-
#submit(drained) ⇒ Object
Submit drained metrics to the server.
Constructor Details
#initialize(endpoint:, api_key:, project: nil, environment: nil, backoff_policy: nil, open_timeout: nil, read_timeout: nil, write_timeout: nil) ⇒ MetricSubmitter
Returns a new instance of MetricSubmitter.
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/flare/metric_submitter.rb', line 50 def initialize(endpoint:, api_key:, project: nil, environment: nil, backoff_policy: nil, open_timeout: nil, read_timeout: nil, write_timeout: nil) @endpoint = URI("#{endpoint.to_s.chomp('/')}/api/metrics") @api_key = api_key @project = project || default_project @environment = environment || default_environment @backoff_policy = backoff_policy || BackoffPolicy.new @open_timeout = open_timeout || DEFAULT_OPEN_TIMEOUT @read_timeout = read_timeout || DEFAULT_READ_TIMEOUT @write_timeout = write_timeout || DEFAULT_WRITE_TIMEOUT end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
48 49 50 |
# File 'lib/flare/metric_submitter.rb', line 48 def api_key @api_key end |
#backoff_policy ⇒ Object (readonly)
Returns the value of attribute backoff_policy.
48 49 50 |
# File 'lib/flare/metric_submitter.rb', line 48 def backoff_policy @backoff_policy end |
#endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint.
48 49 50 |
# File 'lib/flare/metric_submitter.rb', line 48 def endpoint @endpoint end |
Instance Method Details
#submit(drained) ⇒ Object
Submit drained metrics to the server. Returns [success_count, error] where error may be nil on success.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/flare/metric_submitter.rb', line 63 def submit(drained) return [0, nil] if drained.empty? request_id = SecureRandom.uuid Flare.log "Submitting #{drained.size} metrics to #{@endpoint} (request_id=#{request_id})" body = build_body(drained, request_id) return [0, nil] if body.nil? @backoff_policy.reset response, error = retry_with_backoff(MAX_RETRIES) { post(body, request_id) } if error Flare.log "Submission failed: #{error.} (request_id=#{request_id})" [0, error] else Flare.log "Submission succeeded: #{response.code} (request_id=#{request_id})" [drained.size, nil] end end |