Skip to content

Commit

Permalink
Add Utility to Collect Platform Information (#2097)
Browse files Browse the repository at this point in the history
* Add host information utility

* fixup! Add host information utility

* Rename file to platform

* fixup! Rename file to platform

* Require etc in file
  • Loading branch information
jennchenn committed Jun 20, 2022
1 parent b1ed42d commit e90cdf3
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
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

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
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

0 comments on commit e90cdf3

Please sign in to comment.