diff --git a/Directory.Packages.props b/Directory.Packages.props index f7775366744..30db60c0136 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -13,6 +13,7 @@ + diff --git a/src/OpenTelemetry/Logs/ILogger/OpenTelemetryLogger.cs b/src/OpenTelemetry/Logs/ILogger/OpenTelemetryLogger.cs index 8fb5af320f4..8ee0b195f6f 100644 --- a/src/OpenTelemetry/Logs/ILogger/OpenTelemetryLogger.cs +++ b/src/OpenTelemetry/Logs/ILogger/OpenTelemetryLogger.cs @@ -263,7 +263,7 @@ private LoggerInstrumentationScope(string name, string version) } public static LoggerInstrumentationScope Instance { get; } - = new("OpenTelemetry", typeof(OpenTelemetryLogger).Assembly.GetName().Version?.ToString() ?? "1.0.0"); + = new("OpenTelemetry", Sdk.InformationalVersion); public override void EmitLog(in LogRecordData data, in LogRecordAttributeList attributes) => throw new NotSupportedException(); diff --git a/src/OpenTelemetry/Logs/LogRecord.cs b/src/OpenTelemetry/Logs/LogRecord.cs index cbc5e11ff32..41232d5797d 100644 --- a/src/OpenTelemetry/Logs/LogRecord.cs +++ b/src/OpenTelemetry/Logs/LogRecord.cs @@ -386,6 +386,7 @@ internal LogRecord Copy() Data = this.Data, ILoggerData = this.ILoggerData.Copy(), Attributes = this.Attributes == null ? null : new List>(this.Attributes), + Logger = this.Logger, }; } diff --git a/src/OpenTelemetry/Resources/ResourceBuilderExtensions.cs b/src/OpenTelemetry/Resources/ResourceBuilderExtensions.cs index bc1d4c075bf..e4fed16f9ae 100644 --- a/src/OpenTelemetry/Resources/ResourceBuilderExtensions.cs +++ b/src/OpenTelemetry/Resources/ResourceBuilderExtensions.cs @@ -16,7 +16,6 @@ #nullable enable -using System.Reflection; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using OpenTelemetry.Internal; @@ -32,7 +31,7 @@ public static class ResourceBuilderExtensions { [ResourceSemanticConventions.AttributeTelemetrySdkName] = "opentelemetry", [ResourceSemanticConventions.AttributeTelemetrySdkLanguage] = "dotnet", - [ResourceSemanticConventions.AttributeTelemetrySdkVersion] = GetAssemblyInformationalVersion(), + [ResourceSemanticConventions.AttributeTelemetrySdkVersion] = Sdk.InformationalVersion, }); /// @@ -125,30 +124,5 @@ public static ResourceBuilder AddEnvironmentVariableDetector(this ResourceBuilde .AddDetectorInternal(sp => new OtelEnvResourceDetector(sp?.GetService() ?? configuration.Value)) .AddDetectorInternal(sp => new OtelServiceNameEnvVarDetector(sp?.GetService() ?? configuration.Value)); } - - private static string GetAssemblyInformationalVersion() - { - try - { - var informationalVersion = typeof(Resource).Assembly.GetCustomAttribute()?.InformationalVersion; - - if (informationalVersion == null) - { - return string.Empty; - } - - // informationalVersion could be in the following format: - // {majorVersion}.{minorVersion}.{patchVersion}.{pre-release label}.{pre-release version}.{gitHeight}.{Git SHA of current commit} - // The following parts are optional: pre-release label, pre-release version, git height, Git SHA of current commit - // for example: 1.5.0-alpha.1.40+807f703e1b4d9874a92bd86d9f2d4ebe5b5d52e4 - - var indexOfPlusSign = informationalVersion.IndexOf('+'); - return indexOfPlusSign > 0 ? informationalVersion.Substring(0, indexOfPlusSign) : informationalVersion; - } - catch (Exception) - { - return string.Empty; - } - } } } diff --git a/src/OpenTelemetry/Sdk.cs b/src/OpenTelemetry/Sdk.cs index 92d0b0a0bd4..eb02cb80e1f 100644 --- a/src/OpenTelemetry/Sdk.cs +++ b/src/OpenTelemetry/Sdk.cs @@ -17,6 +17,7 @@ #nullable enable using System.Diagnostics; +using System.Reflection; using OpenTelemetry.Context.Propagation; using OpenTelemetry.Internal; using OpenTelemetry.Logs; @@ -41,6 +42,9 @@ static Sdk() Activity.DefaultIdFormat = ActivityIdFormat.W3C; Activity.ForceDefaultIdFormat = true; SelfDiagnostics.EnsureInitialized(); + + var assemblyInformationalVersion = typeof(Sdk).Assembly.GetCustomAttribute()?.InformationalVersion; + InformationalVersion = ParseAssemblyInformationalVersion(assemblyInformationalVersion); } /// @@ -48,6 +52,8 @@ static Sdk() /// public static bool SuppressInstrumentation => SuppressInstrumentationScope.IsSuppressed; + internal static string InformationalVersion { get; } + /// /// Sets the Default TextMapPropagator. /// @@ -98,5 +104,25 @@ public static TracerProviderBuilder CreateTracerProviderBuilder() { return new TracerProviderBuilderBase(); } + + internal static string ParseAssemblyInformationalVersion(string? informationalVersion) + { + if (string.IsNullOrWhiteSpace(informationalVersion)) + { + informationalVersion = "1.0.0"; + } + + /* + * InformationalVersion will be in the following format: + * {majorVersion}.{minorVersion}.{patchVersion}.{pre-release label}.{pre-release version}.{gitHeight}+{Git SHA of current commit} + * Ex: 1.5.0-alpha.1.40+807f703e1b4d9874a92bd86d9f2d4ebe5b5d52e4 + * The following parts are optional: pre-release label, pre-release version, git height, Git SHA of current commit + */ + + var indexOfPlusSign = informationalVersion!.IndexOf('+'); + return indexOfPlusSign > 0 + ? informationalVersion.Substring(0, indexOfPlusSign) + : informationalVersion; + } } } diff --git a/test/OpenTelemetry.Tests/Logs/LogRecordTest.cs b/test/OpenTelemetry.Tests/Logs/LogRecordTest.cs index a828e7af63e..c38cfdcacca 100644 --- a/test/OpenTelemetry.Tests/Logs/LogRecordTest.cs +++ b/test/OpenTelemetry.Tests/Logs/LogRecordTest.cs @@ -14,14 +14,9 @@ // limitations under the License. // -#if !NETFRAMEWORK -using System; using System.Collections; -using System.Collections.Generic; using System.Diagnostics; -using System.Diagnostics.CodeAnalysis; using System.Globalization; -using System.Linq; using Microsoft.Extensions.Logging; using OpenTelemetry.Exporter; using OpenTelemetry.Logs; @@ -104,14 +99,15 @@ public void CheckStateForUnstructuredLog(bool includeFormattedMessage) [Theory] [InlineData(true)] [InlineData(false)] - [SuppressMessage("CA2254", "CA2254", Justification = "While you shouldn't use interpolation in a log message, this test verifies things work with it anyway.")] public void CheckStateForUnstructuredLogWithStringInterpolation(bool includeFormattedMessage) { using var loggerFactory = InitializeLoggerFactory(out List exportedItems, configure: o => o.IncludeFormattedMessage = includeFormattedMessage); var logger = loggerFactory.CreateLogger(); +#pragma warning disable CA2254 // Template should be a static expression var message = $"Hello from potato {0.99}."; logger.LogInformation(message); +#pragma warning restore CA2254 // Template should be a static expression Assert.NotNull(exportedItems[0].State); @@ -983,6 +979,23 @@ public void SeverityLogLevelTest(int logSeverity, LogLevel logLevel, int? transf } } + [Fact] + public void LogRecordInstrumentationScopeTest() + { + using var loggerFactory = InitializeLoggerFactory(out List exportedItems); + + var logger = loggerFactory.CreateLogger(); + + logger.LogInformation("Hello world!"); + + var logRecord = exportedItems.FirstOrDefault(); + + Assert.NotNull(logRecord); + Assert.NotNull(logRecord.Logger); + Assert.Equal("OpenTelemetry", logRecord.Logger.Name); + Assert.Equal(Sdk.InformationalVersion, logRecord.Logger.Version); + } + private static ILoggerFactory InitializeLoggerFactory(out List exportedItems, Action configure = null) { var items = exportedItems = new List(); @@ -1005,7 +1018,7 @@ internal struct Food public double Price { get; set; } } - private struct StructState : IReadOnlyList> + private readonly struct StructState : IReadOnlyList> { private readonly List> list; @@ -1161,4 +1174,3 @@ public override void OnEnd(LogRecord data) } } } -#endif diff --git a/test/OpenTelemetry.Tests/OpenTelemetry.Tests.csproj b/test/OpenTelemetry.Tests/OpenTelemetry.Tests.csproj index 88c75b48306..c33920aacb0 100644 --- a/test/OpenTelemetry.Tests/OpenTelemetry.Tests.csproj +++ b/test/OpenTelemetry.Tests/OpenTelemetry.Tests.csproj @@ -16,7 +16,8 @@ - + + diff --git a/test/OpenTelemetry.Tests/SdkTests.cs b/test/OpenTelemetry.Tests/SdkTests.cs new file mode 100644 index 00000000000..2a4195682e1 --- /dev/null +++ b/test/OpenTelemetry.Tests/SdkTests.cs @@ -0,0 +1,41 @@ +// +// 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.Tests; + +public class SdkTests +{ + [Theory] + [InlineData(null, "1.0.0")] + [InlineData("1.5.0", "1.5.0")] + [InlineData("1.0.0.0", "1.0.0.0")] + [InlineData("1.0-beta.1", "1.0-beta.1")] + [InlineData("1.5.0-alpha.1.40+807f703e1b4d9874a92bd86d9f2d4ebe5b5d52e4", "1.5.0-alpha.1.40")] + [InlineData("1.5.0-rc.1+807f703e1b4d9874a92bd86d9f2d4ebe5b5d52e4", "1.5.0-rc.1")] + [InlineData("8.0", "8.0")] + [InlineData("8", "8")] + [InlineData("8.0.1.18-alpha1", "8.0.1.18-alpha1")] + public void ParseAssemblyInformationalVersionTests(string? informationalVersion, string expectedVersion) + { + var actualVersion = Sdk.ParseAssemblyInformationalVersion(informationalVersion); + + Assert.Equal(expectedVersion, actualVersion); + } +}