diff --git a/CHANGELOG.md b/CHANGELOG.md index 67f5640..e7913f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.1.5 (03-09-2024): + +Added `ServiceDiscoveryIPv4` to `EnvironmentInfo` + ## 0.1.4 (22-08-2022): Read Home Directory from `System.Environment.SpecialFolder.UserProfile`. diff --git a/Vostok.Commons.Environment.Tests/EnvironmentInfo_Tests.cs b/Vostok.Commons.Environment.Tests/EnvironmentInfo_Tests.cs index 88c2c5e..4375e2e 100644 --- a/Vostok.Commons.Environment.Tests/EnvironmentInfo_Tests.cs +++ b/Vostok.Commons.Environment.Tests/EnvironmentInfo_Tests.cs @@ -15,7 +15,7 @@ public static void Application_name_should_be_not_null_or_empty() [Test] public static void Host_name_should_be_not_null_or_empty() - => string.IsNullOrEmpty(EnvironmentInfo.Application).Should().BeFalse(); + => string.IsNullOrEmpty(EnvironmentInfo.Host).Should().BeFalse(); [Test] public void FQDN_should_not_be_null_or_empty() @@ -25,6 +25,14 @@ public void FQDN_should_not_be_null_or_empty() Console.Out.WriteLine(EnvironmentInfo.FQDN); } + [Test] + public void ServiceDiscoveryIPv4_should_be_null_if_environment_variable_is_not_set() + { + EnvironmentInfo.ServiceDiscoveryIPv4.Should().BeNull(); + + Console.Out.WriteLine(EnvironmentInfo.ServiceDiscoveryIPv4); + } + [Test] public void HomeDirectory_should_not_be_null_or_empty() { diff --git a/Vostok.Commons.Environment/EnvironmentInfo.cs b/Vostok.Commons.Environment/EnvironmentInfo.cs index 159f50b..4a53b4a 100644 --- a/Vostok.Commons.Environment/EnvironmentInfo.cs +++ b/Vostok.Commons.Environment/EnvironmentInfo.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Net; using System.Net.NetworkInformation; +using System.Net.Sockets; using System.Reflection; using System.Runtime.InteropServices; using JetBrains.Annotations; @@ -18,10 +19,12 @@ internal static class EnvironmentInfo { public const string LocalHostnameVariable = "VOSTOK_LOCAL_HOSTNAME"; public const string LocalFQDNVariable = "VOSTOK_LOCAL_FQDN"; + public const string LocalServiceDiscoveryIPv4Variable = "VOSTOK_LOCAL_SERVICE_DISCOVERY_IPV4"; private static Lazy application = new Lazy(ObtainApplicationName); private static Lazy host = new Lazy(ObtainHostname); private static Lazy fqdn = new Lazy(ObtainFQDN); + private static Lazy serviceDiscoveryIPv4 = new Lazy(ObtainServiceDiscoveryIPv4); private static Lazy processName = new Lazy(GetProcessNameOrNull); private static Lazy homeDirectory = new Lazy(ObtainHomeDirectory); private static Lazy processId = new Lazy(GetProcessIdOrNull); @@ -41,6 +44,12 @@ internal static class EnvironmentInfo /// public static string FQDN => fqdn.Value; + /// + /// Returns the IPv4 through which the application is accessible on the hosting network. + /// This value is obtained once when the application starts and is cached for subsequent calls. + /// + public static string ServiceDiscoveryIPv4 => serviceDiscoveryIPv4.Value; + /// /// Returns the name of current process. /// @@ -193,6 +202,22 @@ private static string ObtainFQDN() } } + private static string ObtainServiceDiscoveryIPv4() + { + try + { + var localIPv4 = System.Environment.GetEnvironmentVariable(LocalServiceDiscoveryIPv4Variable); + if (!string.IsNullOrEmpty(localIPv4)) + return localIPv4; + + return null; + } + catch + { + return null; + } + } + private static string GetWindowsDomainName() { try