-
Notifications
You must be signed in to change notification settings - Fork 12
Ensure correct execution order for user-provided delegates #325
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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,18 @@ | ||
| // 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.Diagnostics; | ||
| using OpenTelemetry; | ||
|
|
||
| namespace Example.MinimalApi; | ||
|
|
||
| public class CustomProcessor : BaseProcessor<Activity> | ||
| { | ||
| public override void OnEnd(Activity data) | ||
| { | ||
| Thread.Sleep(1000); // Enough time for the export to have happened | ||
|
|
||
| data.SetTag("custom-processor-tag", "ProcessedByCustomProcessor"); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 @@ | ||
| // 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 Microsoft.Extensions.DependencyInjection; | ||
|
|
||
| namespace Elastic.OpenTelemetry.Core; | ||
|
|
||
| /// <summary> | ||
| /// This is used internally to pass context around during the fluent builder configuration process. | ||
| /// </summary> | ||
| internal sealed class BuilderContext<T> where T : class | ||
| { | ||
| internal required T Builder { get; init; } | ||
|
|
||
| internal required BuilderState BuilderState { get; init; } | ||
|
|
||
| internal required BuilderOptions<T> BuilderOptions { get; init; } | ||
|
|
||
| internal IServiceCollection? Services { get; init; } | ||
| } |
This file contains hidden or 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 @@ | ||
| // 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 | ||
|
|
||
| namespace Elastic.OpenTelemetry.Core; | ||
|
|
||
| internal readonly record struct BuilderOptions<T>( | ||
| Action<T>? UserProvidedConfigureBuilder, | ||
| bool DeferAddOtlpExporter) where T : class; |
This file contains hidden or 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
109 changes: 109 additions & 0 deletions
109
src/Elastic.OpenTelemetry.Core/Diagnostics/DeferredLogger.cs
This file contains hidden or 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,109 @@ | ||
| // 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.Collections.Concurrent; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using Elastic.OpenTelemetry.Configuration; | ||
| using Microsoft.Extensions.Logging; | ||
| using Microsoft.Extensions.Logging.Abstractions; | ||
|
|
||
| namespace Elastic.OpenTelemetry.Diagnostics; | ||
|
|
||
| /// <summary> | ||
| /// Used to capture log entries before the file logger is initialized. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Log entries are stored in a concurrent queue and drained to the first file logger that is available. | ||
| /// Log entries are only captured if file logging is enabled in the options. | ||
| /// </remarks> | ||
| internal sealed class DeferredLogger : ILogger | ||
| { | ||
| private readonly bool _isEnabled = false; | ||
| private readonly LogLevel _configuredLogLevel; | ||
| private readonly ConcurrentQueue<string> _logQueue = new(); | ||
| private readonly ILogger? _additionalLogger; | ||
|
|
||
| private static readonly Lock Lock = new(); | ||
|
|
||
| /// <summary> | ||
| /// Create an instance of <see cref="DeferredLogger"/>. | ||
| /// </summary> | ||
| /// <param name="options">The options used to configure the logger.</param> | ||
| /// <param name="additionalLogger">An optional additional logger to which log entries will be written. For example, | ||
| /// this may be available in ASP.NET Core scenarios and ensures the early log entries are sent to other sinks, such | ||
| /// as the console, when available.</param> | ||
| private DeferredLogger(CompositeElasticOpenTelemetryOptions options, ILogger? additionalLogger = null) | ||
| { | ||
| _isEnabled = options.GlobalLogEnabled && options.LogTargets.HasFlag(LogTargets.File); | ||
|
|
||
| if (!_isEnabled) | ||
| return; | ||
|
|
||
| _configuredLogLevel = options.LogLevel; | ||
| _additionalLogger = additionalLogger; | ||
| } | ||
|
|
||
| public IDisposable BeginScope<TState>(TState state) where TState : notnull => NullScope.Instance; | ||
|
|
||
| public bool IsEnabled(LogLevel logLevel) => _isEnabled && _configuredLogLevel <= logLevel; | ||
|
|
||
| public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter) | ||
| { | ||
| _additionalLogger?.Log(logLevel, eventId, state, exception, formatter); | ||
|
|
||
| if (!IsEnabled(logLevel)) | ||
| return; | ||
|
|
||
| var logLine = LogFormatter.Format(logLevel, eventId, state, exception, formatter); | ||
|
|
||
| if (exception is not null) | ||
| logLine = $"{logLine}{Environment.NewLine}{exception}"; | ||
|
|
||
| _logQueue.Enqueue(logLine); | ||
| } | ||
|
|
||
| internal void DrainAndRelease(StreamWriter streamWriter) | ||
| { | ||
| using (Lock.EnterScope()) | ||
| { | ||
| while (_logQueue.TryDequeue(out var deferredLog)) | ||
| { | ||
| streamWriter.WriteLine(deferredLog); | ||
| } | ||
|
|
||
| Instance = null; | ||
| } | ||
| } | ||
|
|
||
| private static DeferredLogger? Instance; | ||
|
|
||
| internal static ILogger GetOrCreate(CompositeElasticOpenTelemetryOptions options) | ||
| { | ||
| using (Lock.EnterScope()) | ||
| { | ||
| // We want, at most, one instance of DeferredLogger for the application. | ||
| // We only create a DeferredFileLogger if file logging is enabled | ||
| if (options.GlobalLogEnabled && options.LogTargets.HasFlag(LogTargets.File)) | ||
| return Instance ??= new DeferredLogger(options, options.AdditionalLogger); | ||
|
|
||
| return NullLogger.Instance; | ||
| } | ||
| } | ||
|
|
||
| internal static bool TryGetInstance([NotNullWhen(true)] out DeferredLogger? instance) | ||
| { | ||
| instance = Instance; | ||
| return instance is not null; | ||
| } | ||
|
|
||
| private class NullScope : IDisposable | ||
| { | ||
| public static readonly NullScope Instance = new(); | ||
|
|
||
| public void Dispose() | ||
| { | ||
| // No-op | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.