Module: Apidepth::CLI::TestCmd

Defined in:
lib/apidepth/cli/test_cmd.rb

Defined Under Namespace

Classes: TestError

Constant Summary collapse

DEFAULT_COLLECTOR_URL =
"https://collector.apidepth.io".freeze
TIMEOUT_SECONDS =
5

Class Method Summary collapse

Class Method Details

._load_config(_argv) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/apidepth/cli/test_cmd.rb', line 53

def self._load_config(_argv)
  # Try the SDK configuration first, fall back to environment variable
  begin
    require "apidepth"
    cfg = Apidepth.configuration
    api_key = cfg.api_key || ENV.fetch("APIDEPTH_API_KEY", nil)
    collector_url = cfg.collector_url || ENV.fetch("APIDEPTH_COLLECTOR_URL", nil)
  rescue StandardError
    api_key = ENV.fetch("APIDEPTH_API_KEY", nil)
    collector_url = ENV.fetch("APIDEPTH_COLLECTOR_URL", nil)
  end
  [api_key, collector_url]
end

._send_test_event(api_key, base_url) ⇒ Object



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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/apidepth/cli/test_cmd.rb', line 67

def self._send_test_event(api_key, base_url)
  uri = URI.parse("#{base_url}/v1/events")
  payload = {
    batch: [
      {
        vendor: "apidepth-test",
        endpoint: "/test",
        method: "GET",
        status: 200,
        outcome: "success",
        duration_ms: 1,
        cold_start: false,
        env: "test",
        ts: (Time.now.to_f * 1000).to_i,
        test: true
      }
    ],
    sdk: { name: "apidepth-ruby", version: Apidepth::VERSION }
  }.to_json

  start = Process.clock_gettime(Process::CLOCK_MONOTONIC)

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == "https"
  http.read_timeout = TIMEOUT_SECONDS
  http.open_timeout = TIMEOUT_SECONDS

  request = Net::HTTP::Post.new(uri.path.empty? ? "/" : uri.path)
  request["Content-Type"]  = "application/json"
  request["Authorization"] = "Bearer #{api_key}"
  request.body = payload

  # Bypass our own instrumentation
  Thread.current[:apidepth_skip] = true
  response = http.request(request)
  Thread.current[:apidepth_skip] = false

  case response.code.to_i
  when 200, 201, 204
    ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - start) * 1000).round
  when 401, 403
    raise TestError.new(
      "API key not recognised (HTTP #{response.code}).",
      hint: "Check the key in your initializer matches your dashboard at https://apidepth.io/dashboard/api-keys"
    )
  else
    raise TestError.new(
      "Collector returned HTTP #{response.code}.",
      hint: "Check https://status.apidepth.io for service status."
    )
  end
rescue Net::OpenTimeout, Net::ReadTimeout
  raise TestError.new(
    "No response after #{TIMEOUT_SECONDS} seconds.",
    hint: "Check for a firewall blocking outbound port 443."
  )
rescue OpenSSL::SSL::SSLError => e
  raise TestError.new(
    "SSL certificate verification failed: #{e.message}",
    hint: "Check your Ruby SSL configuration."
  )
rescue Errno::ECONNREFUSED, SocketError => e
  raise TestError.new(
    "Could not reach #{uri.host}: #{e.message}",
    hint: "Check outbound HTTPS (port 443) is allowed from this environment."
  )
end

.run(argv = ARGV) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/apidepth/cli/test_cmd.rb', line 20

def self.run(argv = ARGV)
  api_key, collector_url = _load_config(argv)

  unless api_key
    warn "No API key configured."
    warn "Run `bundle exec apidepth setup` or set APIDEPTH_API_KEY."
    exit 1
  end

  base_url = (collector_url || DEFAULT_COLLECTOR_URL).chomp("/")
  $stdout.print "Sending test event to collector... "

  begin
    elapsed = _send_test_event(api_key, base_url)
    $stdout.puts "✓ received in #{elapsed}ms"
    $stdout.puts "Visit your dashboard: https://apidepth.io/dashboard"
  rescue TestError => e
    $stdout.puts ""
    warn "\n#{e.message}"
    warn e.hint if e.hint
    exit 1
  end
end