From 791eafea8189108f6adbc1eeb5f8013bff5baded Mon Sep 17 00:00:00 2001 From: Erwin de Haan Date: Fri, 18 Nov 2022 12:44:39 +0100 Subject: [PATCH 1/2] Add opt out to filtering the properties list when using propertiesAsLabels Workaround for #138 --- .../LoggerConfigurationLokiExtensions.cs | 9 +++++++-- src/Serilog.Sinks.Grafana.Loki/LokiBatchFormatter.cs | 8 ++++++-- 2 files changed, 13 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..20c9db8 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; /// @@ -56,12 +58,14 @@ 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 +218,6 @@ private void GenerateEntry( } return (labels, - lokiLogEvent.CopyWithProperties(remainingProperties)); + lokiLogEvent.CopyWithProperties(_leavePropertiesIntact ? properties : remainingProperties)); } } \ No newline at end of file From 5ef9cc0799e569f8044ba8ed8e3c4b566a71ca28 Mon Sep 17 00:00:00 2001 From: Erwin de Haan Date: Fri, 25 Nov 2022 13:44:20 +0100 Subject: [PATCH 2/2] Added missing XML comment --- src/Serilog.Sinks.Grafana.Loki/LokiBatchFormatter.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Serilog.Sinks.Grafana.Loki/LokiBatchFormatter.cs b/src/Serilog.Sinks.Grafana.Loki/LokiBatchFormatter.cs index 20c9db8..bade389 100644 --- a/src/Serilog.Sinks.Grafana.Loki/LokiBatchFormatter.cs +++ b/src/Serilog.Sinks.Grafana.Loki/LokiBatchFormatter.cs @@ -54,6 +54,9 @@ 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,