Skip to content

Commit

Permalink
send correct settings request payload
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Mar 5, 2024
1 parent 4842bba commit 688f02a
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 14 deletions.
42 changes: 42 additions & 0 deletions lib/datadog/ci/span.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,48 @@ def set_tags(tags)
tracer_span.set_tags(tags)
end

# Returns the git repository URL extracted from the environment.
# @return [String] the repository URL.
def git_repository_url
tracer_span.get_tag(Ext::Git::TAG_REPOSITORY_URL)
end

# Returns the latest commit SHA extracted from the environment.
# @return [String] the commit SHA of the last commit.
def git_commit_sha
tracer_span.get_tag(Ext::Git::TAG_COMMIT_SHA)
end

# Returns the git branch name extracted from the environment.
# @return [String] the branch.
def git_branch
tracer_span.get_tag(Ext::Git::TAG_BRANCH)
end

# Returns the OS architecture extracted from the environment.
# @return [String] OS arch.
def os_architecture
tracer_span.get_tag(Ext::Test::TAG_OS_ARCHITECTURE)
end

# Returns the OS platform extracted from the environment.
# @return [String] OS platform.
def os_platform
tracer_span.get_tag(Ext::Test::TAG_OS_PLATFORM)
end

# Returns the runtime name extracted from the environment.
# @return [String] runtime name.
def runtime_name
tracer_span.get_tag(Ext::Test::TAG_RUNTIME_NAME)
end

# Returns the runtime version extracted from the environment.
# @return [String] runtime version.
def runtime_version
tracer_span.get_tag(Ext::Test::TAG_RUNTIME_VERSION)
end

def set_environment_runtime_tags
tracer_span.set_tag(Ext::Test::TAG_OS_ARCHITECTURE, ::RbConfig::CONFIG["host_cpu"])
tracer_span.set_tag(Ext::Test::TAG_OS_PLATFORM, ::RbConfig::CONFIG["host_os"])
Expand Down
3 changes: 1 addition & 2 deletions lib/datadog/ci/test_visibility/recorder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,7 @@ def itr_enabled?
def configure_library(test_session)
return unless itr_enabled?
# TODO: error handling when request failed - disable ITR runner
# TODO: need to pass runtime information
# TODO: disable ITR runner if itr_enabled is false in settings
# TODO: configure ITR runner based on response
@api_client.fetch_library_settings(test_session)
end

Expand Down
29 changes: 19 additions & 10 deletions lib/datadog/ci/transport/api_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ module CI
module Transport
# Datadog API client
# Calls settings endpoint to fetch library settings for given service and env
#
# TODO: Rename ApiClient to SettingsApiClient
class ApiClient
def initialize(api: nil, dd_env: nil)
@api = api
Expand All @@ -19,25 +21,32 @@ def fetch_library_settings(test_session)
# TODO: return error response if api is not present
api = @api
return {} unless api
# TODO: id generation
# TODO: runtime information is required for payload
# TODO: return error response - use some wrapper from ddtrace as an example
api.api_request(
path: Ext::Transport::DD_API_SETTINGS_PATH,
payload: settings_payload(service: test_session.service)
payload: payload(test_session)
)
end

private

def settings_payload(service:)
def payload(test_session)
{
data: {
id: Datadog::Core::Environment::Identity.id,
type: Ext::Transport::DD_API_SETTINGS_TYPE,
attributes: {
service: service,
env: @dd_env
"data" => {
"id" => Datadog::Core::Environment::Identity.id,
"type" => Ext::Transport::DD_API_SETTINGS_TYPE,
"attributes" => {
"service" => test_session.service,
"env" => @dd_env,
"repository_url" => test_session.git_repository_url,
"branch" => test_session.git_branch,
"sha" => test_session.git_commit_sha,
"configurations" => {
"os.platform" => test_session.os_platform,
"os.arch" => test_session.os_architecture,
"runtime.name" => test_session.runtime_name,
"runtime.version" => test_session.runtime_version
}
}
}
}.to_json
Expand Down
14 changes: 14 additions & 0 deletions sig/datadog/ci/span.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ module Datadog

def set_parameters: (Hash[String, Object] arguments, ?Hash[String, Object] metadata) -> void

def git_repository_url: () -> String?

def git_commit_sha: () -> String?

def git_branch: () -> String?

def os_architecture: () -> String?

def os_platform: () -> String?

def runtime_name: () -> String?

def runtime_version: () -> String?

private

def recorder: () -> Datadog::CI::TestVisibility::Recorder
Expand Down
2 changes: 1 addition & 1 deletion sig/datadog/ci/transport/api_client.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Datadog

private

def settings_payload: (service: String?) -> String
def payload: (Datadog::CI::TestSession test_session) -> String
end
end
end
Expand Down
56 changes: 56 additions & 0 deletions spec/datadog/ci/span_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,60 @@
expect(span.type).to eq("test")
end
end

describe "#git_repository_url" do
it "returns the git repository URL" do
expect(tracer_span).to receive(:get_tag).with("git.repository_url").and_return("url")

expect(span.git_repository_url).to eq("url")
end
end

describe "#git_commit_sha" do
it "returns the git commit SHA" do
expect(tracer_span).to receive(:get_tag).with("git.commit.sha").and_return("sha")

expect(span.git_commit_sha).to eq("sha")
end
end

describe "#git_branch" do
it "returns the git branch" do
expect(tracer_span).to receive(:get_tag).with("git.branch").and_return("branch")

expect(span.git_branch).to eq("branch")
end
end

describe "#os_architecture" do
it "returns the OS architecture" do
expect(tracer_span).to receive(:get_tag).with("os.architecture").and_return("arch")

expect(span.os_architecture).to eq("arch")
end
end

describe "#os_platform" do
it "returns the OS platform" do
expect(tracer_span).to receive(:get_tag).with("os.platform").and_return("platform")

expect(span.os_platform).to eq("platform")
end
end

describe "#runtime_name" do
it "returns the runtime name" do
expect(tracer_span).to receive(:get_tag).with("runtime.name").and_return("name")

expect(span.runtime_name).to eq("name")
end
end

describe "#runtime_version" do
it "returns the runtime version" do
expect(tracer_span).to receive(:get_tag).with("runtime.version").and_return("version")

expect(span.runtime_version).to eq("version")
end
end
end
23 changes: 22 additions & 1 deletion spec/datadog/ci/transport/api_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,19 @@

describe "#fetch_library_settings" do
let(:service) { "service" }
let(:tracer_span) { double("tracer_span", service: service) }
let(:tracer_span) do
Datadog::Tracing::SpanOperation.new("session", service: service).tap do |span|
span.set_tags({
"git.repository_url" => "repository_url",
"git.branch" => "branch",
"git.commit.sha" => "commit_sha",
"os.platform" => "platform",
"os.architecture" => "arch",
"runtime.name" => "runtime_name",
"runtime.version" => "runtime_version"
})
end
end
let(:test_session) { Datadog::CI::TestSession.new(tracer_span) }

let(:path) { Datadog::CI::Ext::Transport::DD_API_SETTINGS_PATH }
Expand All @@ -27,6 +39,15 @@
attributes = data["attributes"]
expect(attributes["service"]).to eq(service)
expect(attributes["env"]).to eq(dd_env)
expect(attributes["repository_url"]).to eq("repository_url")
expect(attributes["branch"]).to eq("branch")
expect(attributes["sha"]).to eq("commit_sha")

configurations = attributes["configurations"]
expect(configurations["os.platform"]).to eq("platform")
expect(configurations["os.arch"]).to eq("arch")
expect(configurations["runtime.name"]).to eq("runtime_name")
expect(configurations["runtime.version"]).to eq("runtime_version")
end
end
end
Expand Down

0 comments on commit 688f02a

Please sign in to comment.