-
Notifications
You must be signed in to change notification settings - Fork 368
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Utility to Collect Platform Information (#2097)
* 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
Showing
2 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |