Skip to content

Commit

Permalink
Excluded global labels from filtration
Browse files Browse the repository at this point in the history
  • Loading branch information
mishamyte committed Nov 15, 2020
1 parent 13015f5 commit c23e782
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 16 deletions.
10 changes: 2 additions & 8 deletions src/Serilog.Sinks.Grafana.Loki/LokiBatchFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,7 @@ public void Format(IEnumerable<string> logEvents, TextWriter output)

foreach (var label in _globalLabels)
{
if (IsAllowedByFilter(label.Key))
{
stream.AddLabel(label.Key, label.Value);
}
stream.AddLabel(label.Key, label.Value);
}

stream.AddEntry(DateTimeOffset.UtcNow, logEvent.TrimEnd('\r', '\n'));
Expand All @@ -183,10 +180,7 @@ private void GenerateLabels(LogEvent logEvent, LokiStream stream)

foreach (var label in _globalLabels)
{
if (IsAllowedByFilter(label.Key))
{
stream.AddLabel(label.Key, label.Value);
}
stream.AddLabel(label.Key, label.Value);
}

foreach (var property in logEvent.Properties)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<LangVersion>8</LangVersion>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>4.0.1</Version>
<Version>4.0.2</Version>
<!--SourceLink -->
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<RepositoryUrl>https://github.com/mishamyte/serilog-sinks-grafana-loki.git</RepositoryUrl>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ namespace Serilog.Sinks.Grafana.Loki.Tests.Fixtures
{
public class HttpClientTextFixture : IDisposable
{
public HttpClientTextFixture()
{
}

public void Dispose()
{
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"streams":[{"stream":{"level":"error","server_ip":"127.0.0.1"},"values":[["<unixepochinnanoseconds>","An error occured"]]}]}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Text.RegularExpressions;
using Serilog.Sinks.Grafana.Loki.Tests.Fixtures;
using Serilog.Sinks.Grafana.Loki.Tests.Infrastructure;
using Shouldly;
Expand Down Expand Up @@ -93,5 +92,25 @@ public void EntryShouldBeRenderedAccordingToOutputTemplate()
_client.Content.ShouldMatchApproved(c =>
c.WithScrubber(s => Regex.Replace(s, "\"[0-9]{19}\"", "\"<unixepochinnanoseconds>\"")));
}

[Fact]
public void GlobalLabelsShouldNotBeFiltered()
{
var logger = new LoggerConfiguration()
.WriteTo.GrafanaLoki(
"http://loki:3100",
outputTemplate: OutputTemplate,
filtrationMode: LokiLabelFiltrationMode.Exclude,
filtrationLabels: new[] {"server_ip"},
labels: new[] {new LokiLabel {Key = "server_ip", Value = "127.0.0.1"}},
httpClient: _client)
.CreateLogger();

logger.Error("An error occured");
logger.Dispose();

_client.Content.ShouldMatchApproved(c =>
c.WithScrubber(s => Regex.Replace(s, "\"[0-9]{19}\"", "\"<unixepochinnanoseconds>\"")));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.0" />
<PackageReference Include="Shouldly" Version="4.0.0" />
<PackageReference Include="Shouldly" Version="4.0.1" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
Expand Down

0 comments on commit c23e782

Please sign in to comment.