-
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
[sdk-logs] Tests and bug fixes for logging builder & provider #4467
Merged
CodeBlanch
merged 5 commits into
open-telemetry:main
from
CodeBlanch:sdk-logs-builderandprovidertests
May 8, 2023
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fd8452b
Tests and bug fixes for logging builder & provider.
CodeBlanch beab070
Warning fix.
CodeBlanch 66d2e75
Merge branch 'main' into sdk-logs-builderandprovidertests
CodeBlanch 79aedbf
Merge branch 'main' into sdk-logs-builderandprovidertests
CodeBlanch a1d9712
Code review.
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
185 changes: 185 additions & 0 deletions
185
test/OpenTelemetry.Tests/Logs/LoggerProviderBuilderExtensionsTests.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,185 @@ | ||
// <copyright file="LoggerProviderBuilderExtensionsTests.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> | ||
|
||
#nullable enable | ||
|
||
using Microsoft.Extensions.DependencyInjection; | ||
using OpenTelemetry.Resources; | ||
using Xunit; | ||
|
||
namespace OpenTelemetry.Logs.Tests; | ||
|
||
public sealed class LoggerProviderBuilderExtensionsTests | ||
{ | ||
[Fact] | ||
public void LoggerProviderBuilderAddInstrumentationTest() | ||
{ | ||
List<object>? instrumentation = null; | ||
|
||
using (var provider = Sdk.CreateLoggerProviderBuilder() | ||
.AddInstrumentation<CustomInstrumentation>() | ||
.AddInstrumentation((sp, provider) => new CustomInstrumentation() { Provider = provider }) | ||
.AddInstrumentation(new CustomInstrumentation()) | ||
.Build() as LoggerProviderSdk) | ||
{ | ||
Assert.NotNull(provider); | ||
|
||
Assert.Equal(3, provider.Instrumentations.Count); | ||
|
||
Assert.Null(((CustomInstrumentation)provider.Instrumentations[0]).Provider); | ||
Assert.False(((CustomInstrumentation)provider.Instrumentations[0]).Disposed); | ||
|
||
Assert.NotNull(((CustomInstrumentation)provider.Instrumentations[1]).Provider); | ||
Assert.False(((CustomInstrumentation)provider.Instrumentations[1]).Disposed); | ||
|
||
Assert.Null(((CustomInstrumentation)provider.Instrumentations[2]).Provider); | ||
Assert.False(((CustomInstrumentation)provider.Instrumentations[2]).Disposed); | ||
|
||
instrumentation = new List<object>(provider.Instrumentations); | ||
} | ||
|
||
Assert.True(((CustomInstrumentation)instrumentation[0]).Disposed); | ||
Assert.True(((CustomInstrumentation)instrumentation[1]).Disposed); | ||
Assert.True(((CustomInstrumentation)instrumentation[2]).Disposed); | ||
} | ||
|
||
[Theory] | ||
[InlineData(true)] | ||
[InlineData(false)] | ||
public void LoggerProviderBuilderNestedResolutionUsingBuilderTest(bool callNestedConfigure) | ||
{ | ||
bool innerTestExecuted = false; | ||
|
||
using var provider = Sdk.CreateLoggerProviderBuilder() | ||
.ConfigureServices(services => | ||
{ | ||
if (callNestedConfigure) | ||
{ | ||
services.ConfigureOpenTelemetryLoggerProvider((sp, builder) => { }); | ||
} | ||
}) | ||
.ConfigureBuilder((sp, builder) => | ||
{ | ||
innerTestExecuted = true; | ||
Assert.Throws<NotSupportedException>(() => sp.GetService<LoggerProvider>()); | ||
}) | ||
.Build(); | ||
|
||
Assert.True(innerTestExecuted); | ||
|
||
Assert.Throws<NotSupportedException>(() => provider.GetServiceProvider()?.GetService<LoggerProvider>()); | ||
} | ||
|
||
[Fact] | ||
public void LoggerProviderBuilderSetResourceBuilderTests() | ||
{ | ||
using var provider = Sdk.CreateLoggerProviderBuilder() | ||
.SetResourceBuilder(ResourceBuilder | ||
.CreateEmpty() | ||
.AddAttributes(new[] { new KeyValuePair<string, object>("key1", "value1") })) | ||
.Build() as LoggerProviderSdk; | ||
|
||
Assert.NotNull(provider); | ||
|
||
Assert.NotNull(provider.Resource); | ||
Assert.Contains(provider.Resource.Attributes, value => value.Key == "key1" && (string)value.Value == "value1"); | ||
} | ||
|
||
[Fact] | ||
public void LoggerProviderBuilderConfigureResourceBuilderTests() | ||
{ | ||
using var provider = Sdk.CreateLoggerProviderBuilder() | ||
.ConfigureResource(resource => resource | ||
.Clear() | ||
.AddAttributes(new[] { new KeyValuePair<string, object>("key1", "value1") })) | ||
.Build() as LoggerProviderSdk; | ||
|
||
Assert.NotNull(provider); | ||
|
||
Assert.NotNull(provider.Resource); | ||
Assert.Contains(provider.Resource.Attributes, value => value.Key == "key1" && (string)value.Value == "value1"); | ||
} | ||
|
||
[Fact] | ||
public void LoggerProviderBuilderAddProcessorTest() | ||
{ | ||
List<CustomProcessor> processors = new(); | ||
|
||
using (var provider = Sdk.CreateLoggerProviderBuilder() | ||
.AddProcessor<CustomProcessor>() | ||
.AddProcessor(sp => new CustomProcessor()) | ||
.AddProcessor(new CustomProcessor()) | ||
.Build() as LoggerProviderSdk) | ||
{ | ||
Assert.NotNull(provider); | ||
Assert.NotNull(provider.Processor); | ||
|
||
var compositeProcessor = provider.Processor as CompositeProcessor<LogRecord>; | ||
|
||
Assert.NotNull(compositeProcessor); | ||
|
||
var current = compositeProcessor.Head; | ||
while (current != null) | ||
{ | ||
var processor = current.Value as CustomProcessor; | ||
Assert.NotNull(processor); | ||
|
||
processors.Add(processor); | ||
Assert.False(processor.Disposed); | ||
|
||
current = current.Next; | ||
} | ||
} | ||
|
||
Assert.Equal(3, processors.Count); | ||
|
||
foreach (var processor in processors) | ||
{ | ||
Assert.True(processor.Disposed); | ||
} | ||
} | ||
|
||
private sealed class CustomInstrumentation : IDisposable | ||
{ | ||
public bool Disposed; | ||
public LoggerProvider? Provider; | ||
|
||
public void Dispose() | ||
{ | ||
this.Disposed = true; | ||
} | ||
} | ||
|
||
private sealed class CustomProcessor : BaseProcessor<LogRecord> | ||
{ | ||
public bool Disposed; | ||
|
||
protected override void Dispose(bool disposing) | ||
{ | ||
this.Disposed = true; | ||
|
||
base.Dispose(disposing); | ||
} | ||
} | ||
|
||
private sealed class CustomExporter : BaseExporter<LogRecord> | ||
{ | ||
public override ExportResult Export(in Batch<LogRecord> batch) | ||
{ | ||
return ExportResult.Success; | ||
} | ||
} | ||
} |
142 changes: 142 additions & 0 deletions
142
test/OpenTelemetry.Tests/Logs/LoggerProviderSdkTests.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,142 @@ | ||
// <copyright file="LoggerProviderSdkTests.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> | ||
|
||
#nullable enable | ||
|
||
using OpenTelemetry.Exporter; | ||
using Xunit; | ||
|
||
namespace OpenTelemetry.Logs.Tests; | ||
|
||
public sealed class LoggerProviderSdkTests | ||
{ | ||
[Fact] | ||
public void ForceFlushTest() | ||
{ | ||
using var provider = Sdk.CreateLoggerProviderBuilder().Build() as LoggerProviderSdk; | ||
|
||
Assert.NotNull(provider); | ||
|
||
Assert.True(provider.ForceFlush()); | ||
|
||
List<LogRecord> exportedItems = new(); | ||
|
||
provider.AddProcessor(new BatchLogRecordExportProcessor(new InMemoryExporter<LogRecord>(exportedItems))); | ||
|
||
var logger = provider.GetLogger("TestLogger"); | ||
|
||
logger.EmitLog(new() { Body = "Hello world" }); | ||
|
||
Assert.Empty(exportedItems); | ||
|
||
Assert.True(provider.ForceFlush()); | ||
|
||
Assert.Single(exportedItems); | ||
} | ||
|
||
[Fact] | ||
public void ThreadStaticPoolUsedByProviderTests() | ||
{ | ||
using var provider1 = Sdk.CreateLoggerProviderBuilder().Build() as LoggerProviderSdk; | ||
|
||
Assert.NotNull(provider1); | ||
|
||
Assert.Equal(LogRecordThreadStaticPool.Instance, provider1.LogRecordPool); | ||
|
||
using var provider2 = Sdk.CreateLoggerProviderBuilder() | ||
.AddProcessor(new SimpleLogRecordExportProcessor(new NoopExporter())) | ||
.Build() as LoggerProviderSdk; | ||
|
||
Assert.NotNull(provider2); | ||
|
||
Assert.Equal(LogRecordThreadStaticPool.Instance, provider2.LogRecordPool); | ||
|
||
using var provider3 = Sdk.CreateLoggerProviderBuilder() | ||
.AddProcessor(new SimpleLogRecordExportProcessor(new NoopExporter())) | ||
.AddProcessor(new SimpleLogRecordExportProcessor(new NoopExporter())) | ||
.Build() as LoggerProviderSdk; | ||
|
||
Assert.NotNull(provider3); | ||
|
||
Assert.Equal(LogRecordThreadStaticPool.Instance, provider3.LogRecordPool); | ||
} | ||
|
||
[Fact] | ||
public void SharedPoolUsedByProviderTests() | ||
{ | ||
using var provider1 = Sdk.CreateLoggerProviderBuilder() | ||
.AddProcessor(new BatchLogRecordExportProcessor(new NoopExporter())) | ||
.Build() as LoggerProviderSdk; | ||
|
||
Assert.NotNull(provider1); | ||
|
||
Assert.Equal(LogRecordSharedPool.Current, provider1.LogRecordPool); | ||
|
||
using var provider2 = Sdk.CreateLoggerProviderBuilder() | ||
.AddProcessor(new SimpleLogRecordExportProcessor(new NoopExporter())) | ||
.AddProcessor(new BatchLogRecordExportProcessor(new NoopExporter())) | ||
.Build() as LoggerProviderSdk; | ||
|
||
Assert.NotNull(provider2); | ||
|
||
Assert.Equal(LogRecordSharedPool.Current, provider2.LogRecordPool); | ||
|
||
using var provider3 = Sdk.CreateLoggerProviderBuilder() | ||
.AddProcessor(new SimpleLogRecordExportProcessor(new NoopExporter())) | ||
.AddProcessor(new CompositeProcessor<LogRecord>(new BaseProcessor<LogRecord>[] | ||
{ | ||
new SimpleLogRecordExportProcessor(new NoopExporter()), | ||
new BatchLogRecordExportProcessor(new NoopExporter()), | ||
})) | ||
.Build() as LoggerProviderSdk; | ||
|
||
Assert.NotNull(provider3); | ||
|
||
Assert.Equal(LogRecordSharedPool.Current, provider3.LogRecordPool); | ||
} | ||
|
||
[Fact] | ||
public void AddProcessorTest() | ||
{ | ||
using var provider = Sdk.CreateLoggerProviderBuilder() | ||
.Build() as LoggerProviderSdk; | ||
|
||
Assert.NotNull(provider); | ||
Assert.Null(provider.Processor); | ||
|
||
provider.AddProcessor(new NoopProcessor()); | ||
|
||
Assert.NotNull(provider.Processor); | ||
Assert.True(provider.Processor is NoopProcessor); | ||
|
||
provider.AddProcessor(new NoopProcessor()); | ||
|
||
Assert.NotNull(provider.Processor); | ||
Assert.True(provider.Processor is CompositeProcessor<LogRecord>); | ||
} | ||
|
||
private sealed class NoopProcessor : BaseProcessor<LogRecord> | ||
{ | ||
} | ||
|
||
private sealed class NoopExporter : BaseExporter<LogRecord> | ||
{ | ||
public override ExportResult Export(in Batch<LogRecord> batch) | ||
{ | ||
return ExportResult.Success; | ||
} | ||
} | ||
} |
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.
Do we not want to assert something specific based on
callNestedConfigure
?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 wrote that test so long ago I can't remember, TBH. I added an assert in the logging one and its cousins in tracing & metrics. I figured it couldn't hurt 😄