Skip to content

Commit bdb0077

Browse files
committed
Merge branch 'main' into issue/AWS-Lambda-SQS-SNS-support
2 parents 94e489c + 595a9a3 commit bdb0077

9 files changed

Lines changed: 367 additions & 319 deletions

File tree

src/OpenTelemetry.Exporter.Geneva/Metrics/GenevaMetricExporter.cs

Lines changed: 238 additions & 317 deletions
Large diffs are not rendered by default.

src/OpenTelemetry.Extensions/.publicApi/net462/PublicAPI.Unshipped.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#nullable enable
22
Microsoft.Extensions.Logging.OpenTelemetryLoggingExtensions
33
OpenTelemetry.Logs.LogToActivityEventConversionOptions
4+
OpenTelemetry.Logs.LogToActivityEventConversionOptions.Filter.get -> System.Func<OpenTelemetry.Logs.LogRecord!, bool>?
5+
OpenTelemetry.Logs.LogToActivityEventConversionOptions.Filter.set -> void
46
OpenTelemetry.Logs.LogToActivityEventConversionOptions.LogToActivityEventConversionOptions() -> void
57
OpenTelemetry.Logs.LogToActivityEventConversionOptions.ScopeConverter.get -> System.Action<System.Diagnostics.ActivityTagsCollection!, int, OpenTelemetry.Logs.LogRecordScope>!
68
OpenTelemetry.Logs.LogToActivityEventConversionOptions.ScopeConverter.set -> void

src/OpenTelemetry.Extensions/.publicApi/net6.0/PublicAPI.Unshipped.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#nullable enable
22
Microsoft.Extensions.Logging.OpenTelemetryLoggingExtensions
33
OpenTelemetry.Logs.LogToActivityEventConversionOptions
4+
OpenTelemetry.Logs.LogToActivityEventConversionOptions.Filter.get -> System.Func<OpenTelemetry.Logs.LogRecord!, bool>?
5+
OpenTelemetry.Logs.LogToActivityEventConversionOptions.Filter.set -> void
46
OpenTelemetry.Logs.LogToActivityEventConversionOptions.LogToActivityEventConversionOptions() -> void
57
OpenTelemetry.Logs.LogToActivityEventConversionOptions.ScopeConverter.get -> System.Action<System.Diagnostics.ActivityTagsCollection!, int, OpenTelemetry.Logs.LogRecordScope>!
68
OpenTelemetry.Logs.LogToActivityEventConversionOptions.ScopeConverter.set -> void

src/OpenTelemetry.Extensions/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#nullable enable
22
Microsoft.Extensions.Logging.OpenTelemetryLoggingExtensions
33
OpenTelemetry.Logs.LogToActivityEventConversionOptions
4+
OpenTelemetry.Logs.LogToActivityEventConversionOptions.Filter.get -> System.Func<OpenTelemetry.Logs.LogRecord!, bool>?
5+
OpenTelemetry.Logs.LogToActivityEventConversionOptions.Filter.set -> void
46
OpenTelemetry.Logs.LogToActivityEventConversionOptions.LogToActivityEventConversionOptions() -> void
57
OpenTelemetry.Logs.LogToActivityEventConversionOptions.ScopeConverter.get -> System.Action<System.Diagnostics.ActivityTagsCollection!, int, OpenTelemetry.Logs.LogRecordScope>!
68
OpenTelemetry.Logs.LogToActivityEventConversionOptions.ScopeConverter.set -> void

src/OpenTelemetry.Extensions/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## Unreleased
44

5+
* Add LogToActivityEventConversionOptions.Filter callback
6+
([#1059](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1059))
7+
58
## 1.0.0-beta.4
69

710
Released 2023-Feb-27

src/OpenTelemetry.Extensions/Internal/ActivityEventAttachingLogProcessor.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,21 @@ public override void OnEnd(LogRecord data)
5050

5151
if (activity?.IsAllDataRequested == true)
5252
{
53+
try
54+
{
55+
if (this.options.Filter?.Invoke(data) == false)
56+
{
57+
return;
58+
}
59+
}
60+
#pragma warning disable CA1031 // Do not catch general exception types
61+
catch (Exception ex)
62+
#pragma warning restore CA1031 // Do not catch general exception types
63+
{
64+
OpenTelemetryExtensionsEventSource.Log.LogRecordFilterException(data.CategoryName, ex);
65+
return;
66+
}
67+
5368
var tags = new ActivityTagsCollection
5469
{
5570
{ nameof(data.CategoryName), data.CategoryName },

src/OpenTelemetry.Extensions/Internal/OpenTelemetryExtensionsEventSource.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,19 @@ public void LogProcessorException(string @event, string exception)
4242
{
4343
this.WriteEvent(1, @event, exception);
4444
}
45+
46+
[NonEvent]
47+
public void LogRecordFilterException(string? categoryName, Exception ex)
48+
{
49+
if (this.IsEnabled(EventLevel.Warning, (EventKeywords)(-1)))
50+
{
51+
this.LogRecordFilterException(categoryName, ex.ToInvariantString());
52+
}
53+
}
54+
55+
[Event(2, Message = "Filter threw an exception, log record will not be attached to an activity, the log record would flow to its pipeline unaffected. CategoryName: '{0}', Exception: {1}.", Level = EventLevel.Warning)]
56+
public void LogRecordFilterException(string? categoryName, string exception)
57+
{
58+
this.WriteEvent(2, categoryName, exception);
59+
}
4560
}

src/OpenTelemetry.Extensions/Logs/LogToActivityEventConversionOptions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,17 @@ public class LogToActivityEventConversionOptions
3434
/// Gets or sets the callback action used to convert log scopes into <see cref="ActivityEvent"/> tags.
3535
/// </summary>
3636
public Action<ActivityTagsCollection, int, LogRecordScope> ScopeConverter { get; set; } = DefaultLogStateConverter.ConvertScope;
37+
38+
/// <summary>
39+
/// Gets or sets the callback method allowing to filter out particular <see cref="LogRecord"/>.
40+
/// </summary>
41+
/// <remarks>
42+
/// The filter callback receives the <see cref="LogRecord"/> for the
43+
/// processed logRecord and should return a boolean.
44+
/// <list type="bullet">
45+
/// <item>If filter returns <see langword="true"/> the event is collected.</item>
46+
/// <item>If filter returns <see langword="false"/> or throws an exception the event is filtered out (NOT collected).</item>
47+
/// </list>
48+
/// </remarks>
49+
public Func<LogRecord, bool>? Filter { get; set; }
3750
}

test/OpenTelemetry.Extensions.Tests/ActivityEventAttachingLogProcessorTests.cs

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,16 @@ public void Dispose()
5757
[InlineData(false)]
5858
[InlineData(true, 18, true, true, true)]
5959
[InlineData(true, 0, false, false, true, true)]
60+
[InlineData(true, 18, true, true, true, false, true)]
61+
[InlineData(true, 0, false, false, true, true, true)]
6062
public void AttachLogsToActivityEventTest(
6163
bool sampled,
6264
int eventId = 0,
6365
bool includeFormattedMessage = false,
6466
bool parseStateValues = false,
6567
bool includeScopes = false,
66-
bool recordException = false)
68+
bool recordException = false,
69+
bool? filter = null)
6770
{
6871
this.sampled = sampled;
6972

@@ -74,7 +77,15 @@ public void AttachLogsToActivityEventTest(
7477
options.IncludeScopes = includeScopes;
7578
options.IncludeFormattedMessage = includeFormattedMessage;
7679
options.ParseStateValues = parseStateValues;
77-
options.AttachLogsToActivityEvent();
80+
options.AttachLogsToActivityEvent(x =>
81+
{
82+
x.Filter = filter switch
83+
{
84+
true => _ => true,
85+
false => _ => false,
86+
null => null,
87+
};
88+
});
7889
});
7990
builder.AddFilter(typeof(ActivityEventAttachingLogProcessorTests).FullName, LogLevel.Trace);
8091
});
@@ -180,4 +191,68 @@ public void AttachLogsToActivityEventTest(
180191
Assert.Empty(activity.Events);
181192
}
182193
}
194+
195+
[Theory]
196+
[InlineData(true, true)]
197+
[InlineData(false, true)]
198+
[InlineData(true, true, 18, true, true, true)]
199+
[InlineData(true, true, 0, false, false, true, true)]
200+
[InlineData(true, false)]
201+
[InlineData(false, false)]
202+
[InlineData(true, false, 18, true, true, true)]
203+
[InlineData(true, false, 0, false, false, true, true)]
204+
public void AttachLogsToActivityEventTest_Filter(
205+
bool sampled,
206+
bool filterThrows,
207+
int eventId = 0,
208+
bool includeFormattedMessage = false,
209+
bool parseStateValues = false,
210+
bool includeScopes = false,
211+
bool recordException = false)
212+
{
213+
this.sampled = sampled;
214+
215+
using ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
216+
{
217+
builder.AddOpenTelemetry(options =>
218+
{
219+
options.IncludeScopes = includeScopes;
220+
options.IncludeFormattedMessage = includeFormattedMessage;
221+
options.ParseStateValues = parseStateValues;
222+
options.AttachLogsToActivityEvent(x => x.Filter = _ => filterThrows
223+
? throw new Exception()
224+
: false);
225+
});
226+
builder.AddFilter(typeof(ActivityEventAttachingLogProcessorTests).FullName, LogLevel.Trace);
227+
});
228+
229+
ILogger logger = loggerFactory.CreateLogger<ActivityEventAttachingLogProcessorTests>();
230+
Activity activity = this.activitySource.StartActivity("Test");
231+
232+
using IDisposable scope = logger.BeginScope("{NodeId}", 99);
233+
234+
logger.LogInformation(eventId, "Hello OpenTelemetry {UserId}!", 8);
235+
236+
if (recordException)
237+
{
238+
var innerActivity = this.activitySource.StartActivity("InnerTest");
239+
240+
using IDisposable innerScope = logger.BeginScope("{RequestId}", "1234");
241+
242+
logger.LogError(new InvalidOperationException("Goodbye OpenTelemetry."), "Exception event.");
243+
244+
innerActivity.Dispose();
245+
}
246+
247+
activity.Dispose();
248+
249+
if (sampled)
250+
{
251+
Assert.DoesNotContain(activity.Events, x => x.Name == "log");
252+
}
253+
else
254+
{
255+
Assert.Empty(activity.Events);
256+
}
257+
}
183258
}

0 commit comments

Comments
 (0)