Class: MetrixWire::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/metrixwire/config.rb

Overview

Immutable-ish config holder. Built once by init from explicit options with ENV fallbacks. Nothing here throws — bad input degrades to a sensible default so instrumentation never breaks the host app.

Constant Summary collapse

DEFAULT_ENDPOINT =
"http://localhost:3000/ingest"
DEFAULT_FLUSH_INTERVAL_MS =
5000
DEFAULT_TIMEOUT_MS =
3000
DEFAULT_MAX_BATCH =
20

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Config

Returns a new instance of Config.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/metrixwire/config.rb', line 16

def initialize(opts = {})
  @api_key = to_s(opts.fetch(:api_key, env("METRIXWIRE_KEY")))
  @endpoint = normalize_endpoint(opts.fetch(:endpoint, env("METRIXWIRE_ENDPOINT")) || DEFAULT_ENDPOINT)
  @flush_interval_ms = to_i(opts[:flush_interval_ms], DEFAULT_FLUSH_INTERVAL_MS)
  @timeout_ms = to_i(opts[:timeout_ms], DEFAULT_TIMEOUT_MS)
  @max_batch = to_i(opts[:max_batch], DEFAULT_MAX_BATCH)
  @capture_source = opts.fetch(:capture_source, true) ? true : false

  enabled = opts.key?(:enabled) ? opts[:enabled] : env_enabled
  # No key ⇒ disabled mode (never throw). Matches Node/PHP behavior.
  @enabled = (enabled ? true : false) && !@api_key.empty?
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



13
14
15
# File 'lib/metrixwire/config.rb', line 13

def api_key
  @api_key
end

#capture_sourceObject (readonly)

Returns the value of attribute capture_source.



13
14
15
# File 'lib/metrixwire/config.rb', line 13

def capture_source
  @capture_source
end

#enabledObject (readonly)

Returns the value of attribute enabled.



13
14
15
# File 'lib/metrixwire/config.rb', line 13

def enabled
  @enabled
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



13
14
15
# File 'lib/metrixwire/config.rb', line 13

def endpoint
  @endpoint
end

#flush_interval_msObject (readonly)

Returns the value of attribute flush_interval_ms.



13
14
15
# File 'lib/metrixwire/config.rb', line 13

def flush_interval_ms
  @flush_interval_ms
end

#max_batchObject (readonly)

Returns the value of attribute max_batch.



13
14
15
# File 'lib/metrixwire/config.rb', line 13

def max_batch
  @max_batch
end

#timeout_msObject (readonly)

Returns the value of attribute timeout_ms.



13
14
15
# File 'lib/metrixwire/config.rb', line 13

def timeout_ms
  @timeout_ms
end

Instance Method Details

#normalize_endpoint(url) ⇒ Object

Accept a base URL too: append /ingest when missing.



30
31
32
33
34
35
36
37
38
# File 'lib/metrixwire/config.rb', line 30

def normalize_endpoint(url)
  url = to_s(url)
  url = DEFAULT_ENDPOINT if url.empty?
  url = url.sub(%r{/+\z}, "")
  url = "#{url}/ingest" unless url.end_with?("/ingest")
  url
rescue StandardError
  DEFAULT_ENDPOINT
end