Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<PackageVersion Include="Microsoft.AspNetCore.Http.Features" Version="[2.1.1,6.0)" />
<PackageVersion Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" Version="[3.3.3]" />
<PackageVersion Include="Microsoft.CodeCoverage" Version="[17.4.1]" />
<PackageVersion Include="Microsoft.CSharp" Version="[4.7.0]" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="[3.1.0,)" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="[3.1.0,)" />
<PackageVersion Include="Microsoft.Extensions.Hosting.Abstractions" Version="[2.1.0,)" />
Expand Down
2 changes: 1 addition & 1 deletion src/OpenTelemetry/Logs/ILogger/OpenTelemetryLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/OpenTelemetry/Logs/LogRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ internal LogRecord Copy()
Data = this.Data,
ILoggerData = this.ILoggerData.Copy(),
Attributes = this.Attributes == null ? null : new List<KeyValuePair<string, object?>>(this.Attributes),
Logger = this.Logger,
};
}

Expand Down
28 changes: 1 addition & 27 deletions src/OpenTelemetry/Resources/ResourceBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

#nullable enable

using System.Reflection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using OpenTelemetry.Internal;
Expand All @@ -32,7 +31,7 @@ public static class ResourceBuilderExtensions
{
[ResourceSemanticConventions.AttributeTelemetrySdkName] = "opentelemetry",
[ResourceSemanticConventions.AttributeTelemetrySdkLanguage] = "dotnet",
[ResourceSemanticConventions.AttributeTelemetrySdkVersion] = GetAssemblyInformationalVersion(),
[ResourceSemanticConventions.AttributeTelemetrySdkVersion] = Sdk.InformationalVersion,
});

/// <summary>
Expand Down Expand Up @@ -125,30 +124,5 @@ public static ResourceBuilder AddEnvironmentVariableDetector(this ResourceBuilde
.AddDetectorInternal(sp => new OtelEnvResourceDetector(sp?.GetService<IConfiguration>() ?? configuration.Value))
.AddDetectorInternal(sp => new OtelServiceNameEnvVarDetector(sp?.GetService<IConfiguration>() ?? configuration.Value));
}

private static string GetAssemblyInformationalVersion()
{
try
{
var informationalVersion = typeof(Resource).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.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;
}
}
}
}
21 changes: 21 additions & 0 deletions src/OpenTelemetry/Sdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#nullable enable

using System.Diagnostics;
using System.Reflection;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Internal;
using OpenTelemetry.Logs;
Expand All @@ -41,13 +42,33 @@ static Sdk()
Activity.DefaultIdFormat = ActivityIdFormat.W3C;
Activity.ForceDefaultIdFormat = true;
SelfDiagnostics.EnsureInitialized();

var assemblyInformationalVersion = typeof(Sdk).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion;
if (string.IsNullOrWhiteSpace(assemblyInformationalVersion))
{
assemblyInformationalVersion = "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 = assemblyInformationalVersion!.IndexOf('+');
InformationalVersion = indexOfPlusSign > 0
? assemblyInformationalVersion.Substring(0, indexOfPlusSign)
: assemblyInformationalVersion;
}

/// <summary>
/// Gets a value indicating whether instrumentation is suppressed (disabled).
/// </summary>
public static bool SuppressInstrumentation => SuppressInstrumentationScope.IsSuppressed;

internal static string InformationalVersion { get; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
internal static string InformationalVersion { get; }
internal static readonly string InformationalVersion;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not opposed to this, but why? 🤣 A property with just a getter ({ get; }) is readonly for the backing generated by the compiler IIRC.

Copy link
Contributor

@utpilla utpilla Jul 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but why?

Just because it's more obvious and intuitive when you use readonly field 😀


/// <summary>
/// Sets the Default TextMapPropagator.
/// </summary>
Expand Down
28 changes: 20 additions & 8 deletions test/OpenTelemetry.Tests/Logs/LogRecordTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,9 @@
// limitations under the License.
// </copyright>

#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;
Expand Down Expand Up @@ -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<LogRecord> exportedItems, configure: o => o.IncludeFormattedMessage = includeFormattedMessage);
var logger = loggerFactory.CreateLogger<LogRecordTest>();

#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);

Expand Down Expand Up @@ -983,6 +979,23 @@ public void SeverityLogLevelTest(int logSeverity, LogLevel logLevel, int? transf
}
}

[Fact]
public void LogRecordInstrumentationScopeTest()
{
using var loggerFactory = InitializeLoggerFactory(out List<LogRecord> exportedItems);

var logger = loggerFactory.CreateLogger<LogRecordTest>();

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<LogRecord> exportedItems, Action<OpenTelemetryLoggerOptions> configure = null)
{
var items = exportedItems = new List<LogRecord>();
Expand All @@ -1005,7 +1018,7 @@ internal struct Food
public double Price { get; set; }
}

private struct StructState : IReadOnlyList<KeyValuePair<string, object>>
private readonly struct StructState : IReadOnlyList<KeyValuePair<string, object>>
{
private readonly List<KeyValuePair<string, object>> list;

Expand Down Expand Up @@ -1161,4 +1174,3 @@ public override void OnEnd(LogRecord data)
}
}
}
#endif
3 changes: 2 additions & 1 deletion test/OpenTelemetry.Tests/OpenTelemetry.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" />
<PackageReference Include="Microsoft.Extensions.Logging" />
<PackageReference Include="Microsoft.CSharp" Condition="'$(TargetFramework)' == 'net462'" />
</ItemGroup>

<ItemGroup>
Expand Down