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 (#154)

* Add opt out to filtering the properties list when using propertiesAsLabels

Workaround for #138

* Added missing XML comment
  • Loading branch information
EraYaN authored Nov 25, 2022
1 parent b8cf3a9 commit 0f58802
Show file tree
Hide file tree
Showing 2 changed files with 16 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
11 changes: 9 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 @@ -52,16 +54,21 @@ internal class LokiBatchFormatter : ILokiBatchFormatter
/// <param name="useInternalTimestamp">
/// Compute internal timestamp
/// </param>
/// <param name="leavePropertiesIntact">
/// Leave the list of properties intact after extracting the labels specified in propertiesAsLabels.
/// </param>
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 +221,6 @@ private void GenerateEntry(
}

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

0 comments on commit 0f58802

Please sign in to comment.