[api-logs] Remove InstrumentationScope class (#4436)
This commit is contained in:
parent
201beab7c0
commit
9d2c31aab0
|
|
@ -1,65 +0,0 @@
|
|||
// <copyright file="InstrumentationScope.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
|
||||
|
||||
namespace OpenTelemetry;
|
||||
|
||||
/// <summary>
|
||||
/// Contains details about the library emitting telemetry.
|
||||
/// </summary>
|
||||
internal sealed class InstrumentationScope
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="InstrumentationScope"/> class.
|
||||
/// </summary>
|
||||
public InstrumentationScope()
|
||||
: this(name: null)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="InstrumentationScope"/> class.
|
||||
/// </summary>
|
||||
/// <param name="name">Optional name identifying the instrumentation library.</param>
|
||||
public InstrumentationScope(string? name)
|
||||
{
|
||||
this.Name = string.IsNullOrWhiteSpace(name)
|
||||
? string.Empty
|
||||
: name!;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name identifying the instrumentation library.
|
||||
/// </summary>
|
||||
public string Name { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the version of the instrumentation library.
|
||||
/// </summary>
|
||||
public string? Version { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the schema url of the instrumentation library.
|
||||
/// </summary>
|
||||
public string? SchemaUrl { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the attributes which should be associated with log records created
|
||||
/// by the instrumentation library.
|
||||
/// </summary>
|
||||
public IReadOnlyDictionary<string, object>? Attributes { get; init; }
|
||||
}
|
||||
|
|
@ -16,8 +16,6 @@
|
|||
|
||||
#nullable enable
|
||||
|
||||
using OpenTelemetry.Internal;
|
||||
|
||||
namespace OpenTelemetry.Logs;
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -28,20 +26,23 @@ internal abstract class Logger
|
|||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Logger"/> class.
|
||||
/// </summary>
|
||||
/// <param name="instrumentationScope"><see
|
||||
/// cref="OpenTelemetry.InstrumentationScope"/>.</param>
|
||||
protected Logger(InstrumentationScope instrumentationScope)
|
||||
/// <param name="name">Optional name identifying the instrumentation library.</param>
|
||||
protected Logger(string? name)
|
||||
{
|
||||
Guard.ThrowIfNull(instrumentationScope);
|
||||
|
||||
this.InstrumentationScope = instrumentationScope;
|
||||
this.Name = string.IsNullOrWhiteSpace(name)
|
||||
? string.Empty
|
||||
: name!;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="OpenTelemetry.InstrumentationScope"/> associated
|
||||
/// with the logger.
|
||||
/// Gets the name identifying the instrumentation library.
|
||||
/// </summary>
|
||||
public InstrumentationScope InstrumentationScope { get; }
|
||||
public string Name { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the version of the instrumentation library.
|
||||
/// </summary>
|
||||
public string? Version { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Emit a log.
|
||||
|
|
@ -51,4 +52,10 @@ internal abstract class Logger
|
|||
public abstract void EmitLog(
|
||||
in LogRecordData data,
|
||||
in LogRecordAttributeList attributes = default);
|
||||
|
||||
internal void SetInstrumentationScope(
|
||||
string? version)
|
||||
{
|
||||
this.Version = version;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 given instrumentation scope.
|
||||
/// Gets a logger with the given name and version.
|
||||
/// </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="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>
|
||||
/// Try to create a logger with the given name.
|
||||
/// </summary>
|
||||
/// <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(
|
||||
string? name,
|
||||
#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
|
||||
[NotNullWhen(true)]
|
||||
#endif
|
||||
out Logger? logger)
|
||||
{
|
||||
logger = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ namespace OpenTelemetry.Logs;
|
|||
internal sealed class NoopLogger : Logger
|
||||
{
|
||||
public NoopLogger()
|
||||
: base(instrumentationScope: new())
|
||||
: base(name: null)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -14,6 +14,8 @@
|
|||
// limitations under the License.
|
||||
// </copyright>
|
||||
|
||||
#nullable enable
|
||||
|
||||
using System.Diagnostics;
|
||||
using Xunit;
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue