Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add opt out to filtering the properties list when using propertiesAsLabels #154

Merged
merged 2 commits into from
Nov 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
EraYaN marked this conversation as resolved.
Show resolved Hide resolved
{
_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));
}
}