Skip to content

Commit

Permalink
feat: Added configuration method for host name
Browse files Browse the repository at this point in the history
  • Loading branch information
kulbachnyi.v committed Dec 25, 2023
1 parent c0b01c7 commit bcf42dd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
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 SetupHostnameProvider([CanBeNull] Func<bool, string> hostNameProvider);

/// <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.SetupHostnameProvider(System.Func<bool, string> hostNameProvider) -> Vostok.ServiceDiscovery.IReplicaInfoBuilder
14 changes: 11 additions & 3 deletions Vostok.ServiceDiscovery/ReplicaInfoBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ internal class ReplicaInfoBuilder : IReplicaInfoBuilder
private List<string> dependencies;
private TagCollection tags;

private Func<bool, string> hostNameProvider;

private ReplicaInfoBuilder(bool useFQDN)
{
environment = "default";
application = EnvironmentInfo.Application;
host = useFQDN ? EnvironmentInfo.FQDN : EnvironmentInfo.Host;
host = useFQDN ? hostNameProvider?.Invoke(useFQDN) ?? EnvironmentInfo.FQDN : EnvironmentInfo.Host;
processName = EnvironmentInfo.ProcessName;
processId = EnvironmentInfo.ProcessId;
baseDirectory = EnvironmentInfo.BaseDirectory;
Expand All @@ -57,7 +59,7 @@ public static ServiceBeaconInfo Build(ReplicaInfoSetup setup, bool useFQDN)

public ServiceBeaconInfo Build()
{
url = url ?? BuildUrl();
url ??= BuildUrl();

if (replica == null)
replica = url?.ToString() ?? $"{host}({EnvironmentInfo.ProcessId})";
Expand Down Expand Up @@ -166,6 +168,12 @@ public IReplicaInfoBuilder SetUrlPath(string path)
return this;
}

public IReplicaInfoBuilder SetupHostnameProvider(Func<bool, string> hostNameProvider)
{
this.hostNameProvider = hostNameProvider;
return this;
}

public IReplicaInfoBuilder SetCommitHash(string commitHash)
{
this.commitHash = commitHash;
Expand Down Expand Up @@ -195,7 +203,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 bcf42dd

Please sign in to comment.