-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
899 additions
and
315 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System.Net; | ||
using Serilog; | ||
|
||
namespace CAServer.Nightingale; | ||
|
||
public static class HostHelper | ||
{ | ||
/// <summary> | ||
/// note: Failure will return "". | ||
/// </summary> | ||
/// <returns></returns> | ||
public static string GetLocalHostName() | ||
{ | ||
try | ||
{ | ||
return Dns.GetHostName(); | ||
} | ||
catch (Exception e) | ||
{ | ||
Log.Logger.Error(e, "get host name error."); | ||
} | ||
|
||
return string.Empty; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using Orleans.Runtime; | ||
|
||
namespace CAServer.Nightingale; | ||
|
||
public interface IN9EClient | ||
{ | ||
//IMetricTelemetryConsumer | ||
public void TrackMetric(string name, double value, IDictionary<string, string>? properties = null); | ||
|
||
public void TrackMetric(string name, TimeSpan value, IDictionary<string, string> properties = null); | ||
|
||
public void IncrementMetric(string name); | ||
|
||
public void IncrementMetric(string name, double value); | ||
|
||
public void DecrementMetric(string name); | ||
|
||
public void DecrementMetric(string name, double value); | ||
|
||
//ITraceTelemetryConsumer | ||
public void TrackTrace(string message); | ||
|
||
public void TrackTrace(string message, Severity severity); | ||
|
||
public void TrackTrace(string message, Severity severity, IDictionary<string, string> properties); | ||
|
||
public void TrackTrace(string message, IDictionary<string, string> properties); | ||
|
||
//IRequestTelemetryConsumer | ||
public void TrackRequest(string name, DateTimeOffset startTime, TimeSpan duration, string responseCode, | ||
bool success); | ||
|
||
//IDependencyTelemetryConsumer | ||
public void TrackDependency(string dependencyName, string commandName, DateTimeOffset startTime, TimeSpan duration, | ||
bool success); | ||
|
||
//IExceptionTelemetryConsumer | ||
public void TrackException(Exception exception, IDictionary<string, string> properties = null, IDictionary<string, double> metrics = null); | ||
|
||
//IEventTelemetryConsumer | ||
public void TrackEvent(string eventName, IDictionary<string, string> properties = null, | ||
IDictionary<string, double> metrics = null); | ||
|
||
public bool IsEnabled(); | ||
|
||
public void Flush(); | ||
|
||
public void Close(); | ||
} |
209 changes: 209 additions & 0 deletions
209
src/CAServer.Nightingale/Logging/N9EClientForLogging.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
using Microsoft.Extensions.Options; | ||
using Orleans.Runtime; | ||
using Serilog; | ||
|
||
namespace CAServer.Nightingale.Logging; | ||
|
||
public class N9EClientForLogging : IN9EClient | ||
{ | ||
private readonly ILogger _logger; | ||
private readonly IOptionsMonitor<N9EClientForLoggingOptions> _n9EClientForLoggingOptions; | ||
private readonly string _hostName; | ||
|
||
public N9EClientForLogging(IServiceProvider serviceProvider, | ||
IOptionsMonitor<N9EClientForLoggingOptions> n9EClientForLoggingOptions) | ||
{ | ||
_n9EClientForLoggingOptions = n9EClientForLoggingOptions; | ||
_logger = N9EClientLogHelper.CreateLogger(n9EClientForLoggingOptions); | ||
_hostName = HostHelper.GetLocalHostName(); | ||
} | ||
|
||
public void TrackMetric(string name, double value, IDictionary<string, string>? properties = null) | ||
{ | ||
TrackMetric(FormatMetric(name, value, properties)); | ||
} | ||
|
||
public void TrackMetric(string name, TimeSpan value, IDictionary<string, string> properties = null) | ||
{ | ||
TrackMetric(FormatMetric(name, value, properties)); | ||
} | ||
|
||
public void IncrementMetric(string name) | ||
{ | ||
TrackMetric(FormatMetric(name, 1, null)); | ||
} | ||
|
||
public void IncrementMetric(string name, double value) | ||
{ | ||
TrackMetric(FormatMetric(name, value, null)); | ||
} | ||
|
||
public void DecrementMetric(string name) | ||
{ | ||
TrackMetric(FormatMetric(name, -1, null)); | ||
} | ||
|
||
public void DecrementMetric(string name, double value) | ||
{ | ||
TrackMetric(FormatMetric(name, value * -1, null)); | ||
} | ||
|
||
public void TrackTrace(string message) | ||
{ | ||
//Doing nothing | ||
} | ||
|
||
public void TrackTrace(string message, Severity severity) | ||
{ | ||
//Doing nothing | ||
} | ||
|
||
public void TrackTrace(string message, Severity severity, IDictionary<string, string> properties) | ||
{ | ||
//Doing nothing | ||
} | ||
|
||
public void TrackTrace(string message, IDictionary<string, string> properties) | ||
{ | ||
//Doing nothing | ||
} | ||
|
||
public void TrackRequest(string name, DateTimeOffset startTime, TimeSpan duration, string responseCode, | ||
bool success) | ||
{ | ||
TrackMetric(FormatMetric(name, duration, new Dictionary<string, string>() | ||
{ | ||
{ N9EClientConstant.LabelStartTime, startTime.Millisecond.ToString() }, | ||
{ N9EClientConstant.LabelResponseCode, responseCode }, | ||
{ N9EClientConstant.LabelSuccess, success.ToString() } | ||
})); | ||
} | ||
|
||
public void TrackDependency(string dependencyName, string commandName, DateTimeOffset startTime, TimeSpan duration, | ||
bool success) | ||
{ | ||
TrackMetric(FormatMetric(dependencyName, duration, new Dictionary<string, string>() | ||
{ | ||
{ N9EClientConstant.LabelCommandName, commandName }, | ||
{ N9EClientConstant.LabelStartTime, startTime.Millisecond.ToString() }, | ||
{ N9EClientConstant.LabelSuccess, success.ToString() } | ||
})); | ||
} | ||
|
||
public void TrackException(Exception exception, IDictionary<string, string> properties = null, | ||
IDictionary<string, double> metrics = null) | ||
{ | ||
if (metrics != null && metrics.Count > 0) | ||
{ | ||
foreach (var metric in metrics) | ||
{ | ||
var dictionary = properties == null | ||
? new Dictionary<string, string>() | ||
: new Dictionary<string, string>(properties); | ||
dictionary.Add(N9EClientConstant.LabelChart, metric.Key); | ||
dictionary.Add(N9EClientConstant.LabelMessage, exception.Message.ToString()); | ||
TrackMetric(FormatMetric(N9EClientConstant.MetricExceptions, metric.Value, dictionary)); | ||
} | ||
} | ||
else | ||
{ | ||
var dictionary = properties == null | ||
? new Dictionary<string, string>() | ||
: new Dictionary<string, string>(properties); | ||
dictionary.Add(N9EClientConstant.LabelMessage, exception.Message.ToString()); | ||
TrackMetric(FormatMetric(N9EClientConstant.MetricExceptions, 1, dictionary)); | ||
} | ||
} | ||
|
||
public void TrackEvent(string eventName, IDictionary<string, string> properties = null, | ||
IDictionary<string, double> metrics = null) | ||
{ | ||
if (metrics != null && metrics.Count > 0) | ||
{ | ||
foreach (var metric in metrics) | ||
{ | ||
var dictionary = properties == null | ||
? new Dictionary<string, string>() | ||
: new Dictionary<string, string>(properties); | ||
dictionary.Add(N9EClientConstant.LabelChart, metric.Key); | ||
TrackMetric(FormatMetric(eventName, metric.Value, dictionary)); | ||
} | ||
} | ||
else | ||
{ | ||
TrackMetric(FormatMetric(eventName, 1, properties)); | ||
} | ||
} | ||
|
||
public bool IsEnabled() | ||
{ | ||
return !_n9EClientForLoggingOptions.CurrentValue.DisableLogging; | ||
} | ||
|
||
public void Flush() | ||
{ | ||
} | ||
|
||
public void Close() | ||
{ | ||
} | ||
|
||
private string FormatMetric(string? name, int value, IDictionary<string, string>? properties) | ||
{ | ||
return $"{FormatMetricName(name)}{{{FormatMetricLabel(properties)}}} {value}"; | ||
} | ||
|
||
private string FormatMetric(string? name, long value, IDictionary<string, string>? properties) | ||
{ | ||
return $"{FormatMetricName(name)}{{{FormatMetricLabel(properties)}}} {value}"; | ||
} | ||
|
||
private string FormatMetric(string? name, double value, IDictionary<string, string>? properties) | ||
{ | ||
return $"{FormatMetricName(name)}{{{FormatMetricLabel(properties)}}} {value}"; | ||
} | ||
|
||
private string FormatMetric(string? name, TimeSpan value, IDictionary<string, string>? properties) | ||
{ | ||
return $"{FormatMetricName(name)}{{{FormatMetricLabel(properties)}}} {value}"; | ||
} | ||
|
||
private string FormatMetricName(string? name) | ||
{ | ||
if (string.IsNullOrWhiteSpace(name)) | ||
{ | ||
return string.Empty; | ||
} | ||
|
||
var formatMetricName = name.Replace(".", "_"); | ||
if (formatMetricName.Length > _n9EClientForLoggingOptions.CurrentValue.MetricNameMaxLength) | ||
{ | ||
formatMetricName = formatMetricName[.._n9EClientForLoggingOptions.CurrentValue.MetricNameMaxLength]; | ||
} | ||
|
||
return formatMetricName; | ||
} | ||
|
||
private string FormatMetricLabel(IDictionary<string, string>? properties) | ||
{ | ||
properties = properties != null && properties.Count > 0 | ||
? new Dictionary<string, string>(properties) | ||
: new Dictionary<string, string>(); | ||
properties.Add(N9EClientConstant.LabelHostName, _hostName); | ||
properties.Add(N9EClientConstant.LabelTimestamp, DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString()); | ||
var labelStr = string.Empty; | ||
properties.Aggregate(labelStr, | ||
(current, property) => current + $"{property.Key}=\"{property.Value}\""); | ||
return labelStr; | ||
} | ||
|
||
private void TrackMetric(string metric) | ||
{ | ||
if (_n9EClientForLoggingOptions.CurrentValue.DisableLogging) | ||
{ | ||
return; | ||
} | ||
|
||
_logger.Warning(metric); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/CAServer.Nightingale/Logging/N9EClientForLoggingOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace CAServer.Nightingale.Logging; | ||
|
||
public class N9EClientForLoggingOptions | ||
{ | ||
public bool DisableLogging { get; set; } = false; | ||
|
||
public string LogFilePathFormat { get; set; } = "./telemetry/trace-.log"; | ||
|
||
public int LogRetainedFileCountLimit { get; set; } = 5; | ||
|
||
public int MetricNameMaxLength { get; set; } = 100; | ||
} |
18 changes: 10 additions & 8 deletions
18
...lemetryConsumers.Nightingale/LogHelper.cs → ...Nightingale/Logging/N9EClientLogHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.