Class: Tracelit::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/tracelit/configuration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



31
32
33
34
35
36
37
38
39
# File 'lib/tracelit/configuration.rb', line 31

def initialize
  @api_key             = ENV["TRACELIT_API_KEY"]
  @service_name        = ENV["TRACELIT_SERVICE_NAME"]
  @environment         = ENV["TRACELIT_ENVIRONMENT"] || "production"
  @endpoint            = ENV["TRACELIT_ENDPOINT"]    || "https://ingest.tracelit.app"
  @sample_rate         = (ENV["TRACELIT_SAMPLE_RATE"] || "1.0").to_f
  @enabled             = ENV["TRACELIT_ENABLED"] != "false"
  @resource_attributes = {}
end

Instance Attribute Details

#api_keyObject

Required



6
7
8
# File 'lib/tracelit/configuration.rb', line 6

def api_key
  @api_key
end

#enabledObject

Set false to disable all telemetry without removing the gem. Useful for test environments.



25
26
27
# File 'lib/tracelit/configuration.rb', line 25

def enabled
  @enabled
end

#endpointObject

Full URL of the Tracelit ingest endpoint. Override only if self-hosting.



17
18
19
# File 'lib/tracelit/configuration.rb', line 17

def endpoint
  @endpoint
end

#environmentObject

Environment tag — production, staging, development, etc.



13
14
15
# File 'lib/tracelit/configuration.rb', line 13

def environment
  @environment
end

#resource_attributesObject

Additional resource attributes appended to every span and log. Hash of string keys and string values.



29
30
31
# File 'lib/tracelit/configuration.rb', line 29

def resource_attributes
  @resource_attributes
end

#sample_rateObject

Head-based sampling rate (0.0–1.0). Default: 1.0 (keep all traces). Set to 0.1 to keep 10% of traces. Errors are always kept regardless.



21
22
23
# File 'lib/tracelit/configuration.rb', line 21

def sample_rate
  @sample_rate
end

#service_nameObject

The name of this service as it will appear in Tracelit. Defaults to the Rails app name if Rails is present.



10
11
12
# File 'lib/tracelit/configuration.rb', line 10

def service_name
  @service_name
end

Instance Method Details

#resolved_service_nameObject

Infer service name from Rails application if not explicitly set.



69
70
71
72
73
# File 'lib/tracelit/configuration.rb', line 69

def resolved_service_name
  return service_name if service_name && !service_name.empty?
  return ::Rails.application.class.module_parent_name.underscore if defined?(::Rails)
  "unknown-service"
end

#valid?Boolean

Returns an array of human-readable error strings. Empty array means the configuration is valid. Never raises — callers decide whether to warn or abort.

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/tracelit/configuration.rb', line 44

def valid?
  errors = []
  errors << "api_key is required" if api_key.nil? || api_key.to_s.empty?

  # Fix 3: check resolved_service_name so Rails apps that rely on automatic
  # name inference (module_parent_name) are not incorrectly flagged.
  if resolved_service_name == "unknown-service"
    errors << "service_name is required (set config.service_name or TRACELIT_SERVICE_NAME)"
  end

  unless sample_rate.between?(0.0, 1.0)
    errors << "sample_rate must be between 0.0 and 1.0 (got #{sample_rate})"
  end

  errors
end

#validate!Object

Kept for backwards compatibility. Previously raised ArgumentError; now a no-op because an observability SDK must never crash the host app. Use valid? to check for configuration errors programmatically.



64
65
66
# File 'lib/tracelit/configuration.rb', line 64

def validate!
  # no-op — see valid? for soft validation
end