Skip to content

Commit

Permalink
Metrics for ConsulAdapter.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
muphblu committed Nov 14, 2023
1 parent 827b541 commit 8b28259
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions ATI.Services.Consul/ConsulAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using ATI.Services.Common.Metrics;
using Consul;
using NLog;

Expand All @@ -11,24 +12,29 @@ namespace ATI.Services.Consul;
internal class ConsulAdapter: IDisposable
{
private readonly ILogger _logger = LogManager.GetCurrentClassLogger();
private ConsulClient _consulClient;
private readonly ConsulClient _consulClient = new();
private readonly MetricsFactory _metricsFactory = MetricsFactory.CreateExternalHttpMetricsFactory();

/// <summary>
/// Возвращает список живых сервисов
/// </summary>
/// <returns></returns>
public async Task<List<ServiceEntry>> GetPassingServiceInstancesAsync(string serviceName, string environment, bool passingOnly = true)
{


try
{
_consulClient = new ConsulClient();
var fromConsul = await _consulClient.Health.Service(serviceName, environment, passingOnly);
if (fromConsul.StatusCode == HttpStatusCode.OK)
using (_metricsFactory.CreateMetricsTimer(nameof(GetPassingServiceInstancesAsync)))
{
return fromConsul.Response?.ToList();
}
var fromConsul = await _consulClient.Health.Service(serviceName, environment, passingOnly);
if (fromConsul.StatusCode == HttpStatusCode.OK)
{
return fromConsul.Response?.ToList();
}

_logger.Error($"По запросу в консул {serviceName}:{environment}, вернулся ответ со статусом: {fromConsul.StatusCode}");
_logger.Error($"По запросу в консул {serviceName}:{environment}, вернулся ответ со статусом: {fromConsul.StatusCode}");
}
}
catch (Exception e)
{
Expand Down

0 comments on commit 8b28259

Please sign in to comment.