diff --git a/lib/datadog/ci/git/telemetry.rb b/lib/datadog/ci/git/telemetry.rb new file mode 100644 index 00000000..c779f64f --- /dev/null +++ b/lib/datadog/ci/git/telemetry.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +module Datadog + module CI + module Git + module Telemetry + def self.git_command(command) + Utils::Telemetry.inc(Ext::Telemetry::METRIC_GIT_COMMAND, 1, tags_for_command(command)) + end + + def self.git_command_errors(command, exit_code: nil, executable_missing: false) + tags = tags_for_command(command) + + exit_code_tag_value = exit_code_for(exit_code: exit_code, executable_missing: executable_missing) + tags[Ext::Telemetry::TAG_EXIT_CODE] = exit_code_tag_value if exit_code_tag_value + + Utils::Telemetry.inc(Ext::Telemetry::METRIC_GIT_COMMAND_ERRORS, 1, tags) + end + + def self.git_command_ms(command, duration_ms) + Utils::Telemetry.distribution(Ext::Telemetry::METRIC_GIT_COMMAND_MS, duration_ms, tags_for_command(command)) + end + + def self.tags_for_command(command) + {Ext::Telemetry::TAG_COMMAND => command} + end + + def self.exit_code_for(exit_code: nil, executable_missing: false) + return Ext::Telemetry::ExitCode::MISSING if executable_missing + return exit_code.to_s if exit_code + + nil + end + end + end + end +end diff --git a/sig/datadog/ci/git/telemetry.rbs b/sig/datadog/ci/git/telemetry.rbs new file mode 100644 index 00000000..633bdfdd --- /dev/null +++ b/sig/datadog/ci/git/telemetry.rbs @@ -0,0 +1,17 @@ +module Datadog + module CI + module Git + module Telemetry + def self.git_command: (String command) -> void + + def self.git_command_errors: (String command, ?exit_code: Integer?, ?executable_missing: bool) -> void + + def self.git_command_ms: (String command, untyped duration_ms) -> void + + def self.tags_for_command: (String command) -> ::Hash[String, String] + + def self.exit_code_for: (?exit_code: Integer?, ?executable_missing: bool) -> String? + end + end + end +end diff --git a/spec/datadog/ci/git/telemetry_spec.rb b/spec/datadog/ci/git/telemetry_spec.rb new file mode 100644 index 00000000..80c55dee --- /dev/null +++ b/spec/datadog/ci/git/telemetry_spec.rb @@ -0,0 +1,82 @@ +# frozen_string_literal: true + +require_relative "../../../../lib/datadog/ci/git/telemetry" + +RSpec.describe Datadog::CI::Git::Telemetry do + describe ".git_command" do + subject(:git_command) { described_class.git_command(command) } + + let(:command) { "git ls-remote --get-url" } + + before do + expect(Datadog::CI::Utils::Telemetry).to receive(:inc) + .with(Datadog::CI::Ext::Telemetry::METRIC_GIT_COMMAND, 1, expected_tags) + end + + let(:expected_tags) do + { + Datadog::CI::Ext::Telemetry::TAG_COMMAND => command + } + end + + it { git_command } + end + + describe ".git_command_errors" do + subject(:git_command_errors) { described_class.git_command_errors(command, exit_code: exit_code, executable_missing: executable_missing) } + + let(:command) { "git ls-remote --get-url" } + before do + expect(Datadog::CI::Utils::Telemetry).to receive(:inc) + .with(Datadog::CI::Ext::Telemetry::METRIC_GIT_COMMAND_ERRORS, 1, expected_tags) + end + + context "when exit code is 1" do + let(:exit_code) { 1 } + let(:executable_missing) { false } + + let(:expected_tags) do + { + Datadog::CI::Ext::Telemetry::TAG_COMMAND => command, + Datadog::CI::Ext::Telemetry::TAG_EXIT_CODE => exit_code.to_s + } + end + + it { git_command_errors } + end + + context "when executable is missing" do + let(:executable_missing) { true } + let(:exit_code) { nil } + + let(:expected_tags) do + { + Datadog::CI::Ext::Telemetry::TAG_COMMAND => command, + Datadog::CI::Ext::Telemetry::TAG_EXIT_CODE => Datadog::CI::Ext::Telemetry::ExitCode::MISSING + } + end + + it { git_command_errors } + end + end + + describe ".git_command_ms" do + subject(:git_command_ms) { described_class.git_command_ms(command, duration_ms) } + + let(:command) { "git ls-remote --get-url" } + let(:duration_ms) { 100 } + + before do + expect(Datadog::CI::Utils::Telemetry).to receive(:distribution) + .with(Datadog::CI::Ext::Telemetry::METRIC_GIT_COMMAND_MS, duration_ms, expected_tags) + end + + let(:expected_tags) do + { + Datadog::CI::Ext::Telemetry::TAG_COMMAND => command + } + end + + it { git_command_ms } + end +end