Skip to content

Commit 0f58802

Browse files
authored
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
1 parent b8cf3a9 commit 0f58802

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/Serilog.Sinks.Grafana.Loki/LoggerConfigurationLokiExtensions.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ public static class LoggerConfigurationLokiExtensions
7373
/// <param name="useInternalTimestamp">
7474
/// Should use internal sink timestamp instead of application one to use as log timestamp.
7575
/// </param>
76+
/// <param name="leavePropertiesIntact">
77+
/// Leaves the list of properties intact after extracting the labels specified in propertiesAsLabels.
78+
/// </param>
7679
/// <returns>Logger configuration, allowing configuration to continue.</returns>
7780
public static LoggerConfiguration GrafanaLoki(
7881
this LoggerSinkConfiguration sinkConfiguration,
@@ -87,7 +90,8 @@ public static LoggerConfiguration GrafanaLoki(
8790
ITextFormatter? textFormatter = null,
8891
ILokiHttpClient? httpClient = null,
8992
IReservedPropertyRenamingStrategy? reservedPropertyRenamingStrategy = null,
90-
bool useInternalTimestamp = false)
93+
bool useInternalTimestamp = false,
94+
bool leavePropertiesIntact = false)
9195
{
9296
if (sinkConfiguration == null)
9397
{
@@ -105,7 +109,8 @@ public static LoggerConfiguration GrafanaLoki(
105109
reservedPropertyRenamingStrategy,
106110
labels,
107111
propertiesAsLabels,
108-
useInternalTimestamp);
112+
useInternalTimestamp,
113+
leavePropertiesIntact);
109114

110115
var sink = new LokiSink(
111116
LokiRoutesBuilder.BuildLogsEntriesRoute(uri),

src/Serilog.Sinks.Grafana.Loki/LokiBatchFormatter.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ internal class LokiBatchFormatter : ILokiBatchFormatter
3434
private readonly IEnumerable<LokiLabel> _globalLabels;
3535
private readonly IReservedPropertyRenamingStrategy _renamingStrategy;
3636
private readonly IEnumerable<string> _propertiesAsLabels;
37+
38+
private readonly bool _leavePropertiesIntact;
3739
private readonly bool _useInternalTimestamp;
3840

3941
/// <summary>
@@ -52,16 +54,21 @@ internal class LokiBatchFormatter : ILokiBatchFormatter
5254
/// <param name="useInternalTimestamp">
5355
/// Compute internal timestamp
5456
/// </param>
57+
/// <param name="leavePropertiesIntact">
58+
/// Leave the list of properties intact after extracting the labels specified in propertiesAsLabels.
59+
/// </param>
5560
public LokiBatchFormatter(
5661
IReservedPropertyRenamingStrategy renamingStrategy,
5762
IEnumerable<LokiLabel>? globalLabels = null,
5863
IEnumerable<string>? propertiesAsLabels = null,
59-
bool useInternalTimestamp = false)
64+
bool useInternalTimestamp = false,
65+
bool leavePropertiesIntact = false)
6066
{
6167
_renamingStrategy = renamingStrategy;
6268
_globalLabels = globalLabels ?? Enumerable.Empty<LokiLabel>();
6369
_propertiesAsLabels = propertiesAsLabels ?? Enumerable.Empty<string>();
6470
_useInternalTimestamp = useInternalTimestamp;
71+
_leavePropertiesIntact = leavePropertiesIntact;
6572
}
6673

6774
/// <summary>
@@ -214,6 +221,6 @@ private void GenerateEntry(
214221
}
215222

216223
return (labels,
217-
lokiLogEvent.CopyWithProperties(remainingProperties));
224+
lokiLogEvent.CopyWithProperties(_leavePropertiesIntact ? properties : remainingProperties));
218225
}
219226
}

0 commit comments

Comments
 (0)