Skip to content

Commit

Permalink
#1057 #1068 Add service discovery logs (#1366)
Browse files Browse the repository at this point in the history
* log Consul querying and the answers

* Code review: Fix long expressions

* Remove and Sort Usings

* Use expression body

* Right order of initialization sentances

---------

Co-authored-by: raman-m <[email protected]>
  • Loading branch information
jlukawska and raman-m authored Sep 23, 2023
1 parent ec85b13 commit b27761f
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions src/Ocelot.Provider.Consul/Consul.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using global::Consul;

using Consul;
using Ocelot.Infrastructure.Extensions;

using Ocelot.Logging;

using Ocelot.ServiceDiscovery.Providers;

using Ocelot.Values;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Ocelot.Provider.Consul
{
Expand All @@ -24,19 +19,25 @@ public class Consul : IServiceDiscoveryProvider

public Consul(ConsulRegistryConfiguration config, IOcelotLoggerFactory factory, IConsulClientFactory clientFactory)
{
_logger = factory.CreateLogger<Consul>();
_config = config;
_logger = factory.CreateLogger<Consul>();
_consul = clientFactory.Get(_config);
}

public async Task<List<Service>> Get()
{
var consulAddress = (_consul as ConsulClient)?.Config.Address;
_logger.LogDebug($"Querying Consul {consulAddress} about a service: {_config.KeyOfServiceInConsul}");

var queryResult = await _consul.Health.Service(_config.KeyOfServiceInConsul, string.Empty, true);

var services = new List<Service>();

foreach (var serviceEntry in queryResult.Response)
{
var address = serviceEntry.Service.Address;
var port = serviceEntry.Service.Port;

if (IsValid(serviceEntry))
{
var nodes = await _consul.Catalog.Nodes();
Expand All @@ -46,13 +47,15 @@ public async Task<List<Service>> Get()
}
else
{
var serviceNode = nodes.Response.FirstOrDefault(n => n.Address == serviceEntry.Service.Address);
var serviceNode = nodes.Response.FirstOrDefault(n => n.Address == address);
services.Add(BuildService(serviceEntry, serviceNode));
}

_logger.LogDebug($"Consul answer: Address: {address}, Port: {port}");
}
else
{
_logger.LogWarning($"Unable to use service Address: {serviceEntry.Service.Address} and Port: {serviceEntry.Service.Port} as it is invalid. Address must contain host only e.g. localhost and port must be greater than 0");
_logger.LogWarning($"Unable to use service Address: {address} and Port: {port} as it is invalid. Address must contain host only e.g. localhost and port must be greater than 0");
}
}

Expand Down Expand Up @@ -80,10 +83,8 @@ private static bool IsValid(ServiceEntry serviceEntry)
}

private static string GetVersionFromStrings(IEnumerable<string> strings)
{
return strings
?.FirstOrDefault(x => x.StartsWith(VersionPrefix, StringComparison.Ordinal))
=> strings?
.FirstOrDefault(x => x.StartsWith(VersionPrefix, StringComparison.Ordinal))
.TrimStart(VersionPrefix);
}
}
}

0 comments on commit b27761f

Please sign in to comment.