From fed5e6794f45329658351fb7ff3c60f408d604f5 Mon Sep 17 00:00:00 2001 From: Andrey Date: Mon, 29 Jul 2024 16:56:12 +0200 Subject: [PATCH] some of the skippable HTTP client metrics, fix a lot of typing issues --- lib/datadog/ci/git/upload_packfile.rb | 2 +- lib/datadog/ci/test_optimisation/skippable.rb | 12 +++++++ lib/datadog/ci/transport/adapters/net.rb | 4 +++ sig/datadog/ci/git/search_commits.rbs | 2 +- .../ci/test_optimisation/skippable.rbs | 4 +-- sig/datadog/ci/transport/adapters/net.rbs | 2 ++ sig/datadog/ci/transport/http.rbs | 8 ----- .../ci/transport/remote_settings_api.rbs | 4 +-- .../ci/test_optimisation/skippable_spec.rb | 36 +++++++++++++++---- spec/datadog/ci/transport/http_spec.rb | 1 + 10 files changed, 55 insertions(+), 20 deletions(-) diff --git a/lib/datadog/ci/git/upload_packfile.rb b/lib/datadog/ci/git/upload_packfile.rb index 1e05afc7..7bacdd7c 100644 --- a/lib/datadog/ci/git/upload_packfile.rb +++ b/lib/datadog/ci/git/upload_packfile.rb @@ -45,7 +45,7 @@ def call(filepath:) compressed: http_response.request_compressed ) Utils::Telemetry.distribution(Ext::Telemetry::METRIC_GIT_REQUESTS_OBJECT_PACK_MS, http_response.duration_ms) - Utils::Telemetry.distribution(Ext::Telemetry::METRIC_GIT_REQUESTS_OBJECT_PACK_BYTES, http_response.request_size) + Utils::Telemetry.distribution(Ext::Telemetry::METRIC_GIT_REQUESTS_OBJECT_PACK_BYTES, http_response.request_size.to_f) unless http_response.ok? Transport::Telemetry.api_requests_errors( diff --git a/lib/datadog/ci/test_optimisation/skippable.rb b/lib/datadog/ci/test_optimisation/skippable.rb index 8cd4b660..18e7a001 100644 --- a/lib/datadog/ci/test_optimisation/skippable.rb +++ b/lib/datadog/ci/test_optimisation/skippable.rb @@ -75,6 +75,18 @@ def fetch_skippable_tests(test_session) payload: request_payload ) + Transport::Telemetry.api_requests( + Ext::Telemetry::METRIC_ITR_SKIPPABLE_TESTS_REQUEST, + 1, + compressed: http_response.request_compressed + ) + Utils::Telemetry.distribution(Ext::Telemetry::METRIC_ITR_SKIPPABLE_TESTS_REQUEST_MS, http_response.duration_ms) + Utils::Telemetry.distribution( + Ext::Telemetry::METRIC_ITR_SKIPPABLE_TESTS_RESPONSE_BYTES, + http_response.response_size.to_f, + {Ext::Telemetry::TAG_RESPONSE_COMPRESSED => http_response.gzipped_content?.to_s} + ) + Response.new(http_response) end diff --git a/lib/datadog/ci/transport/adapters/net.rb b/lib/datadog/ci/transport/adapters/net.rb index a6d7f4b2..a92de751 100644 --- a/lib/datadog/ci/transport/adapters/net.rb +++ b/lib/datadog/ci/transport/adapters/net.rb @@ -81,6 +81,10 @@ def payload @decompressed_payload = Gzip.decompress(http_response.body) end + def response_size + http_response.body.bytesize + end + def header(name) http_response[name] end diff --git a/sig/datadog/ci/git/search_commits.rbs b/sig/datadog/ci/git/search_commits.rbs index e650957f..0b4835c0 100644 --- a/sig/datadog/ci/git/search_commits.rbs +++ b/sig/datadog/ci/git/search_commits.rbs @@ -17,7 +17,7 @@ module Datadog def request_payload: (String repository_url, Array[String] commits) -> String - def parse_json_response: (Datadog::Core::Transport::Response response) -> Hash[String, untyped] + def parse_json_response: (Datadog::CI::Transport::Adapters::Net::Response response) -> Hash[String, untyped] def extract_commits: (Hash[String, untyped] response) -> Set[String] end diff --git a/sig/datadog/ci/test_optimisation/skippable.rbs b/sig/datadog/ci/test_optimisation/skippable.rbs index 4c704ea5..dc1333a6 100644 --- a/sig/datadog/ci/test_optimisation/skippable.rbs +++ b/sig/datadog/ci/test_optimisation/skippable.rbs @@ -7,10 +7,10 @@ module Datadog @config_tags: Hash[String, String] class Response - @http_response: Datadog::Core::Transport::HTTP::Adapters::Net::Response? + @http_response: Datadog::CI::Transport::Adapters::Net::Response? @json: Hash[String, untyped]? - def initialize: (Datadog::Core::Transport::HTTP::Adapters::Net::Response? http_response) -> void + def initialize: (Datadog::CI::Transport::Adapters::Net::Response? http_response) -> void def ok?: () -> bool diff --git a/sig/datadog/ci/transport/adapters/net.rbs b/sig/datadog/ci/transport/adapters/net.rbs index 886f15da..f25eddc8 100644 --- a/sig/datadog/ci/transport/adapters/net.rbs +++ b/sig/datadog/ci/transport/adapters/net.rbs @@ -66,6 +66,8 @@ module Datadog def telemetry_error_type: () -> String? + def response_size: () -> Integer + def trace_count: () -> 0 def inspect: () -> ::String diff --git a/sig/datadog/ci/transport/http.rbs b/sig/datadog/ci/transport/http.rbs index b2d3d21a..4548ac33 100644 --- a/sig/datadog/ci/transport/http.rbs +++ b/sig/datadog/ci/transport/http.rbs @@ -1,11 +1,3 @@ -class SimpleDelegator - @decompressed_payload: String - - def __getobj__: () -> Datadog::Core::Transport::Response - def gzipped?: (String payload) -> bool - def payload: () -> String -end - module Datadog module CI module Transport diff --git a/sig/datadog/ci/transport/remote_settings_api.rbs b/sig/datadog/ci/transport/remote_settings_api.rbs index 925066bf..d017100d 100644 --- a/sig/datadog/ci/transport/remote_settings_api.rbs +++ b/sig/datadog/ci/transport/remote_settings_api.rbs @@ -3,10 +3,10 @@ module Datadog module Transport class RemoteSettingsApi class Response - @http_response: Datadog::Core::Transport::HTTP::Adapters::Net::Response? + @http_response: Datadog::CI::Transport::Adapters::Net::Response? @json: Hash[String, untyped]? - def initialize: (Datadog::Core::Transport::HTTP::Adapters::Net::Response? http_response) -> void + def initialize: (Datadog::CI::Transport::Adapters::Net::Response? http_response) -> void def ok?: () -> bool diff --git a/spec/datadog/ci/test_optimisation/skippable_spec.rb b/spec/datadog/ci/test_optimisation/skippable_spec.rb index d9699f79..2d266c72 100644 --- a/spec/datadog/ci/test_optimisation/skippable_spec.rb +++ b/spec/datadog/ci/test_optimisation/skippable_spec.rb @@ -3,6 +3,8 @@ require_relative "../../../../lib/datadog/ci/test_optimisation/skippable" RSpec.describe Datadog::CI::TestOptimisation::Skippable do + include_context "Telemetry spy" + let(:api) { spy("api") } let(:dd_env) { "ci" } let(:config_tags) { {} } @@ -10,6 +12,8 @@ subject(:client) { described_class.new(api: api, dd_env: dd_env, config_tags: config_tags) } describe "#fetch_skippable_tests" do + subject { client.fetch_skippable_tests(test_session) } + let(:service) { "service" } let(:tracer_span) do Datadog::Tracing::SpanOperation.new("session", service: service).tap do |span| @@ -30,7 +34,7 @@ let(:path) { Datadog::CI::Ext::Transport::DD_API_SKIPPABLE_TESTS_PATH } it "requests the skippable tests" do - client.fetch_skippable_tests(test_session) + subject expect(api).to have_received(:api_request) do |args| expect(args[:path]).to eq(path) @@ -90,7 +94,11 @@ } } ] - }.to_json + }.to_json, + request_compressed: false, + duration_ms: 1.2, + gzipped_content?: false, + response_size: 100 ) end @@ -99,6 +107,10 @@ expect(response.correlation_id).to eq("correlation_id_123") expect(response.tests).to eq(Set.new(["test_suite_name.test_name.string"])) end + + it_behaves_like "emits telemetry metric", :inc, "itr_skippable_tests.request", 1 + it_behaves_like "emits telemetry metric", :distribution, "itr_skippable_tests.request_ms" + it_behaves_like "emits telemetry metric", :distribution, "itr_skippable_tests.response_bytes" end context "when response is not OK" do @@ -106,7 +118,11 @@ double( "http_response", ok?: false, - payload: "" + payload: "", + request_compressed: false, + duration_ms: 1.2, + gzipped_content?: false, + response_size: 100 ) end @@ -122,7 +138,11 @@ double( "http_response", ok?: true, - payload: "not json" + payload: "not json", + request_compressed: false, + duration_ms: 1.2, + gzipped_content?: false, + response_size: 100 ) end @@ -155,7 +175,11 @@ "runtime.architecture" => "amd64" } } - }.to_json + }.to_json, + request_compressed: false, + duration_ms: 1.2, + gzipped_content?: false, + response_size: 100 ) end @@ -186,7 +210,7 @@ end it "requests the skippable tests with custom configurations" do - client.fetch_skippable_tests(test_session) + subject expect(api).to have_received(:api_request) do |args| data = JSON.parse(args[:payload])["data"] diff --git a/spec/datadog/ci/transport/http_spec.rb b/spec/datadog/ci/transport/http_spec.rb index 605856e9..72055858 100644 --- a/spec/datadog/ci/transport/http_spec.rb +++ b/spec/datadog/ci/transport/http_spec.rb @@ -119,6 +119,7 @@ expect(response.payload).to eq("sample payload") expect(response.request_compressed).to eq(false) expect(response.request_size).to eq(payload.bytesize) + expect(response.response_size).to eq(response_payload.bytesize) expect(response.duration_ms).to be > 0 expect(response.telemetry_error_type).to be_nil end