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_commit_shaObject

Resolves the current commit SHA with zero developer friction. Checks common CI/CD environment variables first, then falls back to running ‘git rev-parse HEAD` in the project directory.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/tracelit/configuration.rb', line 44

def resolved_commit_sha
  return @resolved_commit_sha if defined?(@resolved_commit_sha)

  sha = ENV["COMMIT_SHA"] ||
        ENV["GIT_COMMIT_SHA"] ||
        ENV["GIT_COMMIT"] ||
        ENV["GITHUB_SHA"] ||
        ENV["HEROKU_SLUG_COMMIT"] ||
        ENV["SOURCE_VERSION"] ||        # Heroku alt
        ENV["RENDER_GIT_COMMIT"] ||     # Render
        ENV["FLY_APP_VERSION"] ||       # Fly.io
        ENV["RAILWAY_GIT_COMMIT_SHA"]   # Railway

  if sha.nil? || sha.empty?
    begin
      sha = `git rev-parse HEAD 2>/dev/null`.strip
      sha = nil if sha.empty?
    rescue StandardError
      sha = nil
    end
  end

  @resolved_commit_sha = sha
end

#resolved_service_nameObject

Infer service name from Rails application if not explicitly set.



97
98
99
100
101
# File 'lib/tracelit/configuration.rb', line 97

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

#sanitized_resource_attributesObject



110
111
112
113
114
115
# File 'lib/tracelit/configuration.rb', line 110

def sanitized_resource_attributes
  resource_attributes.each_with_object({}) do |(k, v), h|
    next unless PRIMITIVE_TYPES.any? { |t| v.is_a?(t) }
    h[k.to_s] = v
  end
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)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/tracelit/configuration.rb', line 72

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.



92
93
94
# File 'lib/tracelit/configuration.rb', line 92

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