Skip to content

Commit

Permalink
Add opt out to filtering the properties list when using propertiesAsL…
Browse files Browse the repository at this point in the history
…abels

Workaround for serilog-contrib#138
  • Loading branch information
EraYaN committed Nov 18, 2022
1 parent 0a577e5 commit 791eafe
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ public static class LoggerConfigurationLokiExtensions
/// <param name="useInternalTimestamp">
/// Should use internal sink timestamp instead of application one to use as log timestamp.
/// </param>
/// <param name="leavePropertiesIntact">
/// Leaves the list of properties intact after extracting the labels specified in propertiesAsLabels.
/// </param>
/// <returns>Logger configuration, allowing configuration to continue.</returns>
public static LoggerConfiguration GrafanaLoki(
this LoggerSinkConfiguration sinkConfiguration,
Expand All @@ -87,7 +90,8 @@ public static LoggerConfiguration GrafanaLoki(
ITextFormatter? textFormatter = null,
ILokiHttpClient? httpClient = null,
IReservedPropertyRenamingStrategy? reservedPropertyRenamingStrategy = null,
bool useInternalTimestamp = false)
bool useInternalTimestamp = false,
bool leavePropertiesIntact = false)
{
if (sinkConfiguration == null)
{
Expand All @@ -105,7 +109,8 @@ public static LoggerConfiguration GrafanaLoki(
reservedPropertyRenamingStrategy,
labels,
propertiesAsLabels,
useInternalTimestamp);
useInternalTimestamp,
leavePropertiesIntact);

var sink = new LokiSink(
LokiRoutesBuilder.BuildLogsEntriesRoute(uri),
Expand Down
8 changes: 6 additions & 2 deletions src/Serilog.Sinks.Grafana.Loki/LokiBatchFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ internal class LokiBatchFormatter : ILokiBatchFormatter
private readonly IEnumerable<LokiLabel> _globalLabels;
private readonly IReservedPropertyRenamingStrategy _renamingStrategy;
private readonly IEnumerable<string> _propertiesAsLabels;

private readonly bool _leavePropertiesIntact;
private readonly bool _useInternalTimestamp;

/// <summary>
Expand All @@ -56,12 +58,14 @@ public LokiBatchFormatter(
IReservedPropertyRenamingStrategy renamingStrategy,
IEnumerable<LokiLabel>? globalLabels = null,
IEnumerable<string>? propertiesAsLabels = null,
bool useInternalTimestamp = false)
bool useInternalTimestamp = false,
bool leavePropertiesIntact = false)
{
_renamingStrategy = renamingStrategy;
_globalLabels = globalLabels ?? Enumerable.Empty<LokiLabel>();
_propertiesAsLabels = propertiesAsLabels ?? Enumerable.Empty<string>();
_useInternalTimestamp = useInternalTimestamp;
_leavePropertiesIntact = leavePropertiesIntact;
}

/// <summary>
Expand Down Expand Up @@ -214,6 +218,6 @@ private void GenerateEntry(
}

return (labels,
lokiLogEvent.CopyWithProperties(remainingProperties));
lokiLogEvent.CopyWithProperties(_leavePropertiesIntact ? properties : remainingProperties));
}
}

0 comments on commit 791eafe

Please sign in to comment.