-
Notifications
You must be signed in to change notification settings - Fork 772
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
Add ActivityEventAttachingLogProcessor to build ActivityEvents from logs #1833
Closed
CodeBlanch
wants to merge
11
commits into
open-telemetry:main
from
CodeBlanch:logs-to-activityevents
Closed
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
69eabf1
Added ActivityEventAttachingLogProcessor.
CodeBlanch 4fde024
Testing and tweaks.
CodeBlanch 0725ae6
State parsing and code review.
CodeBlanch 8c505e3
Code review.
CodeBlanch c78c50a
Merge branch 'main' into logs-to-activityevents
CodeBlanch 7367ea5
Merge branch 'main' into logs-to-activityevents
cijothomas 28d77ec
Switch to Hashtable for OpenTelemetryLoggerProvider logger cache.
CodeBlanch 36e3722
Merge from main.
CodeBlanch 79c6bf8
Merge branch 'logs-to-activityevents' of https://github.com/CodeBlanc…
CodeBlanch 94bf100
Fixed enumerator.
CodeBlanch f940610
Merge branch 'main' into logs-to-activityevents
cijothomas 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
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,16 @@ | ||
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 | ||
OpenTelemetry.Logs.LogRecord.ForEachScope<TState>(System.Action<object, TState> callback, TState state) -> void | ||
OpenTelemetry.Logs.LogRecord.Message.get -> string | ||
OpenTelemetry.Logs.LogRecord.StateValues.get -> System.Collections.Generic.IReadOnlyList<System.Collections.Generic.KeyValuePair<string, object>> | ||
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeMessage.get -> bool | ||
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeMessage.set -> void | ||
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeScopes.get -> bool | ||
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeScopes.set -> void | ||
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ParseStateValues.get -> bool | ||
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ParseStateValues.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,16 @@ | ||
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 | ||
OpenTelemetry.Logs.LogRecord.ForEachScope<TState>(System.Action<object, TState> callback, TState state) -> void | ||
OpenTelemetry.Logs.LogRecord.Message.get -> string | ||
OpenTelemetry.Logs.LogRecord.StateValues.get -> System.Collections.Generic.IReadOnlyList<System.Collections.Generic.KeyValuePair<string, object>> | ||
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeMessage.get -> bool | ||
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeMessage.set -> void | ||
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeScopes.get -> bool | ||
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.IncludeScopes.set -> void | ||
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ParseStateValues.get -> bool | ||
OpenTelemetry.Logs.OpenTelemetryLoggerOptions.ParseStateValues.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
112 changes: 112 additions & 0 deletions
112
src/OpenTelemetry/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,112 @@ | ||
// <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 || NETSTANDARD2_0 | ||
using System; | ||
using System.Diagnostics; | ||
using OpenTelemetry.Internal; | ||
using OpenTelemetry.Trace; | ||
|
||
namespace OpenTelemetry.Logs | ||
{ | ||
internal class ActivityEventAttachingLogProcessor : BaseProcessor<LogRecord> | ||
{ | ||
private static readonly Action<object, 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 = tags, | ||
Processor = this, | ||
}); | ||
|
||
if (data.StateValues != null) | ||
{ | ||
try | ||
{ | ||
this.options.StateConverter?.Invoke(tags, data.StateValues); | ||
} | ||
catch (Exception ex) | ||
{ | ||
OpenTelemetrySdkEventSource.Log.LogProcessorException($"Processing state of type [{data.State.GetType().FullName}]", ex); | ||
} | ||
} | ||
|
||
if (!string.IsNullOrEmpty(data.Message)) | ||
{ | ||
tags["Message"] = data.Message; | ||
} | ||
|
||
activity.AddEvent(activityEvent); | ||
|
||
if (data.Exception != null) | ||
{ | ||
activity.RecordException(data.Exception); | ||
} | ||
} | ||
} | ||
|
||
private static void ProcessScope(object scope, State state) | ||
{ | ||
if (scope != null) | ||
{ | ||
try | ||
{ | ||
state.Processor.options.ScopeConverter?.Invoke(state.Tags, state.Index++, scope); | ||
} | ||
catch (Exception ex) | ||
{ | ||
OpenTelemetrySdkEventSource.Log.LogProcessorException($"Processing scope of type [{scope.GetType().FullName}]", ex); | ||
} | ||
} | ||
} | ||
|
||
private class State | ||
{ | ||
public ActivityTagsCollection Tags; | ||
public int Index; | ||
public ActivityEventAttachingLogProcessor Processor; | ||
} | ||
} | ||
} | ||
#endif |
37 changes: 37 additions & 0 deletions
37
src/OpenTelemetry/Logs/ActivityEventAttachingLogProcessorOptions.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,37 @@ | ||
// <copyright file="ActivityEventAttachingLogProcessorOptions.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 || NETSTANDARD2_0 | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
|
||
namespace OpenTelemetry.Logs | ||
{ | ||
public class ActivityEventAttachingLogProcessorOptions | ||
{ | ||
/// <summary> | ||
/// Gets or sets the callback action used to convert log state to tags. | ||
/// </summary> | ||
public Action<ActivityTagsCollection, IReadOnlyList<KeyValuePair<string, object>>> StateConverter { get; set; } = DefaultLogStateConverter.ConvertState; | ||
|
||
/// <summary> | ||
/// Gets or sets the callback action used to convert log scopes to tags. | ||
/// </summary> | ||
public Action<ActivityTagsCollection, int, object> ScopeConverter { get; set; } = DefaultLogStateConverter.ConvertScope; | ||
} | ||
} | ||
#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.
I'm struggling with this name, meanwhile I don't have a good proposal...
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.
Ya I know it's a mouthful. Open to suggestions.
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.
open-telemetry/opentelemetry-specification#1461
This PR suggests that span events are conceptually the same as logs.
I guess we could consider something like this?
TracerProvider.LogRecordToActivityEventProcessor()
(inherited from LogProcessor): ConvertsLogRecord
toActivityEvent
, and usesActivity.AddEvent
to attach it to the current Activity. If there there is no "current activity", this processor will simply ignore the log record.LoggerProvider.ActivityEventToLogRecordProcessor(logger)
(inherited from ActivityProcessor): Converts theActivityEvents
objects to LogRecords.