diff --git a/ATI.Services.Common/ATI.Services.Common.csproj b/ATI.Services.Common/ATI.Services.Common.csproj
index 12a642c..aeb5a12 100644
--- a/ATI.Services.Common/ATI.Services.Common.csproj
+++ b/ATI.Services.Common/ATI.Services.Common.csproj
@@ -4,7 +4,6 @@
linux-x64
Team Services
ATI
- 10
true
true
atisu.services.common
@@ -18,7 +17,7 @@
-
+
@@ -26,7 +25,7 @@
-
+
diff --git a/ATI.Services.Common/Initializers/MetricsInitializer.cs b/ATI.Services.Common/Initializers/MetricsInitializer.cs
index e37e990..e553676 100644
--- a/ATI.Services.Common/Initializers/MetricsInitializer.cs
+++ b/ATI.Services.Common/Initializers/MetricsInitializer.cs
@@ -25,10 +25,7 @@ public Task InitializeAsync()
return Task.CompletedTask;
}
- if (_metricsOptions.MetricsServiceName != null )
- {
- MetricsFactory.Init(_metricsOptions.MetricsServiceName, _metricsOptions.DefaultLongRequestTime);
- }
+ MetricsFactory.Init(_metricsOptions.DefaultLongRequestTime);
_initialized = true;
return Task.CompletedTask;
diff --git a/ATI.Services.Common/Logging/LogSource.cs b/ATI.Services.Common/Logging/LogSource.cs
index 5b3c800..0941e86 100644
--- a/ATI.Services.Common/Logging/LogSource.cs
+++ b/ATI.Services.Common/Logging/LogSource.cs
@@ -13,6 +13,7 @@ public enum LogSource
Sql = 4,
Controller = 5,
Repository = 6,
- ExternalHttpClient = 8
+ RabbitMq = 9,
+ Custom = 10
}
}
diff --git a/ATI.Services.Common/Metrics/ExceptionsMetricsCollector.cs b/ATI.Services.Common/Metrics/ExceptionsMetricsCollector.cs
index 0dd4f9e..eb63171 100644
--- a/ATI.Services.Common/Metrics/ExceptionsMetricsCollector.cs
+++ b/ATI.Services.Common/Metrics/ExceptionsMetricsCollector.cs
@@ -1,59 +1,49 @@
using System;
using System.Collections.Concurrent;
using System.Diagnostics.Tracing;
-using Microsoft.Extensions.Configuration;
using Prometheus;
-using ConfigurationManager = ATI.Services.Common.Behaviors.ConfigurationManager;
-namespace ATI.Services.Common.Metrics
-{
- public class ExceptionsMetricsCollector : EventListener
- {
- // Да, даже под linux - Microsoft-Windows
- private const string ExceptionSourceName = "Microsoft-Windows-DotNETRuntime";
- private const int ExceptionBit = 0x8000;
- private const int ExceptionNameIndex = 0;
- private readonly ConcurrentDictionary _exceptionCounters = new();
- private MetricFactory _metricFactory;
- private const string ExceptionsMetricName = "Exceptions";
- private Gauge _gauge;
+namespace ATI.Services.Common.Metrics;
- private string ServiceName { get; }
- public ExceptionsMetricsCollector()
- {
- ServiceName = ConfigurationManager.GetSection(nameof(MetricsOptions)).Get().MetricsServiceName;
- }
+public class ExceptionsMetricsCollector : EventListener
+{
+ // Да, даже под linux - Microsoft-Windows
+ private const string ExceptionSourceName = "Microsoft-Windows-DotNETRuntime";
+ private const int ExceptionBit = 0x8000;
+ private const int ExceptionNameIndex = 0;
+ private readonly ConcurrentDictionary _exceptionCounters = new();
+ private MetricFactory _metricFactory;
+ private Gauge _gauge;
- protected override void OnEventSourceCreated(EventSource eventSource)
- {
- if (eventSource.Name != ExceptionSourceName)
- return;
+ protected override void OnEventSourceCreated(EventSource eventSource)
+ {
+ if (eventSource.Name != ExceptionSourceName)
+ return;
- EnableEvents(
- eventSource,
- EventLevel.Error,
- (EventKeywords) ExceptionBit);
- }
+ EnableEvents(
+ eventSource,
+ EventLevel.Error,
+ (EventKeywords) ExceptionBit);
+ }
- protected override void OnEventWritten(EventWrittenEventArgs eventData)
- {
- var exceptionType = eventData.Payload[ExceptionNameIndex].ToString();
- _exceptionCounters.AddOrUpdate(exceptionType, _ => 1, (_, oldValue) => oldValue + 1);
- }
+ protected override void OnEventWritten(EventWrittenEventArgs eventData)
+ {
+ var exceptionType = eventData.Payload[ExceptionNameIndex].ToString();
+ _exceptionCounters.AddOrUpdate(exceptionType, _ => 1, (_, oldValue) => oldValue + 1);
+ }
- public void RegisterMetrics(CollectorRegistry registry)
- {
- _metricFactory = Prometheus.Metrics.WithCustomRegistry(registry);
- _gauge = _metricFactory.CreateGauge(ServiceName + ExceptionsMetricName, "", "machine_name", "exception_type");
- }
+ public void RegisterMetrics(CollectorRegistry registry)
+ {
+ _metricFactory = Prometheus.Metrics.WithCustomRegistry(registry);
+ _gauge = _metricFactory.CreateGauge($"{MetricsFactory.Prefix}_Exceptions", "", "machine_name", "exception_type");
+ }
- public void UpdateMetrics()
+ public void UpdateMetrics()
+ {
+ foreach (var exceptionType in _exceptionCounters.Keys)
{
- foreach (var exceptionType in _exceptionCounters.Keys)
- {
- _exceptionCounters.TryRemove(exceptionType, out var count);
- _gauge.WithLabels(Environment.MachineName, exceptionType).Set(count);
- }
+ _exceptionCounters.TryRemove(exceptionType, out var count);
+ _gauge.WithLabels(Environment.MachineName, exceptionType).Set(count);
}
}
}
\ No newline at end of file
diff --git a/ATI.Services.Common/Metrics/HttpWrapper/MetricsHttpClientWrapper.cs b/ATI.Services.Common/Metrics/HttpWrapper/MetricsHttpClientWrapper.cs
index 07eb715..13bfab7 100644
--- a/ATI.Services.Common/Metrics/HttpWrapper/MetricsHttpClientWrapper.cs
+++ b/ATI.Services.Common/Metrics/HttpWrapper/MetricsHttpClientWrapper.cs
@@ -15,537 +15,525 @@
using JetBrains.Annotations;
using NLog;
-namespace ATI.Services.Common.Metrics.HttpWrapper
+namespace ATI.Services.Common.Metrics.HttpWrapper;
+
+///
+/// Для удобства лучше использовать ConsulMetricsHttpClientWrapper из ATI.Services.Consul
+/// Он внутри себя инкапсулирует ConsulServiceAddress и MetricsFactory
+///
+[PublicAPI]
+public class MetricsHttpClientWrapper
{
- ///
- /// Для удобства лучше использовать ConsulMetricsHttpClientWrapper из ATI.Services.Consul
- /// Он внутри себя инкапсулирует ConsulServiceAddress и MetricsFactory
- ///
- [PublicAPI]
- public class MetricsHttpClientWrapper
+ private readonly ILogger _logger;
+ private readonly HttpClient _httpClient;
+ private readonly Func _logLevelOverride;
+
+ private const string LogMessageTemplate =
+ "Сервис:{0} в ответ на запрос [HTTP {1} {2}] вернул ответ с статус кодом {3}.";
+
+ public MetricsHttpClientWrapper(MetricsHttpClientConfig config)
{
- private readonly ILogger _logger;
- private readonly HttpClient _httpClient;
- private readonly MetricsFactory _metricsFactory;
- private readonly Func _logLevelOverride;
+ Config = config;
+ _logger = LogManager.GetLogger(Config.ServiceName);
+ _httpClient = CreateHttpClient(config.Headers, config.PropagateActivity);
+ _logLevelOverride = Config.LogLevelOverride;
+ }
- private const string LogMessageTemplate =
- "Сервис:{0} в ответ на запрос [HTTP {1} {2}] вернул ответ с статус кодом {3}.";
+ public MetricsHttpClientConfig Config { get; }
- public MetricsHttpClientWrapper(MetricsHttpClientConfig config)
+ private HttpClient CreateHttpClient(Dictionary additionalHeaders, bool propagateActivity = true)
+ {
+ HttpClient httpClient;
+ if (propagateActivity)
{
- Config = config;
- _logger = LogManager.GetLogger(Config.ServiceName);
- _httpClient = CreateHttpClient(config.Headers, config.PropagateActivity);
- _metricsFactory = MetricsFactory.CreateExternalHttpMetricsFactory();
- _logLevelOverride = Config.LogLevelOverride;
+ httpClient = new HttpClient();
}
-
- public MetricsHttpClientConfig Config { get; }
-
- private HttpClient CreateHttpClient(Dictionary additionalHeaders, bool propagateActivity = true)
+ else
{
- HttpClient httpClient;
- if (propagateActivity)
- {
- httpClient = new HttpClient();
- }
- else
+ httpClient = new HttpClient(new SocketsHttpHandler
{
- httpClient = new HttpClient(new SocketsHttpHandler
- {
- ActivityHeadersPropagator = DistributedContextPropagator.CreateNoOutputPropagator()
- });
- }
+ ActivityHeadersPropagator = DistributedContextPropagator.CreateNoOutputPropagator()
+ });
+ }
- httpClient.Timeout = Config.Timeout;
- httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
- if (!ServiceVariables.ServiceAsClientName.IsNullOrEmpty() &&
- !ServiceVariables.ServiceAsClientHeaderName.IsNullOrEmpty())
- {
- httpClient.DefaultRequestHeaders.Add(
- ServiceVariables.ServiceAsClientHeaderName,
- ServiceVariables.ServiceAsClientName);
- }
+ httpClient.Timeout = Config.Timeout;
+ httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
+ if (!ServiceVariables.ServiceAsClientName.IsNullOrEmpty() &&
+ !ServiceVariables.ServiceAsClientHeaderName.IsNullOrEmpty())
+ {
+ httpClient.DefaultRequestHeaders.Add(
+ ServiceVariables.ServiceAsClientHeaderName,
+ ServiceVariables.ServiceAsClientName);
+ }
- if (additionalHeaders != null && additionalHeaders.Count > 0)
+ if (additionalHeaders != null && additionalHeaders.Count > 0)
+ {
+ foreach (var header in additionalHeaders)
{
- foreach (var header in additionalHeaders)
- {
- httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
- }
+ httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
}
-
- return httpClient;
}
+ return httpClient;
+ }
- public async Task> GetAsync(Uri fullUrl, string metricName,
- Dictionary headers = null)
- => await SendAsync(metricName, new HttpMessage(HttpMethod.Get, fullUrl, headers));
+ public async Task> GetAsync(Uri fullUrl, string metricName,
+ Dictionary headers = null)
+ => await SendAsync(metricName, new HttpMessage(HttpMethod.Get, fullUrl, headers));
- public async Task> GetAsync(string serviceAddress, string metricName,
- string url, Dictionary headers = null)
- => await SendAsync(metricName,
- new HttpMessage(HttpMethod.Get, FullUri(serviceAddress, url), headers));
+ public async Task> GetAsync(string serviceAddress, string metricName,
+ string url, Dictionary headers = null)
+ => await SendAsync(metricName,
+ new HttpMessage(HttpMethod.Get, FullUri(serviceAddress, url), headers));
- public async Task> GetAsync(string serviceAddress, string metricName, string url,
- Dictionary headers = null)
- => await SendAsync(metricName, new HttpMessage(HttpMethod.Get, FullUri(serviceAddress, url), headers));
+ public async Task> GetAsync(string serviceAddress, string metricName, string url,
+ Dictionary headers = null)
+ => await SendAsync(metricName, new HttpMessage(HttpMethod.Get, FullUri(serviceAddress, url), headers));
- public async Task> GetAsync(Uri fullUrl, string metricName,
- Dictionary headers = null)
- => await SendAsync(metricName, new HttpMessage(HttpMethod.Get, fullUrl, headers));
+ public async Task> GetAsync(Uri fullUrl, string metricName,
+ Dictionary headers = null)
+ => await SendAsync(metricName, new HttpMessage(HttpMethod.Get, fullUrl, headers));
- public async Task> PostAsync(string serviceAddress, string metricName,
- string url, TModel model, Dictionary headers = null)
- => await SendAsync(metricName,
- new HttpMessage(HttpMethod.Post, FullUri(serviceAddress, url), headers)
- {
- Content = Config.Serializer.Serialize(model)
- });
+ public async Task> PostAsync(string serviceAddress, string metricName,
+ string url, TModel model, Dictionary headers = null)
+ => await SendAsync(metricName,
+ new HttpMessage(HttpMethod.Post, FullUri(serviceAddress, url), headers)
+ {
+ Content = Config.Serializer.Serialize(model)
+ });
- public async Task> PostAsync(Uri fullUrl, string metricName,
- TModel model, Dictionary headers = null)
- => await SendAsync(metricName, new HttpMessage(HttpMethod.Post, fullUrl, headers)
- {
- Content = Config.Serializer.Serialize(model)
- });
+ public async Task> PostAsync(Uri fullUrl, string metricName,
+ TModel model, Dictionary headers = null)
+ => await SendAsync(metricName, new HttpMessage(HttpMethod.Post, fullUrl, headers)
+ {
+ Content = Config.Serializer.Serialize(model)
+ });
- public async Task> PostAsync(string serviceAddress, string metricName,
- string url, TModel model, Dictionary headers = null)
- => await SendAsync(metricName, new HttpMessage(HttpMethod.Post, FullUri(serviceAddress, url), headers)
- {
- Content = Config.Serializer.Serialize(model)
- });
+ public async Task> PostAsync(string serviceAddress, string metricName,
+ string url, TModel model, Dictionary headers = null)
+ => await SendAsync(metricName, new HttpMessage(HttpMethod.Post, FullUri(serviceAddress, url), headers)
+ {
+ Content = Config.Serializer.Serialize(model)
+ });
- public async Task> PostAsync(Uri fullUrl, string metricName, TModel model,
- Dictionary headers = null)
- => await SendAsync(metricName, new HttpMessage(HttpMethod.Post, fullUrl, headers)
- {
- Content = Config.Serializer.Serialize(model)
- });
+ public async Task> PostAsync(Uri fullUrl, string metricName, TModel model,
+ Dictionary headers = null)
+ => await SendAsync(metricName, new HttpMessage(HttpMethod.Post, fullUrl, headers)
+ {
+ Content = Config.Serializer.Serialize(model)
+ });
- public async Task> PostAsync(string serviceAddress, string metricName,
- string url, string rawContent, Dictionary headers = null)
- => await SendAsync(metricName,
- new HttpMessage(HttpMethod.Post, FullUri(serviceAddress, url), headers)
- {
- Content = rawContent
- });
+ public async Task> PostAsync(string serviceAddress, string metricName,
+ string url, string rawContent, Dictionary headers = null)
+ => await SendAsync(metricName,
+ new HttpMessage(HttpMethod.Post, FullUri(serviceAddress, url), headers)
+ {
+ Content = rawContent
+ });
- public async Task> PostAsync(Uri fullUrl, string metricName,
- string rawContent, Dictionary headers = null)
- => await SendAsync(metricName, new HttpMessage(HttpMethod.Post, fullUrl, headers)
- {
- Content = rawContent
- });
+ public async Task> PostAsync(Uri fullUrl, string metricName,
+ string rawContent, Dictionary headers = null)
+ => await SendAsync(metricName, new HttpMessage(HttpMethod.Post, fullUrl, headers)
+ {
+ Content = rawContent
+ });
- public async Task> PostAsync(string serviceAddress, string metricName,
- string url, Dictionary headers = null)
- => await SendAsync(metricName,
- new HttpMessage(HttpMethod.Post, FullUri(serviceAddress, url), headers));
+ public async Task> PostAsync(string serviceAddress, string metricName,
+ string url, Dictionary headers = null)
+ => await SendAsync(metricName,
+ new HttpMessage(HttpMethod.Post, FullUri(serviceAddress, url), headers));
- public async Task> PostAsync(Uri fullUrl, string metricName,
- Dictionary headers = null)
- => await SendAsync(metricName, new HttpMessage(HttpMethod.Post, fullUrl, headers));
+ public async Task> PostAsync(Uri fullUrl, string metricName,
+ Dictionary headers = null)
+ => await SendAsync(metricName, new HttpMessage(HttpMethod.Post, fullUrl, headers));
- public async Task> PostAsync(string serviceAddress, string metricName, string url,
- Dictionary headers = null)
- => await SendAsync(metricName, new HttpMessage(HttpMethod.Post, FullUri(serviceAddress, url), headers));
+ public async Task> PostAsync(string serviceAddress, string metricName, string url,
+ Dictionary headers = null)
+ => await SendAsync(metricName, new HttpMessage(HttpMethod.Post, FullUri(serviceAddress, url), headers));
- public async Task> PostAsync(Uri fullUri, string metricName,
- Dictionary headers = null)
- => await SendAsync(metricName, new HttpMessage(HttpMethod.Post, fullUri, headers));
+ public async Task> PostAsync(Uri fullUri, string metricName,
+ Dictionary headers = null)
+ => await SendAsync(metricName, new HttpMessage(HttpMethod.Post, fullUri, headers));
- public async Task> PostAsync(string serviceAddress, string metricName, string url,
- string rawContent, Dictionary headers = null)
- => await SendAsync(metricName,
- new HttpMessage(HttpMethod.Post, FullUri(serviceAddress, url), headers) { Content = rawContent });
+ public async Task> PostAsync(string serviceAddress, string metricName, string url,
+ string rawContent, Dictionary headers = null)
+ => await SendAsync(metricName,
+ new HttpMessage(HttpMethod.Post, FullUri(serviceAddress, url), headers) { Content = rawContent });
- public async Task> PostAsync(Uri fullUri, string metricName, string rawContent,
- Dictionary headers = null)
- => await SendAsync(metricName, new HttpMessage(HttpMethod.Post, fullUri, headers) { Content = rawContent });
+ public async Task> PostAsync(Uri fullUri, string metricName, string rawContent,
+ Dictionary headers = null)
+ => await SendAsync(metricName, new HttpMessage(HttpMethod.Post, fullUri, headers) { Content = rawContent });
- public async Task> PutAsync(string serviceAddress, string metricName,
- string url, TModel model, Dictionary headers = null)
- => await SendAsync(metricName,
- new HttpMessage(HttpMethod.Put, FullUri(serviceAddress, url), headers)
- {
- Content = Config.Serializer.Serialize(model)
- });
+ public async Task> PutAsync(string serviceAddress, string metricName,
+ string url, TModel model, Dictionary headers = null)
+ => await SendAsync(metricName,
+ new HttpMessage(HttpMethod.Put, FullUri(serviceAddress, url), headers)
+ {
+ Content = Config.Serializer.Serialize(model)
+ });
- public async Task> PutAsync(Uri fullUri, string metricName,
- TModel model, Dictionary headers = null)
- => await SendAsync(metricName, new HttpMessage(HttpMethod.Put, fullUri, headers)
- {
- Content = Config.Serializer.Serialize(model)
- });
+ public async Task> PutAsync(Uri fullUri, string metricName,
+ TModel model, Dictionary headers = null)
+ => await SendAsync(metricName, new HttpMessage(HttpMethod.Put, fullUri, headers)
+ {
+ Content = Config.Serializer.Serialize(model)
+ });
- public async Task> PutAsync(string serviceAddress, string metricName,
- string url, Dictionary headers = null)
- => await SendAsync(metricName,
- new HttpMessage(HttpMethod.Put, FullUri(serviceAddress, url), headers));
+ public async Task> PutAsync(string serviceAddress, string metricName,
+ string url, Dictionary headers = null)
+ => await SendAsync(metricName,
+ new HttpMessage(HttpMethod.Put, FullUri(serviceAddress, url), headers));
- public async Task> PutAsync(Uri fullUri, string metricName,
- Dictionary headers = null)
- => await SendAsync(metricName, new HttpMessage(HttpMethod.Put, fullUri, headers));
+ public async Task> PutAsync(Uri fullUri, string metricName,
+ Dictionary headers = null)
+ => await SendAsync(metricName, new HttpMessage(HttpMethod.Put, fullUri, headers));
- public async Task> PutAsync(string serviceAddress, string metricName, string url,
- Dictionary headers = null)
- => await SendAsync(metricName, new HttpMessage(HttpMethod.Put, FullUri(serviceAddress, url), headers));
+ public async Task> PutAsync(string serviceAddress, string metricName, string url,
+ Dictionary headers = null)
+ => await SendAsync(metricName, new HttpMessage(HttpMethod.Put, FullUri(serviceAddress, url), headers));
- public async Task> PutAsync(Uri fullUri, string metricName,
- Dictionary headers = null)
- => await SendAsync(metricName, new HttpMessage(HttpMethod.Put, fullUri, headers));
+ public async Task> PutAsync(Uri fullUri, string metricName,
+ Dictionary headers = null)
+ => await SendAsync(metricName, new HttpMessage(HttpMethod.Put, fullUri, headers));
- public async Task> DeleteAsync(string serviceAddress,
- string metricName, string url, TModel model, Dictionary headers = null)
- => await SendAsync(metricName,
- new HttpMessage(HttpMethod.Delete, FullUri(serviceAddress, url), headers)
- {
- Content = Config.Serializer.Serialize(model)
- });
+ public async Task> DeleteAsync(string serviceAddress,
+ string metricName, string url, TModel model, Dictionary headers = null)
+ => await SendAsync(metricName,
+ new HttpMessage(HttpMethod.Delete, FullUri(serviceAddress, url), headers)
+ {
+ Content = Config.Serializer.Serialize(model)
+ });
- public async Task> DeleteAsync(string serviceAddress, string metricName,
- string url, Dictionary headers = null)
- => await SendAsync(metricName,
- new HttpMessage(HttpMethod.Delete, FullUri(serviceAddress, url), headers));
- public async Task> DeleteAsync(Uri fullUri, string metricName,
- Dictionary headers = null)
- => await SendAsync(metricName, new HttpMessage(HttpMethod.Delete, fullUri, headers));
+ public async Task> DeleteAsync(string serviceAddress, string metricName,
+ string url, Dictionary headers = null)
+ => await SendAsync(metricName,
+ new HttpMessage(HttpMethod.Delete, FullUri(serviceAddress, url), headers));
+ public async Task> DeleteAsync(Uri fullUri, string metricName,
+ Dictionary headers = null)
+ => await SendAsync(metricName, new HttpMessage(HttpMethod.Delete, fullUri, headers));
- public async Task> DeleteAsync(string serviceAddress, string metricName, string url,
- Dictionary headers = null)
- => await SendAsync(metricName, new HttpMessage(HttpMethod.Delete, FullUri(serviceAddress, url), headers));
+ public async Task> DeleteAsync(string serviceAddress, string metricName, string url,
+ Dictionary headers = null)
+ => await SendAsync(metricName, new HttpMessage(HttpMethod.Delete, FullUri(serviceAddress, url), headers));
- public async Task> DeleteAsync(Uri fullUri, string metricName,
- Dictionary headers = null)
- => await SendAsync(metricName, new HttpMessage(HttpMethod.Delete, fullUri, headers));
+ public async Task> DeleteAsync(Uri fullUri, string metricName,
+ Dictionary headers = null)
+ => await SendAsync(metricName, new HttpMessage(HttpMethod.Delete, fullUri, headers));
- public async Task> PatchAsync(string serviceAddress,
- string metricName,
- string url, TModel model, Dictionary headers = null)
- => await SendAsync(metricName,
- new HttpMessage(HttpMethod.Patch, FullUri(serviceAddress, url), headers)
- {
- Content = Config.Serializer.Serialize(model)
- });
+ public async Task> PatchAsync(string serviceAddress,
+ string metricName,
+ string url, TModel model, Dictionary headers = null)
+ => await SendAsync(metricName,
+ new HttpMessage(HttpMethod.Patch, FullUri(serviceAddress, url), headers)
+ {
+ Content = Config.Serializer.Serialize(model)
+ });
- public async Task> PatchAsync(Uri fullUri, string metricName,
- TModel model, Dictionary headers = null)
- => await SendAsync(metricName, new HttpMessage(HttpMethod.Patch, fullUri, headers)
- {
- Content = Config.Serializer.Serialize(model)
- });
+ public async Task> PatchAsync(Uri fullUri, string metricName,
+ TModel model, Dictionary headers = null)
+ => await SendAsync(metricName, new HttpMessage(HttpMethod.Patch, fullUri, headers)
+ {
+ Content = Config.Serializer.Serialize(model)
+ });
- public async Task> PatchAsync(string serviceAddress, string metricName,
- string url, Dictionary headers = null)
- => await SendAsync(metricName,
- new HttpMessage(HttpMethod.Patch, FullUri(serviceAddress, url), headers));
+ public async Task> PatchAsync(string serviceAddress, string metricName,
+ string url, Dictionary headers = null)
+ => await SendAsync(metricName,
+ new HttpMessage(HttpMethod.Patch, FullUri(serviceAddress, url), headers));
- public async Task> PatchAsync(Uri fullUri, string metricName,
- Dictionary headers = null)
- => await SendAsync(metricName, new HttpMessage(HttpMethod.Patch, fullUri, headers));
+ public async Task> PatchAsync(Uri fullUri, string metricName,
+ Dictionary headers = null)
+ => await SendAsync(metricName, new HttpMessage(HttpMethod.Patch, fullUri, headers));
- public async Task> PatchAsync(string serviceAddress, string metricName,
- string url, TModel model, Dictionary headers = null)
- => await SendAsync(metricName,
- new HttpMessage(HttpMethod.Patch, FullUri(serviceAddress, url), headers)
- {
- Content = Config.Serializer.Serialize(model)
- });
+ public async Task> PatchAsync(string serviceAddress, string metricName,
+ string url, TModel model, Dictionary headers = null)
+ => await SendAsync(metricName,
+ new HttpMessage(HttpMethod.Patch, FullUri(serviceAddress, url), headers)
+ {
+ Content = Config.Serializer.Serialize(model)
+ });
- public async Task> PatchAsync(Uri fullUri, string metricName,
- TModel model, Dictionary headers = null)
- => await SendAsync(metricName, new HttpMessage(HttpMethod.Patch, fullUri, headers)
- {
- Content = Config.Serializer.Serialize(model)
- });
+ public async Task> PatchAsync(Uri fullUri, string metricName,
+ TModel model, Dictionary headers = null)
+ => await SendAsync(metricName, new HttpMessage(HttpMethod.Patch, fullUri, headers)
+ {
+ Content = Config.Serializer.Serialize(model)
+ });
- public async Task> PatchAsync(string serviceAddress, string metricName, string url,
- Dictionary headers = null)
- => await SendAsync(metricName, new HttpMessage(HttpMethod.Patch, FullUri(serviceAddress, url), headers));
+ public async Task> PatchAsync(string serviceAddress, string metricName, string url,
+ Dictionary headers = null)
+ => await SendAsync(metricName, new HttpMessage(HttpMethod.Patch, FullUri(serviceAddress, url), headers));
- public async Task> PatchAsync(Uri fullUri, string metricName,
- Dictionary headers = null)
- => await SendAsync(metricName, new HttpMessage(HttpMethod.Patch, fullUri, headers));
+ public async Task> PatchAsync(Uri fullUri, string metricName,
+ Dictionary headers = null)
+ => await SendAsync(metricName, new HttpMessage(HttpMethod.Patch, fullUri, headers));
- public async Task>> SendAsync(Uri fullUri,
- string metricName, TModel model,
- Dictionary headers = null,
- HttpMethod method = null)
+
+ public async Task>> SendAsync(Uri fullUri,
+ string metricName, TModel model,
+ Dictionary headers = null,
+ HttpMethod method = null)
+ {
+ try
{
- try
+ if (fullUri == null)
+ return new OperationResult>(ActionStatus.InternalServerError,
+ "Адрес сообщения не указан (fullUri==null)");
+
+ var message = new HttpMessage(method ?? HttpMethod.Put, fullUri, headers)
{
- if (fullUri == null)
- return new OperationResult>(ActionStatus.InternalServerError,
- "Адрес сообщения не указан (fullUri==null)");
+ Content = model != null ? Config.Serializer.Serialize(model) : ""
+ };
- var message = new HttpMessage(method ?? HttpMethod.Put, fullUri, headers)
- {
- Content = model != null ? Config.Serializer.Serialize(model) : ""
- };
+ using var requestMessage = message.ToRequestMessage(Config);
- using (_metricsFactory.CreateMetricsTimer(metricName, message.Content))
- {
- using var requestMessage = message.ToRequestMessage(Config);
-
- using var responseMessage = await _httpClient.SendAsync(requestMessage);
- var responseContent = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
-
- if (!responseMessage.IsSuccessStatusCode)
- {
- var logMessage = string.Format(LogMessageTemplate, Config.ServiceName, message.Method,
- message.FullUri, responseMessage.StatusCode);
-
- var logLevel = responseMessage.StatusCode == HttpStatusCode.InternalServerError
- ? _logLevelOverride(LogLevel.Error)
- : _logLevelOverride(LogLevel.Warn);
- _logger.LogWithObject(logLevel, ex: null, logMessage, logObjects: responseContent);
- }
-
- var result = new HttpResponseMessage
- {
- StatusCode = responseMessage.StatusCode,
- Headers = responseMessage.Headers,
- TrailingHeaders = responseMessage.TrailingHeaders,
- ReasonPhrase = responseMessage.ReasonPhrase,
- Version = responseMessage.Version,
- RawContent = responseContent
- };
-
- try
- {
- result.Content = !string.IsNullOrEmpty(result.RawContent)
- ? Config.Serializer.Deserialize(result.RawContent)
- : default;
- }
- catch (TaskCanceledException e) when (e.InnerException is TimeoutException)
- {
- _logger.LogWithObject(_logLevelOverride(LogLevel.Warn),
- e,
- logObjects: new
- {
- MetricName = metricName, FullUri = fullUri, Model = model,
- Headers = headers
- });
- return new OperationResult>(ActionStatus.Timeout);
- }
- catch (Exception e)
- {
- _logger.LogWithObject(_logLevelOverride(LogLevel.Error),
- e,
- logObjects:
- new
- {
- MetricName = metricName, FullUri = fullUri, Model = model,
- Headers = headers,
- ResponseBody = result.RawContent
- });
- }
-
- return new(result, OperationResult.GetActionStatusByHttpStatusCode(result.StatusCode));
- }
+ using var responseMessage = await _httpClient.SendAsync(requestMessage);
+ var responseContent = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
+
+ if (!responseMessage.IsSuccessStatusCode)
+ {
+ var logMessage = string.Format(LogMessageTemplate, Config.ServiceName, message.Method,
+ message.FullUri, responseMessage.StatusCode);
+
+ var logLevel = responseMessage.StatusCode == HttpStatusCode.InternalServerError
+ ? _logLevelOverride(LogLevel.Error)
+ : _logLevelOverride(LogLevel.Warn);
+ _logger.LogWithObject(logLevel, ex: null, logMessage, logObjects: responseContent);
+ }
+
+ var result = new HttpResponseMessage
+ {
+ StatusCode = responseMessage.StatusCode,
+ Headers = responseMessage.Headers,
+ TrailingHeaders = responseMessage.TrailingHeaders,
+ ReasonPhrase = responseMessage.ReasonPhrase,
+ Version = responseMessage.Version,
+ RawContent = responseContent
+ };
+
+ try
+ {
+ result.Content = !string.IsNullOrEmpty(result.RawContent)
+ ? Config.Serializer.Deserialize(result.RawContent)
+ : default;
+ }
+ catch (TaskCanceledException e) when (e.InnerException is TimeoutException)
+ {
+ _logger.LogWithObject(_logLevelOverride(LogLevel.Warn),
+ e,
+ logObjects: new
+ {
+ MetricName = metricName, FullUri = fullUri, Model = model,
+ Headers = headers
+ });
+ return new OperationResult>(ActionStatus.Timeout);
}
catch (Exception e)
{
_logger.LogWithObject(_logLevelOverride(LogLevel.Error),
- e,
- logObjects: new { MetricName = metricName, FullUri = fullUri, Model = model, Headers = headers });
- return new OperationResult>(e);
+ e,
+ logObjects:
+ new
+ {
+ MetricName = metricName, FullUri = fullUri, Model = model,
+ Headers = headers,
+ ResponseBody = result.RawContent
+ });
}
+
+ return new(result, OperationResult.GetActionStatusByHttpStatusCode(result.StatusCode));
+ }
+ catch (Exception e)
+ {
+ _logger.LogWithObject(_logLevelOverride(LogLevel.Error),
+ e,
+ logObjects: new { MetricName = metricName, FullUri = fullUri, Model = model, Headers = headers });
+ return new OperationResult>(e);
}
+ }
- private async Task> SendAsync(string methodName, HttpMessage message)
+ private async Task> SendAsync