diff --git a/src/OpenTelemetry.Api/InstrumentationScope.cs b/src/OpenTelemetry.Api/InstrumentationScope.cs deleted file mode 100644 index 9b6540d032e..00000000000 --- a/src/OpenTelemetry.Api/InstrumentationScope.cs +++ /dev/null @@ -1,65 +0,0 @@ -// -// 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. -// - -#nullable enable - -namespace OpenTelemetry; - -/// -/// Contains details about the library emitting telemetry. -/// -internal sealed class InstrumentationScope -{ - /// - /// Initializes a new instance of the class. - /// - public InstrumentationScope() - : this(name: null) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// Optional name identifying the instrumentation library. - public InstrumentationScope(string? name) - { - this.Name = string.IsNullOrWhiteSpace(name) - ? string.Empty - : name!; - } - - /// - /// Gets the name identifying the instrumentation library. - /// - public string Name { get; } - - /// - /// Gets the version of the instrumentation library. - /// - public string? Version { get; init; } - - /// - /// Gets the schema url of the instrumentation library. - /// - public string? SchemaUrl { get; init; } - - /// - /// Gets the attributes which should be associated with log records created - /// by the instrumentation library. - /// - public IReadOnlyDictionary? Attributes { get; init; } -} diff --git a/src/OpenTelemetry.Api/Logs/Logger.cs b/src/OpenTelemetry.Api/Logs/Logger.cs index 6addbadd20e..acbd19906c3 100644 --- a/src/OpenTelemetry.Api/Logs/Logger.cs +++ b/src/OpenTelemetry.Api/Logs/Logger.cs @@ -16,8 +16,6 @@ #nullable enable -using OpenTelemetry.Internal; - namespace OpenTelemetry.Logs; /// @@ -28,20 +26,23 @@ internal abstract class Logger /// /// Initializes a new instance of the class. /// - /// . - protected Logger(InstrumentationScope instrumentationScope) + /// Optional name identifying the instrumentation library. + protected Logger(string? name) { - Guard.ThrowIfNull(instrumentationScope); - - this.InstrumentationScope = instrumentationScope; + this.Name = string.IsNullOrWhiteSpace(name) + ? string.Empty + : name!; } /// - /// Gets the associated - /// with the logger. + /// Gets the name identifying the instrumentation library. + /// + public string Name { get; } + + /// + /// Gets the version of the instrumentation library. /// - public InstrumentationScope InstrumentationScope { get; } + public string? Version { get; private set; } /// /// Emit a log. @@ -51,4 +52,10 @@ protected Logger(InstrumentationScope instrumentationScope) public abstract void EmitLog( in LogRecordData data, in LogRecordAttributeList attributes = default); + + internal void SetInstrumentationScope( + string? version) + { + this.Version = version; + } } diff --git a/src/OpenTelemetry.Api/Logs/LoggerProvider.cs b/src/OpenTelemetry.Api/Logs/LoggerProvider.cs index 6e0f07658b6..194f45f4382 100644 --- a/src/OpenTelemetry.Api/Logs/LoggerProvider.cs +++ b/src/OpenTelemetry.Api/Logs/LoggerProvider.cs @@ -16,6 +16,10 @@ #nullable enable +#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER +using System.Diagnostics.CodeAnalysis; +#endif + namespace OpenTelemetry.Logs; /// @@ -25,33 +29,53 @@ internal class LoggerProvider : BaseProvider { private static readonly NoopLogger NoopLogger = new(); - /// - /// Initializes a new instance of the class. - /// - protected LoggerProvider() - { - } - /// /// Gets a logger. /// /// instance. public Logger GetLogger() - => this.GetLogger(new InstrumentationScope()); + => this.GetLogger(name: null, version: null); /// /// Gets a logger with the given name. /// /// Optional name identifying the instrumentation library. /// instance. - public Logger GetLogger(string name) - => this.GetLogger(new InstrumentationScope(name)); + public Logger GetLogger(string? name) + => this.GetLogger(name, version: null); + + /// + /// Gets a logger with the given name and version. + /// + /// Optional name identifying the instrumentation library. + /// Optional version of the instrumentation library. + /// instance. + public Logger GetLogger(string? name, string? version) + { + if (!this.TryCreateLogger(name, out var logger)) + { + return NoopLogger; + } + + logger!.SetInstrumentationScope(version); + + return logger; + } /// - /// Gets a logger with given instrumentation scope. + /// Try to create a logger with the given name. /// - /// . - /// . - public virtual Logger GetLogger(InstrumentationScope instrumentationScope) - => NoopLogger; + /// Optional name identifying the instrumentation library. + /// . + /// if the logger was created. + protected virtual bool TryCreateLogger( + string? name, +#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER + [NotNullWhen(true)] +#endif + out Logger? logger) + { + logger = null; + return false; + } } diff --git a/src/OpenTelemetry.Api/Logs/NoopLogger.cs b/src/OpenTelemetry.Api/Logs/NoopLogger.cs index 62c74ae8e6e..3493687647a 100644 --- a/src/OpenTelemetry.Api/Logs/NoopLogger.cs +++ b/src/OpenTelemetry.Api/Logs/NoopLogger.cs @@ -21,7 +21,7 @@ namespace OpenTelemetry.Logs; internal sealed class NoopLogger : Logger { public NoopLogger() - : base(instrumentationScope: new()) + : base(name: null) { } diff --git a/src/OpenTelemetry.Api/Internal/Shims/IsExternalInit.cs b/src/OpenTelemetry/Internal/Shims/IsExternalInit.cs similarity index 100% rename from src/OpenTelemetry.Api/Internal/Shims/IsExternalInit.cs rename to src/OpenTelemetry/Internal/Shims/IsExternalInit.cs diff --git a/test/OpenTelemetry.Api.Tests/Logs/LogRecordDataTests.cs b/test/OpenTelemetry.Api.Tests/Logs/LogRecordDataTests.cs index 09d15e2b0f8..700e68b03ef 100644 --- a/test/OpenTelemetry.Api.Tests/Logs/LogRecordDataTests.cs +++ b/test/OpenTelemetry.Api.Tests/Logs/LogRecordDataTests.cs @@ -14,6 +14,8 @@ // limitations under the License. // +#nullable enable + using System.Diagnostics; using Xunit; diff --git a/test/OpenTelemetry.Api.Tests/Logs/LoggerProviderTests.cs b/test/OpenTelemetry.Api.Tests/Logs/LoggerProviderTests.cs new file mode 100644 index 00000000000..b780ca16fa1 --- /dev/null +++ b/test/OpenTelemetry.Api.Tests/Logs/LoggerProviderTests.cs @@ -0,0 +1,89 @@ +// +// 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. +// + +#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) + { + } + } +}