Skip to content

Commit

Permalink
telemetry helpers for git commands
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Jul 26, 2024
1 parent 9cd40ca commit a0e08db
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 0 deletions.
37 changes: 37 additions & 0 deletions lib/datadog/ci/git/telemetry.rb
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions sig/datadog/ci/git/telemetry.rbs
Original file line number Diff line number Diff line change
@@ -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
82 changes: 82 additions & 0 deletions spec/datadog/ci/git/telemetry_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a0e08db

Please sign in to comment.