Class: Tracekit::Metrics::Exporter

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

Overview

Exports metrics to TraceKit in OTLP (OpenTelemetry Protocol) format

Instance Method Summary collapse

Constructor Details

#initialize(endpoint, api_key, service_name) ⇒ Exporter

Returns a new instance of Exporter.



10
11
12
13
14
# File 'lib/tracekit/metrics/exporter.rb', line 10

def initialize(endpoint, api_key, service_name)
  @endpoint = endpoint
  @api_key = api_key
  @service_name = service_name
end

Instance Method Details

#export(data_points) ⇒ Object

Exports a batch of metric data points



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/tracekit/metrics/exporter.rb', line 17

def export(data_points)
  return if data_points.empty?

  payload = to_otlp(data_points)

  uri = URI(@endpoint)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == "https"
  http.read_timeout = 10

  request = Net::HTTP::Post.new(uri.path, {
    "Content-Type" => "application/json",
    "X-API-Key" => @api_key,
    "User-Agent" => "tracekit-ruby-sdk/#{Tracekit::VERSION}"
  })
  request.body = JSON.generate(payload)

  response = http.request(request)

  unless response.is_a?(Net::HTTPSuccess)
    warn "Metrics export failed: HTTP #{response.code}"
  end
rescue => e
  warn "Metrics export error: #{e.message}"
end