-
Notifications
You must be signed in to change notification settings - Fork 775
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
Draft: Add ActivityEventAttachingLogProcessor to build ActivityEvents from logs in OpenTelemetry.Extensions #1907
Closed
CodeBlanch
wants to merge
8
commits into
open-telemetry:main
from
CodeBlanch:logs-to-activityevents2
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
82b65bb
Moved ActivityEventAttachingLogProcessor to OpenTelemetry.Extensions.
CodeBlanch bb8af28
Merge remote-tracking branch 'upstream/main' into logs-to-activityeve…
CodeBlanch ccb5815
Lint.
CodeBlanch abdbac2
Code review.
CodeBlanch 693e075
Project file tweak.
CodeBlanch 5aba3e9
Merging latest from main.
CodeBlanch 8c36e40
Updates for new log scope API. Simplified state & scope default parsers.
CodeBlanch 3bbace4
Logs section in settings.
CodeBlanch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<Project> | ||
<PropertyGroup> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition="'$(SkipAnalysis)'!='true'"> | ||
<AnalysisMode>AllEnabledByDefault</AnalysisMode> | ||
<AnalysisLevel>latest</AnalysisLevel> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition="'$(TargetFramework)' != 'net5.0' and '$(TargetFramework)' != 'netstandard2.1'"> | ||
<!-- Nullable analysis really only works on .net standard 2.1 and newer. --> | ||
<NoWarn>$(NoWarn);nullable</NoWarn> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="$(MicrosoftNetAnalyzersPkgVer)" Condition="'$(SkipAnalysis)'!='true'"> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
src/OpenTelemetry.Extensions/.publicApi/net452/PublicAPI.Shipped.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#nullable enable |
1 change: 1 addition & 0 deletions
1
src/OpenTelemetry.Extensions/.publicApi/net452/PublicAPI.Unshipped.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#nullable enable |
1 change: 1 addition & 0 deletions
1
src/OpenTelemetry.Extensions/.publicApi/net461/PublicAPI.Shipped.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#nullable enable |
9 changes: 9 additions & 0 deletions
9
src/OpenTelemetry.Extensions/.publicApi/net461/PublicAPI.Unshipped.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#nullable enable | ||
Microsoft.Extensions.Logging.OpenTelemetryLoggingExtensions | ||
OpenTelemetry.Logs.ActivityEventAttachingLogProcessorOptions | ||
OpenTelemetry.Logs.ActivityEventAttachingLogProcessorOptions.ActivityEventAttachingLogProcessorOptions() -> void | ||
OpenTelemetry.Logs.ActivityEventAttachingLogProcessorOptions.ScopeConverter.get -> System.Action<System.Diagnostics.ActivityTagsCollection!, int, OpenTelemetry.Logs.LogRecordScope>! | ||
OpenTelemetry.Logs.ActivityEventAttachingLogProcessorOptions.ScopeConverter.set -> void | ||
OpenTelemetry.Logs.ActivityEventAttachingLogProcessorOptions.StateConverter.get -> System.Action<System.Diagnostics.ActivityTagsCollection!, System.Collections.Generic.IReadOnlyList<System.Collections.Generic.KeyValuePair<string!, object!>>!>! | ||
OpenTelemetry.Logs.ActivityEventAttachingLogProcessorOptions.StateConverter.set -> void | ||
static Microsoft.Extensions.Logging.OpenTelemetryLoggingExtensions.AddActivityEventAttachingLogProcessor(this OpenTelemetry.Logs.OpenTelemetryLoggerOptions! loggerOptions, System.Action<OpenTelemetry.Logs.ActivityEventAttachingLogProcessorOptions!>? configure = null) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions! |
1 change: 1 addition & 0 deletions
1
src/OpenTelemetry.Extensions/.publicApi/net5.0/PublicAPI.Shipped.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#nullable enable |
9 changes: 9 additions & 0 deletions
9
src/OpenTelemetry.Extensions/.publicApi/net5.0/PublicAPI.Unshipped.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#nullable enable | ||
Microsoft.Extensions.Logging.OpenTelemetryLoggingExtensions | ||
OpenTelemetry.Logs.ActivityEventAttachingLogProcessorOptions | ||
OpenTelemetry.Logs.ActivityEventAttachingLogProcessorOptions.ActivityEventAttachingLogProcessorOptions() -> void | ||
OpenTelemetry.Logs.ActivityEventAttachingLogProcessorOptions.ScopeConverter.get -> System.Action<System.Diagnostics.ActivityTagsCollection!, int, object!>! | ||
OpenTelemetry.Logs.ActivityEventAttachingLogProcessorOptions.ScopeConverter.set -> void | ||
OpenTelemetry.Logs.ActivityEventAttachingLogProcessorOptions.StateConverter.get -> System.Action<System.Diagnostics.ActivityTagsCollection!, System.Collections.Generic.IReadOnlyList<System.Collections.Generic.KeyValuePair<string!, object!>>!>! | ||
OpenTelemetry.Logs.ActivityEventAttachingLogProcessorOptions.StateConverter.set -> void | ||
static Microsoft.Extensions.Logging.OpenTelemetryLoggingExtensions.AddActivityEventAttachingLogProcessor(this OpenTelemetry.Logs.OpenTelemetryLoggerOptions! loggerOptions, System.Action<OpenTelemetry.Logs.ActivityEventAttachingLogProcessorOptions!>? configure = null) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions! |
1 change: 1 addition & 0 deletions
1
src/OpenTelemetry.Extensions/.publicApi/netstandard2.0/PublicAPI.Shipped.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#nullable enable |
9 changes: 9 additions & 0 deletions
9
src/OpenTelemetry.Extensions/.publicApi/netstandard2.0/PublicAPI.Unshipped.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#nullable enable | ||
Microsoft.Extensions.Logging.OpenTelemetryLoggingExtensions | ||
OpenTelemetry.Logs.ActivityEventAttachingLogProcessorOptions | ||
OpenTelemetry.Logs.ActivityEventAttachingLogProcessorOptions.ActivityEventAttachingLogProcessorOptions() -> void | ||
OpenTelemetry.Logs.ActivityEventAttachingLogProcessorOptions.ScopeConverter.get -> System.Action<System.Diagnostics.ActivityTagsCollection!, int, OpenTelemetry.Logs.LogRecordScope>! | ||
OpenTelemetry.Logs.ActivityEventAttachingLogProcessorOptions.ScopeConverter.set -> void | ||
OpenTelemetry.Logs.ActivityEventAttachingLogProcessorOptions.StateConverter.get -> System.Action<System.Diagnostics.ActivityTagsCollection!, System.Collections.Generic.IReadOnlyList<System.Collections.Generic.KeyValuePair<string!, object!>>!>! | ||
OpenTelemetry.Logs.ActivityEventAttachingLogProcessorOptions.StateConverter.set -> void | ||
static Microsoft.Extensions.Logging.OpenTelemetryLoggingExtensions.AddActivityEventAttachingLogProcessor(this OpenTelemetry.Logs.OpenTelemetryLoggerOptions! loggerOptions, System.Action<OpenTelemetry.Logs.ActivityEventAttachingLogProcessorOptions!>? configure = null) -> OpenTelemetry.Logs.OpenTelemetryLoggerOptions! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// <copyright file="AssemblyInfo.cs" company="OpenTelemetry Authors"> | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
|
||
using System; | ||
|
||
[assembly: CLSCompliant(false)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Changelog | ||
|
||
## Unreleased |
45 changes: 45 additions & 0 deletions
45
src/OpenTelemetry.Extensions/Internal/OpenTelemetryExtensionsEventSource.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// <copyright file="OpenTelemetryExtensionsEventSource.cs" company="OpenTelemetry Authors"> | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
|
||
using System; | ||
using System.Diagnostics.Tracing; | ||
|
||
namespace OpenTelemetry.Internal | ||
{ | ||
/// <summary> | ||
/// EventSource implementation for OpenTelemetry SDK extensions implementation. | ||
/// </summary> | ||
[EventSource(Name = "OpenTelemetry-Extensions")] | ||
internal class OpenTelemetryExtensionsEventSource : EventSource | ||
{ | ||
public static OpenTelemetryExtensionsEventSource Log = new OpenTelemetryExtensionsEventSource(); | ||
|
||
[NonEvent] | ||
public void LogProcessorException(string @event, Exception ex) | ||
{ | ||
if (this.IsEnabled(EventLevel.Error, (EventKeywords)(-1))) | ||
{ | ||
this.LogProcessorException(@event, ex.ToInvariantString()); | ||
} | ||
} | ||
|
||
[Event(1, Message = "Unknown error in LogProcessor event '{0}': '{1}'.", Level = EventLevel.Error)] | ||
public void LogProcessorException(string @event, string exception) | ||
{ | ||
this.WriteEvent(1, @event, exception); | ||
} | ||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
src/OpenTelemetry.Extensions/Logs/ActivityEventAttachingLogProcessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// <copyright file="ActivityEventAttachingLogProcessor.cs" company="OpenTelemetry Authors"> | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
|
||
#if NET461_OR_GREATER || NETSTANDARD2_0 || NET5_0_OR_GREATER | ||
using System; | ||
using System.Diagnostics; | ||
using OpenTelemetry.Internal; | ||
using OpenTelemetry.Trace; | ||
|
||
namespace OpenTelemetry.Logs | ||
{ | ||
internal class ActivityEventAttachingLogProcessor : BaseProcessor<LogRecord> | ||
{ | ||
private static readonly Action<LogRecordScope, State> ProcessScopeRef = ProcessScope; | ||
|
||
private readonly ActivityEventAttachingLogProcessorOptions options; | ||
|
||
public ActivityEventAttachingLogProcessor(ActivityEventAttachingLogProcessorOptions options) | ||
{ | ||
this.options = options ?? throw new ArgumentNullException(nameof(options)); | ||
} | ||
|
||
public override void OnEnd(LogRecord data) | ||
{ | ||
Activity? activity = Activity.Current; | ||
|
||
if (activity?.IsAllDataRequested == true) | ||
{ | ||
var tags = new ActivityTagsCollection | ||
{ | ||
{ nameof(data.CategoryName), data.CategoryName }, | ||
{ nameof(data.LogLevel), data.LogLevel }, | ||
}; | ||
|
||
if (data.EventId != 0) | ||
{ | ||
tags[nameof(data.EventId)] = data.EventId; | ||
} | ||
|
||
var activityEvent = new ActivityEvent("log", data.Timestamp, tags); | ||
|
||
data.ForEachScope(ProcessScopeRef, new State(tags, this)); | ||
|
||
if (data.StateValues != null) | ||
{ | ||
try | ||
{ | ||
this.options.StateConverter?.Invoke(tags, data.StateValues); | ||
} | ||
catch (Exception ex) | ||
{ | ||
OpenTelemetryExtensionsEventSource.Log.LogProcessorException($"Processing state of type [{data.State.GetType().FullName}]", ex); | ||
} | ||
} | ||
|
||
if (!string.IsNullOrEmpty(data.FormattedMessage)) | ||
{ | ||
tags["FormattedMessage"] = data.FormattedMessage; | ||
} | ||
|
||
activity.AddEvent(activityEvent); | ||
|
||
if (data.Exception != null) | ||
{ | ||
activity.RecordException(data.Exception); | ||
} | ||
} | ||
} | ||
|
||
private static void ProcessScope(LogRecordScope scope, State state) | ||
{ | ||
try | ||
{ | ||
state.Processor.options.ScopeConverter?.Invoke(state.Tags, state.Index++, scope); | ||
} | ||
catch (Exception ex) | ||
{ | ||
OpenTelemetryExtensionsEventSource.Log.LogProcessorException($"Processing scope of type [{scope.GetType().FullName}]", ex); | ||
} | ||
} | ||
|
||
private class State | ||
{ | ||
public State(ActivityTagsCollection tags, ActivityEventAttachingLogProcessor processor) | ||
{ | ||
this.Tags = tags; | ||
this.Processor = processor; | ||
} | ||
|
||
public ActivityTagsCollection Tags { get; } | ||
|
||
public ActivityEventAttachingLogProcessor Processor { get; } | ||
|
||
public int Index { get; set; } | ||
} | ||
} | ||
} | ||
#endif |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we use
data.Timestamp
even for the exception event?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't think so? It's a log event with an exception added. The timestamp of the log still feels valid to me, but I don't feel strongly about it.