Class: GitlabQuality::TestTooling::TestMetricsExporter::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab_quality/test_tooling/test_metrics_exporter/client.rb

Constant Summary collapse

ResponseError =
Class.new(StandardError)
TESTS_PATH =
"/api/v1/tests"
MAX_BATCH_SIZE =

Observer enforces a per-request cap; batch above this size to avoid silent failures.

10_000

Instance Method Summary collapse

Constructor Details

#initialize(url:, token:) ⇒ Client

Returns a new instance of Client.



16
17
18
19
# File 'lib/gitlab_quality/test_tooling/test_metrics_exporter/client.rb', line 16

def initialize(url:, token:)
  @url = url
  @token = token
end

Instance Method Details

#post_tests(tests) ⇒ Boolean

POST array of test metric records to the observer service. Wraps each batch as { “tests” => […] } and splits oversized payloads into chunks of at most MAX_BATCH_SIZE records.

Parameters:

  • tests (Array<Hash>)

Returns:

  • (Boolean)

    true when every batch succeeds (or input is empty)

Raises:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/gitlab_quality/test_tooling/test_metrics_exporter/client.rb', line 28

def post_tests(tests)
  tests.each_slice(MAX_BATCH_SIZE) do |batch|
    response = HTTParty.post(
      "#{url.to_s.chomp('/')}#{TESTS_PATH}",
      body: { tests: batch }.to_json,
      headers: {
        "X-Gitlab-Token" => token,
        "Content-Type" => "application/json"
      }
    )
    raise ResponseError, "Observer request failed with status #{response.code}: #{response.body}" unless response.success?
  end

  true
end