Skip to content

Commit

Permalink
Add metrics exception logs (#84)
Browse files Browse the repository at this point in the history
* add logs in MetricsTimer

* add logs in HttpContextHelper

---------

Co-authored-by: Станислав Терещенков <tereschenkov.s@ati.su>
  • Loading branch information
CptnSnail and Станислав Терещенков committed Jun 19, 2024
1 parent 18b0439 commit 0904bc9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
7 changes: 6 additions & 1 deletion ATI.Services.Common/Http/HttpContextHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using ATI.Services.Common.Logging;
using ATI.Services.Common.Metrics;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;
using NLog;

namespace ATI.Services.Common.Http;

internal static class HttpContextHelper
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

public static string[] MetricsHeadersValues(HttpContext? httpContext) => GetHeadersValues(httpContext, MetricsLabelsAndHeaders.UserHeaders);
public static Dictionary<string, string> HeadersAndValuesToProxy(HttpContext? httpContext,IReadOnlyCollection<string>? headersToProxy) => GetHeadersAndValues(httpContext, headersToProxy);

Expand All @@ -24,8 +28,9 @@ private static string[] GetHeadersValues(HttpContext? httpContext, IReadOnlyColl
var headersValues = headersNames.Select(label => GetHeaderValue(httpContext, label)).ToArray();
return headersValues;
}
catch (ObjectDisposedException) // when thing happen outside http ctx e.g eventbus event handler
catch (ObjectDisposedException ex) // when thing happen outside http ctx e.g eventbus event handler
{
Logger.ErrorWithObject(ex, headersNames);
return Array.Empty<string>();
}
}
Expand Down
45 changes: 26 additions & 19 deletions ATI.Services.Common/Metrics/MetricsTimer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,31 +57,38 @@ public void Stop()

public void Dispose()
{
if (_summary == null)
try
{
return;
}
if (_summary == null)
{
return;
}

if (_summaryLabels == null)
{
_summary.Observe(_stopwatch.ElapsedMilliseconds);
}
else
{
_summary.Labels(_summaryLabels).Observe(_stopwatch.ElapsedMilliseconds);
}
if (_summaryLabels == null)
{
_summary.Observe(_stopwatch.ElapsedMilliseconds);
}
else
{
_summary.Labels(_summaryLabels).Observe(_stopwatch.ElapsedMilliseconds);
}

_stopwatch.Stop();
_stopwatch.Stop();

if (_longRequestTime == null
|| !(_stopwatch.Elapsed > _longRequestTime)
|| _context == null
|| _logSource == null)
{
return;
}

if (_longRequestTime == null
|| !(_stopwatch.Elapsed > _longRequestTime)
|| _context == null
|| _logSource == null)
Logger.LogWithObject(LogLevel.Warn, null, "Long request WARN.", GetContext());
}
catch (Exception ex)
{
return;
Logger.ErrorWithObject(ex, new { _summary?.LabelNames, _summaryLabels, _logSource, MetricsLabelsAndHeaders.UserHeaders });
}

Logger.LogWithObject(LogLevel.Warn, null, "Long request WARN.", GetContext());
}

private Dictionary<object, object> GetContext()
Expand Down

0 comments on commit 0904bc9

Please sign in to comment.