Class: LogBrew::Rails::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/logbrew/rails_integration.rb

Overview

Immutable, environment-derived settings for the automatic Rails adapter.

Constant Summary collapse

DEFAULT_ENDPOINT =
LogBrew::HttpTransport::DEFAULT_ENDPOINT
DEFAULT_REQUEST_TIMEOUT_MS =
10_000
DEFAULT_FLUSH_INTERVAL_MS =
5_000
DEFAULT_FLUSH_THRESHOLD =
100
MAX_LABEL_BYTES =
255
MAX_ENDPOINT_BYTES =
2_048
LOOPBACK_HOSTS =
%w[localhost 127.0.0.1 ::1 [::1]].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enabled:, api_key:, service_name:, app_environment:, rails_version:, release:, endpoint:, request_timeout:, flush_interval:, flush_threshold:, capture_exception_messages:, include_exception_backtrace:) ⇒ Configuration

Returns a new instance of Configuration.



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/logbrew/rails_integration.rb', line 208

def initialize(
  enabled:,
  api_key:,
  service_name:,
  app_environment:,
  rails_version:,
  release:,
  endpoint:,
  request_timeout:,
  flush_interval:,
  flush_threshold:,
  capture_exception_messages:,
  include_exception_backtrace:
)
  @enabled = enabled
  @api_key = api_key&.dup&.freeze
  @service_name = service_name
  @app_environment = app_environment
  @rails_version = rails_version
  @release = release
  @endpoint = endpoint
  @request_timeout = request_timeout
  @flush_interval = flush_interval
  @flush_threshold = flush_threshold
  @capture_exception_messages = capture_exception_messages
  @include_exception_backtrace = include_exception_backtrace
  freeze
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



18
19
20
# File 'lib/logbrew/rails_integration.rb', line 18

def api_key
  @api_key
end

#app_environmentObject (readonly)

Returns the value of attribute app_environment.



18
19
20
# File 'lib/logbrew/rails_integration.rb', line 18

def app_environment
  @app_environment
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



18
19
20
# File 'lib/logbrew/rails_integration.rb', line 18

def endpoint
  @endpoint
end

#flush_intervalObject (readonly)

Returns the value of attribute flush_interval.



18
19
20
# File 'lib/logbrew/rails_integration.rb', line 18

def flush_interval
  @flush_interval
end

#flush_thresholdObject (readonly)

Returns the value of attribute flush_threshold.



18
19
20
# File 'lib/logbrew/rails_integration.rb', line 18

def flush_threshold
  @flush_threshold
end

#rails_versionObject (readonly)

Returns the value of attribute rails_version.



18
19
20
# File 'lib/logbrew/rails_integration.rb', line 18

def rails_version
  @rails_version
end

#releaseObject (readonly)

Returns the value of attribute release.



18
19
20
# File 'lib/logbrew/rails_integration.rb', line 18

def release
  @release
end

#request_timeoutObject (readonly)

Returns the value of attribute request_timeout.



18
19
20
# File 'lib/logbrew/rails_integration.rb', line 18

def request_timeout
  @request_timeout
end

#service_nameObject (readonly)

Returns the value of attribute service_name.



18
19
20
# File 'lib/logbrew/rails_integration.rb', line 18

def service_name
  @service_name
end

Class Method Details

.disabled(application_name, rails_environment, rails_version) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/logbrew/rails_integration.rb', line 100

def self.disabled(application_name, rails_environment, rails_version)
  new(
    enabled: false,
    api_key: nil,
    service_name: bounded_label(application_name, "Rails application name"),
    app_environment: bounded_label(rails_environment, "Rails environment"),
    rails_version: bounded_label(rails_version, "Rails version"),
    release: nil,
    endpoint: DEFAULT_ENDPOINT,
    request_timeout: DEFAULT_REQUEST_TIMEOUT_MS / 1_000.0,
    flush_interval: DEFAULT_FLUSH_INTERVAL_MS / 1_000.0,
    flush_threshold: DEFAULT_FLUSH_THRESHOLD,
    capture_exception_messages: false,
    include_exception_backtrace: false
  )
end

.from_environment(environment, application_name:, rails_environment:, rails_version:) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/logbrew/rails_integration.rb', line 30

def self.from_environment(environment, application_name:, rails_environment:, rails_version:)
  unless environment.respond_to?(:[]) && environment.respond_to?(:key?)
    raise SdkError.new("configuration_error", "Rails environment must provide key lookup")
  end

  enabled_setting = boolean_value(environment, "LOGBREW_ENABLED", nil)
  return disabled(application_name, rails_environment, rails_version) if enabled_setting == false

  canonical_key = server_key_value(environment["LOGBREW_SERVER_API_KEY"])
  if canonical_key.nil?
    legacy_present = %w[LOGBREW_API_KEY LOGBREW_INGEST_KEY].any? do |name|
      !optional_text(environment[name]).nil?
    end
    if enabled_setting == true || legacy_present || environment.key?("LOGBREW_SERVER_API_KEY")
      raise SdkError.new(
        "configuration_error",
        "set LOGBREW_SERVER_API_KEY to a non-empty server API key, or set LOGBREW_ENABLED=false"
      )
    end
    return disabled(application_name, rails_environment, rails_version)
  end

  new(
    enabled: true,
    api_key: canonical_key,
    service_name: bounded_label(
      optional_text(environment["LOGBREW_SERVICE_NAME"]) || application_name,
      "LOGBREW_SERVICE_NAME"
    ),
    app_environment: bounded_label(
      optional_text(environment["LOGBREW_ENVIRONMENT"]) || rails_environment,
      "LOGBREW_ENVIRONMENT"
    ),
    rails_version: bounded_label(rails_version, "Rails version"),
    release: optional_bounded_label(environment["LOGBREW_RELEASE"], "LOGBREW_RELEASE"),
    endpoint: endpoint_value(environment["LOGBREW_ENDPOINT"]),
    request_timeout: integer_value(
      environment,
      "LOGBREW_REQUEST_TIMEOUT_MS",
      DEFAULT_REQUEST_TIMEOUT_MS,
      1,
      600_000
    ) / 1_000.0,
    flush_interval: integer_value(
      environment,
      "LOGBREW_FLUSH_INTERVAL_MS",
      DEFAULT_FLUSH_INTERVAL_MS,
      10,
      3_600_000
    ) / 1_000.0,
    flush_threshold: integer_value(
      environment,
      "LOGBREW_FLUSH_THRESHOLD",
      DEFAULT_FLUSH_THRESHOLD,
      1,
      1_000
    ),
    capture_exception_messages: boolean_value(
      environment,
      "LOGBREW_CAPTURE_EXCEPTION_MESSAGES",
      false
    ),
    include_exception_backtrace: boolean_value(
      environment,
      "LOGBREW_INCLUDE_EXCEPTION_BACKTRACE",
      false
    )
  )
end

Instance Method Details

#capture_exception_messages?Boolean

Returns:

  • (Boolean)


241
242
243
# File 'lib/logbrew/rails_integration.rb', line 241

def capture_exception_messages?
  @capture_exception_messages
end

#enabled?Boolean

Returns:

  • (Boolean)


237
238
239
# File 'lib/logbrew/rails_integration.rb', line 237

def enabled?
  @enabled
end

#include_exception_backtrace?Boolean

Returns:

  • (Boolean)


245
246
247
# File 'lib/logbrew/rails_integration.rb', line 245

def include_exception_backtrace?
  @include_exception_backtrace
end