From 0f588025947816b7352fbfe412195ac817a1db45 Mon Sep 17 00:00:00 2001
From: Erwin de Haan <1627021+EraYaN@users.noreply.github.com>
Date: Fri, 25 Nov 2022 15:53:11 +0100
Subject: [PATCH] Add opt out to filtering the properties list when using
propertiesAsLabels (#154)
* Add opt out to filtering the properties list when using propertiesAsLabels
Workaround for #138
* Added missing XML comment
---
.../LoggerConfigurationLokiExtensions.cs | 9 +++++++--
src/Serilog.Sinks.Grafana.Loki/LokiBatchFormatter.cs | 11 +++++++++--
2 files changed, 16 insertions(+), 4 deletions(-)
diff --git a/src/Serilog.Sinks.Grafana.Loki/LoggerConfigurationLokiExtensions.cs b/src/Serilog.Sinks.Grafana.Loki/LoggerConfigurationLokiExtensions.cs
index ccefb19..3b9d98a 100644
--- a/src/Serilog.Sinks.Grafana.Loki/LoggerConfigurationLokiExtensions.cs
+++ b/src/Serilog.Sinks.Grafana.Loki/LoggerConfigurationLokiExtensions.cs
@@ -73,6 +73,9 @@ public static class LoggerConfigurationLokiExtensions
///
/// Should use internal sink timestamp instead of application one to use as log timestamp.
///
+ ///
+ /// Leaves the list of properties intact after extracting the labels specified in propertiesAsLabels.
+ ///
/// Logger configuration, allowing configuration to continue.
public static LoggerConfiguration GrafanaLoki(
this LoggerSinkConfiguration sinkConfiguration,
@@ -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)
{
@@ -105,7 +109,8 @@ public static LoggerConfiguration GrafanaLoki(
reservedPropertyRenamingStrategy,
labels,
propertiesAsLabels,
- useInternalTimestamp);
+ useInternalTimestamp,
+ leavePropertiesIntact);
var sink = new LokiSink(
LokiRoutesBuilder.BuildLogsEntriesRoute(uri),
diff --git a/src/Serilog.Sinks.Grafana.Loki/LokiBatchFormatter.cs b/src/Serilog.Sinks.Grafana.Loki/LokiBatchFormatter.cs
index 3b8daf7..bade389 100644
--- a/src/Serilog.Sinks.Grafana.Loki/LokiBatchFormatter.cs
+++ b/src/Serilog.Sinks.Grafana.Loki/LokiBatchFormatter.cs
@@ -34,6 +34,8 @@ internal class LokiBatchFormatter : ILokiBatchFormatter
private readonly IEnumerable _globalLabels;
private readonly IReservedPropertyRenamingStrategy _renamingStrategy;
private readonly IEnumerable _propertiesAsLabels;
+
+ private readonly bool _leavePropertiesIntact;
private readonly bool _useInternalTimestamp;
///
@@ -52,16 +54,21 @@ internal class LokiBatchFormatter : ILokiBatchFormatter
///
/// Compute internal timestamp
///
+ ///
+ /// Leave the list of properties intact after extracting the labels specified in propertiesAsLabels.
+ ///
public LokiBatchFormatter(
IReservedPropertyRenamingStrategy renamingStrategy,
IEnumerable? globalLabels = null,
IEnumerable? propertiesAsLabels = null,
- bool useInternalTimestamp = false)
+ bool useInternalTimestamp = false,
+ bool leavePropertiesIntact = false)
{
_renamingStrategy = renamingStrategy;
_globalLabels = globalLabels ?? Enumerable.Empty();
_propertiesAsLabels = propertiesAsLabels ?? Enumerable.Empty();
_useInternalTimestamp = useInternalTimestamp;
+ _leavePropertiesIntact = leavePropertiesIntact;
}
///
@@ -214,6 +221,6 @@ private void GenerateEntry(
}
return (labels,
- lokiLogEvent.CopyWithProperties(remainingProperties));
+ lokiLogEvent.CopyWithProperties(_leavePropertiesIntact ? properties : remainingProperties));
}
}
\ No newline at end of file