Class: Tracekit::SDK

Inherits:
Object
  • Object
show all
Defined in:
lib/tracekit/sdk.rb

Overview

Main SDK class for TraceKit Ruby SDK with OpenTelemetry integration Provides distributed tracing, metrics collection, and code monitoring capabilities

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ SDK

Returns a new instance of SDK.



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
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/tracekit/sdk.rb', line 73

def initialize(config)
  @config = config
  @service_name = config.service_name

  puts "Initializing TraceKit SDK v#{Tracekit::VERSION} for service: #{config.service_name}, environment: #{config.environment}"

  # Auto-detect local UI
  local_ui_detector = LocalUIDetector.new(config.local_ui_port)
  if local_endpoint = local_ui_detector.local_ui_endpoint
    puts "Local UI detected at #{local_endpoint}"
  end

  # Resolve endpoints
  traces_endpoint = EndpointResolver.resolve(config.endpoint, "/v1/traces", config.use_ssl)
  metrics_endpoint = EndpointResolver.resolve(config.endpoint, "/v1/metrics", config.use_ssl)
  snapshot_base_url = EndpointResolver.resolve(config.endpoint, "", config.use_ssl)

  # Initialize OpenTelemetry tracer
  setup_tracing(traces_endpoint)

  # Initialize LLM instrumentation (auto-detect providers)
  setup_llm_instrumentation if defined?(Tracekit::LLM)

  # Initialize metrics registry
  @metrics_registry = Metrics::Registry.new(metrics_endpoint, config.api_key, config.service_name)

  # Initialize snapshot client if code monitoring is enabled
  if config.enable_code_monitoring
    @snapshot_client = Snapshots::Client.new(
      config.api_key,
      snapshot_base_url,
      config.service_name,
      config.code_monitoring_poll_interval
    )
    puts "Code monitoring enabled - Snapshot client started"
  end

  puts "TraceKit SDK initialized successfully. Traces: #{traces_endpoint}, Metrics: #{metrics_endpoint}"
end

Class Attribute Details

.instanceObject

Global SDK instance



284
285
286
# File 'lib/tracekit/sdk.rb', line 284

def instance
  @instance
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



71
72
73
# File 'lib/tracekit/sdk.rb', line 71

def config
  @config
end

#service_nameObject (readonly)

Returns the value of attribute service_name.



71
72
73
# File 'lib/tracekit/sdk.rb', line 71

def service_name
  @service_name
end

Class Method Details

.configure(config) ⇒ SDK

Creates and configures the SDK singleton

Parameters:

  • config (Config)

    SDK configuration

Returns:



289
290
291
# File 'lib/tracekit/sdk.rb', line 289

def configure(config)
  @instance = new(config)
end

.currentSDK?

Returns the global SDK instance

Returns:



295
296
297
# File 'lib/tracekit/sdk.rb', line 295

def current
  @instance
end

Instance Method Details

#capture_snapshot(label, variables) ⇒ Object

Captures a snapshot of local variables at the current code location Only active if code monitoring is enabled and there’s an active breakpoint

Parameters:

  • label (String)

    Stable identifier for this snapshot location

  • variables (Hash)

    Variables to capture in the snapshot



141
142
143
144
145
146
# File 'lib/tracekit/sdk.rb', line 141

def capture_snapshot(label, variables)
  return unless @snapshot_client

  caller_location = caller_locations(1, 1).first
  @snapshot_client.capture_snapshot(label, variables, caller_location)
end

#counter(name, tags = {}) ⇒ Metrics::Counter

Creates a Counter metric for tracking monotonically increasing values

Parameters:

  • name (String)

    Metric name (e.g., “http.requests.total”)

  • tags (Hash) (defaults to: {})

    Optional tags for the metric

Returns:



117
118
119
# File 'lib/tracekit/sdk.rb', line 117

def counter(name, tags = {})
  Metrics::Counter.new(name, tags, @metrics_registry)
end

#gauge(name, tags = {}) ⇒ Metrics::Gauge

Creates a Gauge metric for tracking point-in-time values

Parameters:

  • name (String)

    Metric name (e.g., “http.requests.active”)

  • tags (Hash) (defaults to: {})

    Optional tags for the metric

Returns:



125
126
127
# File 'lib/tracekit/sdk.rb', line 125

def gauge(name, tags = {})
  Metrics::Gauge.new(name, tags, @metrics_registry)
end

#histogram(name, tags = {}) ⇒ Metrics::Histogram

Creates a Histogram metric for tracking value distributions

Parameters:

  • name (String)

    Metric name (e.g., “http.request.duration”)

  • tags (Hash) (defaults to: {})

    Optional tags for the metric

Returns:



133
134
135
# File 'lib/tracekit/sdk.rb', line 133

def histogram(name, tags = {})
  Metrics::Histogram.new(name, tags, @metrics_registry)
end

#shutdownObject

Shuts down the SDK and flushes any pending data



149
150
151
152
153
154
# File 'lib/tracekit/sdk.rb', line 149

def shutdown
  @metrics_registry&.shutdown
  @snapshot_client&.shutdown
  OpenTelemetry.tracer_provider&.shutdown
  puts "TraceKit SDK shutdown complete"
end