Skip to content

Commit

Permalink
Merge branch 'INFRA-9686_add_obtaining_IPv4' into 'master'
Browse files Browse the repository at this point in the history
feat: add ServiceDiscoveryIPv4 to EnvironmentInfo

See merge request vostok-libraries/commons.environment!1
  • Loading branch information
Романов Артём Халяфович committed Sep 3, 2024
2 parents 39f7480 + e7a058e commit 5988929
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`.
Expand Down
10 changes: 9 additions & 1 deletion Vostok.Commons.Environment.Tests/EnvironmentInfo_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
{
Expand Down
25 changes: 25 additions & 0 deletions Vostok.Commons.Environment/EnvironmentInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<string> application = new Lazy<string>(ObtainApplicationName);
private static Lazy<string> host = new Lazy<string>(ObtainHostname);
private static Lazy<string> fqdn = new Lazy<string>(ObtainFQDN);
private static Lazy<string> serviceDiscoveryIPv4 = new Lazy<string>(ObtainServiceDiscoveryIPv4);
private static Lazy<string> processName = new Lazy<string>(GetProcessNameOrNull);
private static Lazy<string> homeDirectory = new Lazy<string>(ObtainHomeDirectory);
private static Lazy<int?> processId = new Lazy<int?>(GetProcessIdOrNull);
Expand All @@ -41,6 +44,12 @@ internal static class EnvironmentInfo
/// </summary>
public static string FQDN => fqdn.Value;

/// <summary>
/// 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.
/// </summary>
public static string ServiceDiscoveryIPv4 => serviceDiscoveryIPv4.Value;

/// <summary>
/// Returns the name of current process.
/// </summary>
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5988929

Please sign in to comment.