Skip to content

Commit

Permalink
added default log-label provider
Browse files Browse the repository at this point in the history
  • Loading branch information
jincod committed Aug 23, 2020
1 parent 081598d commit 01e980f
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/Serilog.Sinks.Loki.Example/LogLabelProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ public IList<LokiLabel> GetLabels()
new LokiLabel("namespace", "prod")
};
}

public bool PropertiesAsLabels { get; set; } = false;
}
}
14 changes: 14 additions & 0 deletions src/Serilog.Sinks.Loki/Labels/DefaultLogLabelProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Collections.Generic;

namespace Serilog.Sinks.Loki.Labels
{
class DefaultLogLabelProvider : ILogLabelProvider
{
public IList<LokiLabel> GetLabels()
{
return new List<LokiLabel>();
}

public bool PropertiesAsLabels { get; set; } = true;
}
}
1 change: 1 addition & 0 deletions src/Serilog.Sinks.Loki/Labels/ILogLabelProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ namespace Serilog.Sinks.Loki.Labels
public interface ILogLabelProvider
{
IList<LokiLabel> GetLabels();
bool PropertiesAsLabels { get; set; }
}
}
32 changes: 22 additions & 10 deletions src/Serilog.Sinks.Loki/LokiBatchFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ namespace Serilog.Sinks.Loki
internal class LokiBatchFormatter : IBatchFormatter
{
private readonly IList<LokiLabel> _globalLabels;
private readonly bool _propertiesAsLabels;

public LokiBatchFormatter()
{
_globalLabels = new List<LokiLabel>();
}

public LokiBatchFormatter(IList<LokiLabel> globalLabels)
public LokiBatchFormatter(ILogLabelProvider logLabelProvider)
{
_globalLabels = globalLabels;
_globalLabels = logLabelProvider.GetLabels();
_propertiesAsLabels = logLabelProvider.PropertiesAsLabels;
}

public void Format(IEnumerable<LogEvent> logEvents, ITextFormatter formatter, TextWriter output)
Expand All @@ -46,14 +48,6 @@ public void Format(IEnumerable<LogEvent> logEvents, ITextFormatter formatter, Te
foreach (LokiLabel globalLabel in _globalLabels)
stream.Labels.Add(new LokiLabel(globalLabel.Key, globalLabel.Value));

foreach (KeyValuePair<string, LogEventPropertyValue> property in logEvent.Properties)
// Some enrichers pass strings with quotes surrounding the values inside the string,
// which results in redundant quotes after serialization and a "bad request" response.
// To avoid this, remove all quotes from the value.
// We also remove any \r\n newlines and replace with \n new lines to prevent "bad request" responses
// We also remove backslashes and replace with forward slashes, Loki doesn't like those either
stream.Labels.Add(new LokiLabel(property.Key, property.Value.ToString().Replace("\"", "").Replace("\r\n", "\n").Replace("\\", "/")));

var time = logEvent.Timestamp.ToString("o");

var sb = new StringBuilder();
Expand All @@ -69,6 +63,24 @@ public void Format(IEnumerable<LogEvent> logEvents, ITextFormatter formatter, Te
}
}

foreach (KeyValuePair<string, LogEventPropertyValue> property in logEvent.Properties)
{
// Some enrichers pass strings with quotes surrounding the values inside the string,
// which results in redundant quotes after serialization and a "bad request" response.
// To avoid this, remove all quotes from the value.
// We also remove any \r\n newlines and replace with \n new lines to prevent "bad request" responses
// We also remove backslashes and replace with forward slashes, Loki doesn't like those either
var propertyValue = property.Value.ToString().Replace("\"", "").Replace("\r\n", "\n").Replace("\\", "/");
if (_propertiesAsLabels)
{
stream.Labels.Add(new LokiLabel(property.Key, propertyValue));
}
else
{
sb.Append($" {property.Key}={propertyValue}");
}
}

// Loki doesn't like \r\n for new line, and we can't guarantee the message doesn't have any
// in it, so we replace \r\n with \n on the final message
// We also flip backslashes to forward slashes, Loki doesn't like those either.
Expand Down
2 changes: 1 addition & 1 deletion src/Serilog.Sinks.Loki/LokiSinkExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static LoggerConfiguration LokiHttp(this LoggerSinkConfiguration sinkConf

private static LoggerConfiguration LokiHttpImpl(this LoggerSinkConfiguration sinkConfiguration, LokiCredentials credentials, ILogLabelProvider logLabelProvider, IHttpClient httpClient)
{
var formatter = logLabelProvider != null ? new LokiBatchFormatter(logLabelProvider.GetLabels()) : new LokiBatchFormatter();
var formatter = new LokiBatchFormatter(logLabelProvider ?? new DefaultLogLabelProvider());

var client = httpClient ?? new LokiHttpClient();
if (client is LokiHttpClient c)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ public IList<LokiLabel> GetLabels()
new LokiLabel("app", "tests")
};
}

public bool PropertiesAsLabels { get; set; } = false;
}
}

0 comments on commit 01e980f

Please sign in to comment.