Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/Specify-ip-address-in-host-name-…
Browse files Browse the repository at this point in the history
…for-application-with-vpn'
  • Loading branch information
kulbachnyi.v committed Feb 7, 2024
2 parents c0b01c7 + e99526f commit 32a3bf7
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.1.22 (07-02-2024)

Added method `SetHostnameProvider` for configuring hostname in `IReplicaInfoBuilder`

## 0.1.21 (31-08-2022):

Added missing `ExecutionContext.SuppressFlow` for long-running tasks.
Expand Down
73 changes: 64 additions & 9 deletions Vostok.ServiceDiscovery.Tests/ReplicaInfoBuilder_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,14 @@ public void Should_be_configurable()
.SetCommitHash("ASDF")
.SetReleaseDate("released now")
.SetDependencies(new List<string> {"dep-a", "dep-b"})
.SetTags(new TagCollection{"tag1", {"tag2", "value"}}), false);
.SetTags(new TagCollection {"tag1", {"tag2", "value"}})
.SetHostnameProvider(() => "newHostname"),
false);

info.ReplicaInfo.Environment.Should().Be("custom-environment");
info.ReplicaInfo.Application.Should().Be("Vostok.App.1");
info.ReplicaInfo.Replica.Should().Be("https://github.com:123/vostok");
info.Tags.Should().BeEquivalentTo(new TagCollection{"tag1", {"tag2", "value"}});
info.Tags.Should().BeEquivalentTo(new TagCollection {"tag1", {"tag2", "value"}});

var properties = info.ReplicaInfo.Properties;

Expand All @@ -82,19 +84,23 @@ public void Should_be_configurable()
properties[ReplicaInfoKeys.CommitHash].Should().Be("ASDF");
properties[ReplicaInfoKeys.ReleaseDate].Should().Be("released now");
properties[ReplicaInfoKeys.Dependencies].Should().Be("dep-a;dep-b");

properties[ReplicaInfoKeys.Host].Should().Be("newHostname");
}

[Test]
public void Should_build_url_from_parts()
{
var info = ReplicaInfoBuilder.Build(
setup => setup
.SetScheme("https")
.SetPort(123)
.SetUrlPath("vostok"), false)
setup => setup
.SetScheme("https")
.SetPort(123)
.SetUrlPath("vostok")
.SetHostnameProvider(() => "testhost"),
false)
.ReplicaInfo;

var host = EnvironmentInfo.Host.ToLowerInvariant();
var host = "testhost";

info.Replica.Should().Be($"https://{host}:123/vostok");

Expand Down Expand Up @@ -148,7 +154,8 @@ public void Should_add_properties()
{
builder.SetProperty("key1", "value1");
builder.SetProperty("key2", "value2");
}, false);
},
false);

var properties = info.ReplicaInfo.Properties;
properties["key1"].Should().Be("value1");
Expand All @@ -163,7 +170,8 @@ public void Should_rewrite_properties()
{
builder.SetProperty("key", "value1");
builder.SetProperty("key", "value2");
}, false);
},
false);

var properties = info.ReplicaInfo.Properties;
properties["key"].Should().Be("value2");
Expand All @@ -179,5 +187,52 @@ public void Should_rewrite_default_properties()
info.ReplicaInfo.Replica.Should().Be($"{host}({Process.GetCurrentProcess().Id})");
info.ReplicaInfo.Properties[ReplicaInfoKeys.Replica].Should().Be("value");
}

[Test]
public void Should_not_configure_hostname_when_uri_set()
{
var url = new UriBuilder
{
Scheme = "https",
Host = "github.com",
Port = 123,
Path = "vostok"
}.Uri;

var info = ReplicaInfoBuilder.Build(
setup => setup
.SetUrl(url)
.SetHostnameProvider(() => "newHostname"),
false
);

info.ReplicaInfo.Replica.Should().Be("https://github.com:123/vostok");
}

[TestCase(null)]
[TestCase("newHostname")]
[TestCase("")]
public void Should_correctly_build_url_with_hostname_provider(string hostname)
{
var info = ReplicaInfoBuilder.Build(
setup => setup
.SetScheme("https")
.SetPort(123)
.SetHostnameProvider(() => hostname),
false)
.ReplicaInfo;

var expectedHostname = string.IsNullOrEmpty(hostname) ? EnvironmentInfo.Host : hostname;
var host = expectedHostname.ToLowerInvariant();

info.Replica.Should().Be($"https://{host}:123/");

var properties = info.Properties;

properties[ReplicaInfoKeys.Replica].Should().Be($"https://{host}:123/");

properties[ReplicaInfoKeys.Url].Should().BeEquivalentTo($"https://{host}:123/");
properties[ReplicaInfoKeys.Host].Should().Be(expectedHostname);
}
}
}
9 changes: 8 additions & 1 deletion Vostok.ServiceDiscovery/IReplicaInfoBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ public interface IReplicaInfoBuilder
[NotNull]
IReplicaInfoBuilder SetUrlPath([CanBeNull] string path);

/// <summary>
/// <para>Sets configuration for HostnameProvider.</para>
/// <para>Default value: <c>null</c></para>
/// <para>Should not be called in conjunction with <see cref="SetUrl"/>.</para>
/// </summary>
IReplicaInfoBuilder SetHostnameProvider([CanBeNull] Func<string> vpnHostnameProvider);

/// <summary>
/// <para>Sets build commit hash.</para>
/// <para>By default, it will be parsed from <c>AssemblyTitle</c> attribute of entry assembly.</para>
Expand Down Expand Up @@ -103,7 +110,7 @@ public interface IReplicaInfoBuilder
/// </summary>
[NotNull]
IReplicaInfoBuilder SetProperty([NotNull] string key, [CanBeNull] string value);

/// <summary>
/// Sets a custom beacon <paramref name="tags"/>, which can be overwritten only by restarting the beacon.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions Vostok.ServiceDiscovery/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Vostok.ServiceDiscovery.IReplicaInfoBuilder.SetHostnameProvider(System.Func<string> vpnHostnameProvider) -> Vostok.ServiceDiscovery.IReplicaInfoBuilder
22 changes: 17 additions & 5 deletions Vostok.ServiceDiscovery/ReplicaInfoBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ namespace Vostok.ServiceDiscovery
internal class ReplicaInfoBuilder : IReplicaInfoBuilder
{
private const string DependenciesDelimiter = ";";
private readonly string host;
private readonly int? processId;
private readonly string baseDirectory;

private readonly Dictionary<string, string> properties = new Dictionary<string, string>();

private string host;
private string processName;

private string environment;
Expand All @@ -34,6 +34,8 @@ internal class ReplicaInfoBuilder : IReplicaInfoBuilder
private List<string> dependencies;
private TagCollection tags;

private Func<string> hostnameProvider;

private ReplicaInfoBuilder(bool useFQDN)
{
environment = "default";
Expand All @@ -57,10 +59,14 @@ public static ServiceBeaconInfo Build(ReplicaInfoSetup setup, bool useFQDN)

public ServiceBeaconInfo Build()
{
url = url ?? BuildUrl();
var vpnHostname = hostnameProvider?.Invoke();
if (!string.IsNullOrEmpty(vpnHostname))
{
host = vpnHostname;
}

if (replica == null)
replica = url?.ToString() ?? $"{host}({EnvironmentInfo.ProcessId})";
url ??= BuildUrl();
replica ??= url?.ToString() ?? $"{host}({EnvironmentInfo.ProcessId})";

if (url != null)
{
Expand Down Expand Up @@ -166,6 +172,12 @@ public IReplicaInfoBuilder SetUrlPath(string path)
return this;
}

public IReplicaInfoBuilder SetHostnameProvider(Func<string> vpnHostnameProvider)
{
this.hostnameProvider = vpnHostnameProvider;
return this;
}

public IReplicaInfoBuilder SetCommitHash(string commitHash)
{
this.commitHash = commitHash;
Expand Down Expand Up @@ -195,7 +207,7 @@ public IReplicaInfoBuilder SetProperty(string key, string value)
properties[key ?? throw new ArgumentNullException(nameof(key))] = value;
return this;
}

public IReplicaInfoBuilder SetTags(TagCollection tags)
{
this.tags = tags ?? new TagCollection();
Expand Down

0 comments on commit 32a3bf7

Please sign in to comment.