-
Notifications
You must be signed in to change notification settings - Fork 780
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
[api-logs] Remove InstrumentationScope class #4436
Changes from all commits
22d65e2
a49971b
bbb1af7
65bed28
4885e46
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,10 @@ | |
|
||
#nullable enable | ||
|
||
#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER | ||
using System.Diagnostics.CodeAnalysis; | ||
#endif | ||
|
||
namespace OpenTelemetry.Logs; | ||
|
||
/// <summary> | ||
|
@@ -25,33 +29,53 @@ internal class LoggerProvider : BaseProvider | |
{ | ||
private static readonly NoopLogger NoopLogger = new(); | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="LoggerProvider"/> class. | ||
/// </summary> | ||
protected LoggerProvider() | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Gets a logger. | ||
/// </summary> | ||
/// <returns><see cref="Logger"/> instance.</returns> | ||
public Logger GetLogger() | ||
=> this.GetLogger(new InstrumentationScope()); | ||
=> this.GetLogger(name: null, version: null); | ||
|
||
/// <summary> | ||
/// Gets a logger with the given name. | ||
/// </summary> | ||
/// <param name="name">Optional name identifying the instrumentation library.</param> | ||
/// <returns><see cref="Logger"/> instance.</returns> | ||
public Logger GetLogger(string name) | ||
=> this.GetLogger(new InstrumentationScope(name)); | ||
public Logger GetLogger(string? name) | ||
=> this.GetLogger(name, version: null); | ||
|
||
/// <summary> | ||
/// Gets a logger with the given name and version. | ||
/// </summary> | ||
/// <param name="name">Optional name identifying the instrumentation library.</param> | ||
/// <param name="version">Optional version of the instrumentation library.</param> | ||
/// <returns><see cref="Logger"/> instance.</returns> | ||
public Logger GetLogger(string? name, string? version) | ||
{ | ||
if (!this.TryCreateLogger(name, out var logger)) | ||
{ | ||
return NoopLogger; | ||
} | ||
|
||
logger!.SetInstrumentationScope(version); | ||
|
||
return logger; | ||
} | ||
|
||
/// <summary> | ||
/// Gets a logger with given instrumentation scope. | ||
/// Try to create a logger with the given name. | ||
/// </summary> | ||
/// <param name="instrumentationScope"><see cref="InstrumentationScope"/>.</param> | ||
/// <returns><see cref="Logger"/>.</returns> | ||
public virtual Logger GetLogger(InstrumentationScope instrumentationScope) | ||
=> NoopLogger; | ||
/// <param name="name">Optional name identifying the instrumentation library.</param> | ||
/// <param name="logger"><see cref="Logger"/>.</param> | ||
/// <returns><see langword="true"/> if the logger was created.</returns> | ||
protected virtual bool TryCreateLogger( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This
|
||
string? name, | ||
#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A note about this condition...
|
||
[NotNullWhen(true)] | ||
#endif | ||
out Logger? logger) | ||
{ | ||
logger = null; | ||
return false; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// <copyright file="LoggerProviderTests.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 Xunit; | ||
|
||
namespace OpenTelemetry.Logs.Tests; | ||
|
||
public sealed class LoggerProviderTests | ||
{ | ||
[Fact] | ||
public void NoopLoggerReturnedTest() | ||
{ | ||
using var provider = new LoggerProvider(); | ||
|
||
var logger = provider.GetLogger(name: "TestLogger", version: "Version"); | ||
|
||
Assert.NotNull(logger); | ||
Assert.Equal(typeof(NoopLogger), logger.GetType()); | ||
|
||
Assert.Equal(string.Empty, logger.Name); | ||
Assert.Null(logger.Version); | ||
} | ||
|
||
[Fact] | ||
public void LoggerReturnedWithInstrumentationScopeTest() | ||
{ | ||
using var provider = new TestLoggerProvider(); | ||
|
||
var logger = provider.GetLogger(name: "TestLogger", version: "Version"); | ||
|
||
Assert.NotNull(logger); | ||
Assert.Equal(typeof(TestLogger), logger.GetType()); | ||
|
||
Assert.Equal("TestLogger", logger.Name); | ||
Assert.Equal("Version", logger.Version); | ||
|
||
logger = provider.GetLogger(name: "TestLogger"); | ||
|
||
Assert.NotNull(logger); | ||
Assert.Equal(typeof(TestLogger), logger.GetType()); | ||
|
||
Assert.Equal("TestLogger", logger.Name); | ||
Assert.Null(logger.Version); | ||
|
||
logger = provider.GetLogger(); | ||
|
||
Assert.NotNull(logger); | ||
Assert.Equal(typeof(TestLogger), logger.GetType()); | ||
|
||
Assert.Equal(string.Empty, logger.Name); | ||
Assert.Null(logger.Version); | ||
} | ||
|
||
private sealed class TestLoggerProvider : LoggerProvider | ||
{ | ||
protected override bool TryCreateLogger(string? name, out Logger? logger) | ||
{ | ||
logger = new TestLogger(name); | ||
return true; | ||
} | ||
} | ||
|
||
private sealed class TestLogger : Logger | ||
{ | ||
public TestLogger(string? name) | ||
: base(name) | ||
{ | ||
} | ||
|
||
public override void EmitLog(in LogRecordData data, in LogRecordAttributeList attributes = default) | ||
{ | ||
} | ||
} | ||
} |
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.
XML comments can be clean up in a later PR, but just noting that...
We can still refer to this as instrumentation scope here. Something like
or even just dropping the term instrumentation scope, which mimics the docs for Meter
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 don't disagree with you that they suck 🤣 but I did keep it ~consistent with what we have in TracerProvider:
opentelemetry-dotnet/src/OpenTelemetry.Api/Trace/TracerProvider.cs
Lines 41 to 42 in 2914002
I think I'll leave it alone for now and then in a follow-up we can fix it all up?
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.
Totally, and you're right we'd probably benefit from an editorial pass over our XML docs in general.