Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Utility to Collect Platform Information #2097

Merged
merged 5 commits into from
Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions lib/datadog/core/environment/platform.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# typed: false
marcotc marked this conversation as resolved.
Show resolved Hide resolved

require 'etc'

require 'datadog/core/environment/identity'

module Datadog
module Core
module Environment
# For gathering information about the platform
module Platform
module_function

# @return [String] name of host; `uname -n`
def hostname
Identity.lang_version >= '2.2' ? Etc.uname[:nodename] : nil
end

# @return [String] name of kernel; `uname -s`
def kernel_name
Identity.lang_version >= '2.2' ? Etc.uname[:sysname] : Gem::Platform.local.os.capitalize
end

# @return [String] kernel release; `uname -r`
def kernel_release
marcotc marked this conversation as resolved.
Show resolved Hide resolved
if Identity.lang_engine == 'jruby'
Etc.uname[:version] # Java's `os.version` maps to `uname -r`
elsif Identity.lang_version >= '2.2'
Etc.uname[:release]
end
end

# @return [String] kernel version; `uname -v`
def kernel_version
Etc.uname[:version] if Identity.lang_engine != 'jruby' && Identity.lang_version >= '2.2'
end
end
end
end
end
57 changes: 57 additions & 0 deletions spec/datadog/core/environment/platform_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# typed: false

require 'spec_helper'
require 'datadog/core/environment/platform'

RSpec.describe Datadog::Core::Environment::Platform do
describe '::hostname' do
subject(:hostname) { described_class.hostname }

context 'when Ruby version < 2.2', if: Datadog::Core::Environment::Ext::LANG_VERSION < '2.2' do
it { is_expected.to be_nil }
end

context 'when Ruby version >= 2.2', if: Datadog::Core::Environment::Ext::LANG_VERSION >= '2.2' do
it { is_expected.to be_a_kind_of(String) }
it { is_expected.to eql(`uname -n`.strip) }
end
end

describe '::kernel_name' do
subject(:kernel_name) { described_class.kernel_name }
it { is_expected.to be_a_kind_of(String) }
it { is_expected.to eql(`uname -s`.strip) }
end

describe '::kernel_release' do
subject(:kernel_release) { described_class.kernel_release }

context 'when Ruby version < 2.2', if: Datadog::Core::Environment::Ext::LANG_VERSION < '2.2' do
it { is_expected.to be_nil }
end

context 'when Ruby version >= 2.2', if: Datadog::Core::Environment::Ext::LANG_VERSION >= '2.2' do
it { is_expected.to be_a_kind_of(String) }
it { is_expected.to eql(`uname -r`.strip) }
end
end

describe '::kernel_version' do
subject(:kernel_version) { described_class.kernel_version }

context 'when using JRuby', if: Datadog::Core::Environment::Ext::RUBY_ENGINE == 'jruby' do
it { is_expected.to be_nil }
end

context 'when not using JRuby', unless: Datadog::Core::Environment::Ext::RUBY_ENGINE == 'jruby' do
context 'when Ruby version < 2.2', if: Datadog::Core::Environment::Ext::LANG_VERSION < '2.2' do
it { is_expected.to be_nil }
end

context 'when Ruby version >= 2.2', if: Datadog::Core::Environment::Ext::LANG_VERSION >= '2.2' do
it { is_expected.to be_a_kind_of(String) }
it { is_expected.to eql(`uname -v`.strip) }
end
end
end
end