Module: SecApi::MetricsCollector
- Extended by:
- MetricsCollector
- Included in:
- MetricsCollector
- Defined in:
- lib/sec_api/metrics_collector.rb
Overview
Provides centralized metric recording for SEC API operations.
This module standardizes metric names and tags across all integrations. Use directly with your metrics backend, or configure via metrics_backend for automatic metrics collection.
All metric methods are safe to call - they will not raise exceptions even if the backend is nil, misconfigured, or raises errors. This ensures that metrics never break production operations.
Constant Summary collapse
- REQUESTS_TOTAL =
Metric name for total requests made (counter).
"sec_api.requests.total"- REQUESTS_SUCCESS =
Metric name for successful requests (counter, status < 400).
"sec_api.requests.success"- REQUESTS_ERROR =
Metric name for error requests (counter, status >= 400).
"sec_api.requests.error"- REQUESTS_DURATION =
Metric name for request duration (histogram, milliseconds).
"sec_api.requests.duration_ms"- RETRIES_TOTAL =
Metric name for retry attempts (counter).
"sec_api.retries.total"- RETRIES_EXHAUSTED =
Metric name for exhausted retries (counter).
"sec_api.retries.exhausted"- RATE_LIMIT_HIT =
Metric name for rate limit hits (counter, 429 responses).
"sec_api.rate_limit.hit"- RATE_LIMIT_THROTTLE =
Metric name for proactive throttling events (counter).
"sec_api.rate_limit.throttle"- STREAM_FILINGS =
Metric name for streaming filings received (counter).
"sec_api.stream.filings"- STREAM_LATENCY =
Metric name for streaming delivery latency (histogram, milliseconds).
"sec_api.stream.latency_ms"- STREAM_RECONNECTS =
Metric name for stream reconnection events (counter).
"sec_api.stream.reconnects"- JOURNEY_STAGE_DURATION =
Metric name for filing journey stage duration (histogram, milliseconds).
"sec_api.filing.journey.stage_ms"- JOURNEY_TOTAL_DURATION =
Metric name for total filing journey duration (histogram, milliseconds).
"sec_api.filing.journey.total_ms"
Instance Method Summary collapse
-
#record_error(backend, error_class:, method:) ⇒ void
Records a final error (all retries exhausted).
-
#record_filing(backend, latency_ms:, form_type:) ⇒ void
Records a streaming filing received.
-
#record_journey_stage(backend, stage:, duration_ms:, form_type: nil) ⇒ void
Records a filing journey stage completion.
-
#record_journey_total(backend, total_ms:, form_type: nil, success: true) ⇒ void
Records total filing journey duration.
-
#record_rate_limit(backend, retry_after: nil) ⇒ void
Records a rate limit (429) response.
-
#record_reconnect(backend, attempt_count:, downtime_seconds:) ⇒ void
Records a stream reconnection.
-
#record_response(backend, status:, duration_ms:, method:) ⇒ void
Records a successful or failed response.
-
#record_retry(backend, attempt:, error_class:) ⇒ void
Records a retry attempt.
-
#record_throttle(backend, remaining:, delay:) ⇒ void
Records proactive throttling.
Instance Method Details
#record_error(backend, error_class:, method:) ⇒ void
This method returns an undefined value.
Records a final error (all retries exhausted).
183 184 185 186 |
# File 'lib/sec_api/metrics_collector.rb', line 183 def record_error(backend, error_class:, method:) = {error_class: error_class, method: method.to_s.upcase} increment(backend, RETRIES_EXHAUSTED, tags: ) end |
#record_filing(backend, latency_ms:, form_type:) ⇒ void
This method returns an undefined value.
Records a streaming filing received.
231 232 233 234 235 |
# File 'lib/sec_api/metrics_collector.rb', line 231 def record_filing(backend, latency_ms:, form_type:) = {form_type: form_type} increment(backend, STREAM_FILINGS, tags: ) histogram(backend, STREAM_LATENCY, latency_ms, tags: ) end |
#record_journey_stage(backend, stage:, duration_ms:, form_type: nil) ⇒ void
This method returns an undefined value.
Records a filing journey stage completion.
Use this to track duration of individual pipeline stages (detected, queried, extracted, processed). Combined with FilingJourney logging, this provides both detailed logs and aggregated metrics.
274 275 276 277 278 |
# File 'lib/sec_api/metrics_collector.rb', line 274 def record_journey_stage(backend, stage:, duration_ms:, form_type: nil) = {stage: stage} [:form_type] = form_type if form_type histogram(backend, JOURNEY_STAGE_DURATION, duration_ms, tags: ) end |
#record_journey_total(backend, total_ms:, form_type: nil, success: true) ⇒ void
This method returns an undefined value.
Records total filing journey duration.
Use this to track end-to-end pipeline latency from filing detection through processing completion. Useful for monitoring SLAs and identifying slow pipelines.
308 309 310 311 312 |
# File 'lib/sec_api/metrics_collector.rb', line 308 def record_journey_total(backend, total_ms:, form_type: nil, success: true) = {success: success.to_s} [:form_type] = form_type if form_type histogram(backend, JOURNEY_TOTAL_DURATION, total_ms, tags: ) end |
#record_rate_limit(backend, retry_after: nil) ⇒ void
This method returns an undefined value.
Records a rate limit (429) response.
197 198 199 200 |
# File 'lib/sec_api/metrics_collector.rb', line 197 def record_rate_limit(backend, retry_after: nil) increment(backend, RATE_LIMIT_HIT) gauge(backend, "sec_api.rate_limit.retry_after", retry_after) if retry_after end |
#record_reconnect(backend, attempt_count:, downtime_seconds:) ⇒ void
This method returns an undefined value.
Records a stream reconnection.
247 248 249 250 251 |
# File 'lib/sec_api/metrics_collector.rb', line 247 def record_reconnect(backend, attempt_count:, downtime_seconds:) increment(backend, STREAM_RECONNECTS) gauge(backend, "sec_api.stream.reconnect_attempts", attempt_count) histogram(backend, "sec_api.stream.downtime_ms", (downtime_seconds * 1000).round) end |
#record_response(backend, status:, duration_ms:, method:) ⇒ void
This method returns an undefined value.
Records a successful or failed response.
Increments request counters and records duration histogram. Status codes < 400 are considered successful, >= 400 are errors.
144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/sec_api/metrics_collector.rb', line 144 def record_response(backend, status:, duration_ms:, method:) = {method: method.to_s.upcase, status: status.to_s} increment(backend, REQUESTS_TOTAL, tags: ) if status < 400 increment(backend, REQUESTS_SUCCESS, tags: ) else increment(backend, REQUESTS_ERROR, tags: ) end histogram(backend, REQUESTS_DURATION, duration_ms, tags: ) end |
#record_retry(backend, attempt:, error_class:) ⇒ void
This method returns an undefined value.
Records a retry attempt.
168 169 170 171 |
# File 'lib/sec_api/metrics_collector.rb', line 168 def record_retry(backend, attempt:, error_class:) = {attempt: attempt.to_s, error_class: error_class} increment(backend, RETRIES_TOTAL, tags: ) end |
#record_throttle(backend, remaining:, delay:) ⇒ void
This method returns an undefined value.
Records proactive throttling.
Called when the rate limiter proactively delays a request to avoid hitting the rate limit.
215 216 217 218 219 |
# File 'lib/sec_api/metrics_collector.rb', line 215 def record_throttle(backend, remaining:, delay:) increment(backend, RATE_LIMIT_THROTTLE) gauge(backend, "sec_api.rate_limit.remaining", remaining) histogram(backend, "sec_api.rate_limit.delay_ms", (delay * 1000).round) end |