Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
8d9bb6c
Support dependency injection in logging. Add AddOpenTelemetryEventSou…
CodeBlanch Jul 29, 2022
a3616bb
Merge branch 'main' into loggerprovider-dependencyinjection
CodeBlanch Jul 29, 2022
0836b09
CHANGELOG update.
CodeBlanch Jul 29, 2022
013ab59
Bug fixes and a test.
CodeBlanch Jul 29, 2022
9ee35f2
More fixes and more tests.
CodeBlanch Jul 29, 2022
c0fe823
Tweak comments for clarity.
CodeBlanch Jul 29, 2022
5b4ffc1
Added OpenTelemetryLoggerOptions.Services xml detail remarks.
CodeBlanch Jul 29, 2022
a1fe108
More tests.
CodeBlanch Jul 29, 2022
d2ba768
More tests.
CodeBlanch Jul 29, 2022
a8f8238
Test fix.
CodeBlanch Jul 29, 2022
6daf110
More tests.
CodeBlanch Jul 29, 2022
71cdfbf
Tests and fixes.
CodeBlanch Jul 29, 2022
0c73626
Warning cleanup.
CodeBlanch Jul 29, 2022
c5c93e0
Added resource test.
CodeBlanch Jul 29, 2022
3632366
Smooth out multiple resource configurations.
CodeBlanch Jul 29, 2022
e78734c
Resource chaining fix.
CodeBlanch Jul 29, 2022
8aeb08e
Remove throw for additional SetResourceBuilder calls.
CodeBlanch Jul 29, 2022
570ab37
Warning fixes.
CodeBlanch Jul 29, 2022
813bacc
Moved OpenTelemetryEventSourceLogEmitter extension to OpenTelemetryLo…
CodeBlanch Jul 30, 2022
6ffd623
Tweaks.
CodeBlanch Jul 30, 2022
fad5a6c
Switched from static to builder pattern.
CodeBlanch Aug 1, 2022
d477d8d
Tweaks.
CodeBlanch Aug 1, 2022
2c4195d
More tests.
CodeBlanch Aug 1, 2022
ce2a932
Merge remote-tracking branch 'upstream/main' into loggerprovider-depe…
CodeBlanch Aug 4, 2022
6284ff1
Fix CHANGELOG for release.
CodeBlanch Aug 4, 2022
d416e1b
Merge remote-tracking branch 'upstream/main' into loggerprovider-depe…
CodeBlanch Aug 5, 2022
2b26cb0
Tweaks.
CodeBlanch Aug 5, 2022
280ad5d
Test updates.
CodeBlanch Aug 5, 2022
251ff81
Added a detached configuration option.
CodeBlanch Aug 5, 2022
5f2b5b5
Prevent double registration to be consistent with tracer builder patt…
CodeBlanch Aug 5, 2022
28301d8
API review.
CodeBlanch Aug 5, 2022
e389a5d
Merge remote-tracking branch 'upstream/main' into loggerprovider-depe…
CodeBlanch Aug 5, 2022
7cc2865
Warning cleanup.
CodeBlanch Aug 5, 2022
8ecfdaf
Build fixes.
CodeBlanch Aug 5, 2022
7057343
Test fix.
CodeBlanch Aug 5, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
More tests.
  • Loading branch information
CodeBlanch committed Aug 1, 2022
commit 2c4195d496aeca4438ad85bedd45d3ab5941a57a
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,14 @@ internal OpenTelemetryLoggerProvider Build()

this.ApplyTo(finalOptions);

return new OpenTelemetryLoggerProvider(
var provider = new OpenTelemetryLoggerProvider(
finalOptions,
serviceProvider,
ownsServiceProvider: true);

this.ConfigurationActions = null;

return provider;
}

internal void ApplyTo(OpenTelemetryLoggerOptions other)
Expand Down
135 changes: 135 additions & 0 deletions test/OpenTelemetry.Tests/Logs/OpenTelemetryLoggerOptionsSdkTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// <copyright file="OpenTelemetryLoggerOptionsSdkTests.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 System;
using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;
using OpenTelemetry.Resources;
using Xunit;

namespace OpenTelemetry.Logs.Tests;

public class OpenTelemetryLoggerOptionsSdkTests
{
[Fact]
public void CreateLoggerProviderBuilderBuildValidProviderTest()
{
List<LogRecord> exportedItems = new();

using var provider = Sdk.CreateLoggerProviderBuilder()
.AddInMemoryExporter(exportedItems)
.Build();

Assert.NotNull(provider);

provider.CreateEmitter().Emit(new()
{
Message = "Hello world",
});

Assert.Single(exportedItems);
}

[Fact]
public void CreateLoggerProviderBuilderExtensionPointsTest()
{
int ctorConfigureInvocations = 0;
int optionsConfigureInvocations = 0;
OpenTelemetryLoggerProvider? providerFromConfigureCallback = null;

var returnedOptions = Sdk.CreateLoggerProviderBuilder(options =>
{
ctorConfigureInvocations++;

Assert.NotNull(options.Services);

options.ConfigureServices(services => services.AddSingleton<TestClass1>());
})
.AddProcessor(new CustomProcessor())
.AddProcessor<CustomProcessor>()
.ConfigureServices(services =>
{
services.AddSingleton<TestClass2>();
services.AddSingleton<CustomProcessor>();
services.AddSingleton<BaseProcessor<LogRecord>, CustomProcessor>();
services.Configure<OpenTelemetryLoggerOptions>(o =>
{
optionsConfigureInvocations++;

Assert.Null(o.Services);

Assert.Throws<NotSupportedException>(() => o.ConfigureServices(s => { }));

o.ConfigureResource(r => r.AddAttributes(new Dictionary<string, object> { ["key1"] = "value1" }));

o.ConfigureProvider((sp, p) => optionsConfigureInvocations++);
});
})
.ConfigureProvider((sp, p) =>
{
Assert.NotNull(sp);

providerFromConfigureCallback = p;

Assert.NotNull(sp.GetService<TestClass1>());
Assert.NotNull(sp.GetService<TestClass2>());
});

using var provider = returnedOptions.Build();

Assert.NotNull(provider);

Assert.Throws<NotSupportedException>(() => returnedOptions.ConfigureServices(s => { }));
Assert.Throws<NotSupportedException>(() => returnedOptions.ConfigureResource(r => { }));
Assert.Throws<NotSupportedException>(() => returnedOptions.ConfigureProvider((sp, p) => { }));
Assert.Throws<NotSupportedException>(() => returnedOptions.Build());

Assert.Equal(1, ctorConfigureInvocations);
Assert.Equal(2, optionsConfigureInvocations);
Assert.NotNull(providerFromConfigureCallback);
Assert.Equal(provider, providerFromConfigureCallback);

Assert.NotNull(provider.Resource?.Attributes);
Assert.Contains(provider.Resource!.Attributes, kvp => kvp.Key == "key1" && (string)kvp.Value == "value1");

var processor = provider.Processor as CompositeProcessor<LogRecord>;
Assert.NotNull(processor);

int count = 0;
var current = processor?.Head;
while (current != null)
{
count++;
current = current.Next;
}

Assert.Equal(3, count);
}

private sealed class TestClass1
{
}

private sealed class TestClass2
{
}

private sealed class CustomProcessor : BaseProcessor<LogRecord>
{
}
}