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
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
|