Skip to content

Commit

Permalink
Merge branch 'main' into issue/AWS-Lambda-SQS-SNS-support
Browse files Browse the repository at this point in the history
  • Loading branch information
rypdal committed Mar 27, 2023
2 parents 94e489c + 595a9a3 commit bdb0077
Show file tree
Hide file tree
Showing 9 changed files with 367 additions and 319 deletions.
555 changes: 238 additions & 317 deletions src/OpenTelemetry.Exporter.Geneva/Metrics/GenevaMetricExporter.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#nullable enable
Microsoft.Extensions.Logging.OpenTelemetryLoggingExtensions
OpenTelemetry.Logs.LogToActivityEventConversionOptions
OpenTelemetry.Logs.LogToActivityEventConversionOptions.Filter.get -> System.Func<OpenTelemetry.Logs.LogRecord!, bool>?
OpenTelemetry.Logs.LogToActivityEventConversionOptions.Filter.set -> void
OpenTelemetry.Logs.LogToActivityEventConversionOptions.LogToActivityEventConversionOptions() -> void
OpenTelemetry.Logs.LogToActivityEventConversionOptions.ScopeConverter.get -> System.Action<System.Diagnostics.ActivityTagsCollection!, int, OpenTelemetry.Logs.LogRecordScope>!
OpenTelemetry.Logs.LogToActivityEventConversionOptions.ScopeConverter.set -> void
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#nullable enable
Microsoft.Extensions.Logging.OpenTelemetryLoggingExtensions
OpenTelemetry.Logs.LogToActivityEventConversionOptions
OpenTelemetry.Logs.LogToActivityEventConversionOptions.Filter.get -> System.Func<OpenTelemetry.Logs.LogRecord!, bool>?
OpenTelemetry.Logs.LogToActivityEventConversionOptions.Filter.set -> void
OpenTelemetry.Logs.LogToActivityEventConversionOptions.LogToActivityEventConversionOptions() -> void
OpenTelemetry.Logs.LogToActivityEventConversionOptions.ScopeConverter.get -> System.Action<System.Diagnostics.ActivityTagsCollection!, int, OpenTelemetry.Logs.LogRecordScope>!
OpenTelemetry.Logs.LogToActivityEventConversionOptions.ScopeConverter.set -> void
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#nullable enable
Microsoft.Extensions.Logging.OpenTelemetryLoggingExtensions
OpenTelemetry.Logs.LogToActivityEventConversionOptions
OpenTelemetry.Logs.LogToActivityEventConversionOptions.Filter.get -> System.Func<OpenTelemetry.Logs.LogRecord!, bool>?
OpenTelemetry.Logs.LogToActivityEventConversionOptions.Filter.set -> void
OpenTelemetry.Logs.LogToActivityEventConversionOptions.LogToActivityEventConversionOptions() -> void
OpenTelemetry.Logs.LogToActivityEventConversionOptions.ScopeConverter.get -> System.Action<System.Diagnostics.ActivityTagsCollection!, int, OpenTelemetry.Logs.LogRecordScope>!
OpenTelemetry.Logs.LogToActivityEventConversionOptions.ScopeConverter.set -> void
Expand Down
3 changes: 3 additions & 0 deletions src/OpenTelemetry.Extensions/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

* Add LogToActivityEventConversionOptions.Filter callback
([#1059](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/1059))

## 1.0.0-beta.4

Released 2023-Feb-27
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ public override void OnEnd(LogRecord data)

if (activity?.IsAllDataRequested == true)
{
try
{
if (this.options.Filter?.Invoke(data) == false)
{
return;
}
}
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
{
OpenTelemetryExtensionsEventSource.Log.LogRecordFilterException(data.CategoryName, ex);
return;
}

var tags = new ActivityTagsCollection
{
{ nameof(data.CategoryName), data.CategoryName },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,19 @@ public void LogProcessorException(string @event, string exception)
{
this.WriteEvent(1, @event, exception);
}

[NonEvent]
public void LogRecordFilterException(string? categoryName, Exception ex)
{
if (this.IsEnabled(EventLevel.Warning, (EventKeywords)(-1)))
{
this.LogRecordFilterException(categoryName, ex.ToInvariantString());
}
}

[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)]
public void LogRecordFilterException(string? categoryName, string exception)
{
this.WriteEvent(2, categoryName, exception);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,17 @@ public class LogToActivityEventConversionOptions
/// Gets or sets the callback action used to convert log scopes into <see cref="ActivityEvent"/> tags.
/// </summary>
public Action<ActivityTagsCollection, int, LogRecordScope> ScopeConverter { get; set; } = DefaultLogStateConverter.ConvertScope;

/// <summary>
/// Gets or sets the callback method allowing to filter out particular <see cref="LogRecord"/>.
/// </summary>
/// <remarks>
/// The filter callback receives the <see cref="LogRecord"/> for the
/// processed logRecord and should return a boolean.
/// <list type="bullet">
/// <item>If filter returns <see langword="true"/> the event is collected.</item>
/// <item>If filter returns <see langword="false"/> or throws an exception the event is filtered out (NOT collected).</item>
/// </list>
/// </remarks>
public Func<LogRecord, bool>? Filter { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,16 @@ public void Dispose()
[InlineData(false)]
[InlineData(true, 18, true, true, true)]
[InlineData(true, 0, false, false, true, true)]
[InlineData(true, 18, true, true, true, false, true)]
[InlineData(true, 0, false, false, true, true, true)]
public void AttachLogsToActivityEventTest(
bool sampled,
int eventId = 0,
bool includeFormattedMessage = false,
bool parseStateValues = false,
bool includeScopes = false,
bool recordException = false)
bool recordException = false,
bool? filter = null)
{
this.sampled = sampled;

Expand All @@ -74,7 +77,15 @@ public void AttachLogsToActivityEventTest(
options.IncludeScopes = includeScopes;
options.IncludeFormattedMessage = includeFormattedMessage;
options.ParseStateValues = parseStateValues;
options.AttachLogsToActivityEvent();
options.AttachLogsToActivityEvent(x =>
{
x.Filter = filter switch
{
true => _ => true,
false => _ => false,
null => null,
};
});
});
builder.AddFilter(typeof(ActivityEventAttachingLogProcessorTests).FullName, LogLevel.Trace);
});
Expand Down Expand Up @@ -180,4 +191,68 @@ public void AttachLogsToActivityEventTest(
Assert.Empty(activity.Events);
}
}

[Theory]
[InlineData(true, true)]
[InlineData(false, true)]
[InlineData(true, true, 18, true, true, true)]
[InlineData(true, true, 0, false, false, true, true)]
[InlineData(true, false)]
[InlineData(false, false)]
[InlineData(true, false, 18, true, true, true)]
[InlineData(true, false, 0, false, false, true, true)]
public void AttachLogsToActivityEventTest_Filter(
bool sampled,
bool filterThrows,
int eventId = 0,
bool includeFormattedMessage = false,
bool parseStateValues = false,
bool includeScopes = false,
bool recordException = false)
{
this.sampled = sampled;

using ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddOpenTelemetry(options =>
{
options.IncludeScopes = includeScopes;
options.IncludeFormattedMessage = includeFormattedMessage;
options.ParseStateValues = parseStateValues;
options.AttachLogsToActivityEvent(x => x.Filter = _ => filterThrows
? throw new Exception()
: false);
});
builder.AddFilter(typeof(ActivityEventAttachingLogProcessorTests).FullName, LogLevel.Trace);
});

ILogger logger = loggerFactory.CreateLogger<ActivityEventAttachingLogProcessorTests>();
Activity activity = this.activitySource.StartActivity("Test");

using IDisposable scope = logger.BeginScope("{NodeId}", 99);

logger.LogInformation(eventId, "Hello OpenTelemetry {UserId}!", 8);

if (recordException)
{
var innerActivity = this.activitySource.StartActivity("InnerTest");

using IDisposable innerScope = logger.BeginScope("{RequestId}", "1234");

logger.LogError(new InvalidOperationException("Goodbye OpenTelemetry."), "Exception event.");

innerActivity.Dispose();
}

activity.Dispose();

if (sampled)
{
Assert.DoesNotContain(activity.Events, x => x.Name == "log");
}
else
{
Assert.Empty(activity.Events);
}
}
}

0 comments on commit bdb0077

Please sign in to comment.