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

Feature/serilog property filter #90

Merged
merged 11 commits into from
Jul 23, 2020
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Web;
#endif
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Serilog.Events;

Expand All @@ -19,6 +20,7 @@ public interface IEcsTextFormatterConfiguration
Func<Base, LogEvent, Base> MapCustom { get; set; }
bool MapExceptions { get; set; }
IHttpAdapter MapHttpAdapter { get; set; }
IList<string> LogEventPropertiesToFilter { get;set; }
ghayes7 marked this conversation as resolved.
Show resolved Hide resolved
}

public class EcsTextFormatterConfiguration : IEcsTextFormatterConfiguration
Expand All @@ -27,6 +29,7 @@ public class EcsTextFormatterConfiguration : IEcsTextFormatterConfiguration
bool IEcsTextFormatterConfiguration.MapCurrentThread { get; set; } = true;

IHttpAdapter IEcsTextFormatterConfiguration.MapHttpAdapter { get; set; }
public IList<string> LogEventPropertiesToFilter { get; set; }

Func<Base, LogEvent, Base> IEcsTextFormatterConfiguration.MapCustom { get; set; } = (b, e) => b;

Expand Down
8 changes: 5 additions & 3 deletions src/Elastic.CommonSchema.Serilog/LogEventConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static Base ConvertToEcs(LogEvent logEvent, IEcsTextFormatterConfiguratio
Log = GetLog(logEvent, exceptions, configuration),
Agent = GetAgent(logEvent),
Event = GetEvent(logEvent),
Metadata = GetMetadata(logEvent),
Metadata = GetMetadata(logEvent,configuration.LogEventPropertiesToFilter),
Process = GetProcess(logEvent, configuration.MapCurrentThread),
Host = GetHost(logEvent),
Trace = GetTrace(logEvent),
Expand Down Expand Up @@ -91,7 +91,7 @@ private static Transaction GetTransaction(LogEvent logEvent) =>
? null
: new Transaction { Id = transactionId.Value.ToString() };

private static IDictionary<string, object> GetMetadata(LogEvent logEvent)
private static IDictionary<string, object> GetMetadata(LogEvent logEvent, IList<string> propKeyFilter=null)
{
var dict = new Dictionary<string, object>
{
Expand Down Expand Up @@ -137,7 +137,9 @@ private static IDictionary<string, object> GetMetadata(LogEvent logEvent)
case SpecialKeys.MachineName:
continue;
}

//key present in list of keys to filter
if (propKeyFilter?.Any(key => key.Equals(logEventPropertyValue.Key, StringComparison.OrdinalIgnoreCase)) ?? false)
continue;
dict.Add(ToSnakeCase(logEventPropertyValue.Key), PropertyValueToObject(logEventPropertyValue.Value));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Elastic.Apm;
using Elastic.Apm.SerilogEnricher;
using FluentAssertions;
using Serilog;
using Serilog.Events;
using Serilog.Parsing;
using Xunit;
using Xunit.Abstractions;

namespace Elastic.CommonSchema.Serilog.Tests
{
public class LogEventPropFilterTests : LogTestsBase
{
public LogEventPropFilterTests(ITestOutputHelper output) : base(output)
{
LoggerConfiguration = LoggerConfiguration
.Enrich.WithThreadId()
.Enrich.WithThreadName()
.Enrich.WithMachineName()
.Enrich.WithProcessId()
.Enrich.WithProcessName()
.Enrich.WithEnvironmentUserName()
.Enrich.WithElasticApmCorrelationInfo();

Formatter = new EcsTextFormatter(new EcsTextFormatterConfiguration()
{
LogEventPropertiesToFilter = new List<string>(){{ "foo" }}
});
}

[Fact]
public void FilterLogEventProperty() => TestLogger((logger, getLogEvents) =>
{
var parser = new MessageTemplateParser();
var evnt = new LogEvent(
DateTimeOffset.Now,
LogEventLevel.Information,
null,
parser.Parse("My Log message!"),
new LogEventProperty[]
{
new LogEventProperty("foo", new ScalarValue("aaa")),
new LogEventProperty("bar", new ScalarValue("bbb")),
});
logger.Write(evnt);

var logEvents = getLogEvents();
logEvents.Should().HaveCount(1);

var ecsEvents = ToEcsEvents(logEvents);

var (_, info) = ecsEvents.First();
info.Log.Level.Should().Be("Information");
info.Error.Should().BeNull();
info.Metadata.Should().Contain("bar", "bbb");
info.Metadata.Should().NotContainKey("foo", "Should have been filtered");
});
}
}
2 changes: 1 addition & 1 deletion tests/Elastic.CommonSchema.Serilog.Tests/LogTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public abstract class LogTestsBase
{
protected LoggerConfiguration LoggerConfiguration { get; set; }

protected EcsTextFormatter Formatter { get; } = new EcsTextFormatter();
protected EcsTextFormatter Formatter { get; set; } = new EcsTextFormatter();

protected LogTestsBase(ITestOutputHelper output) =>
LoggerConfiguration = new LoggerConfiguration()
Expand Down