Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 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
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,88 @@
// limitations under the License.
// </copyright>

using System.Diagnostics;
using System.Reflection;
using OpenTelemetry.AutoInstrumentation.Logging;

namespace OpenTelemetry.AutoInstrumentation.RulesEngine;

internal class DiagnosticSourceRule : Rule
{
private static IOtelLogger logger = OtelLogging.GetLogger("StartupHook");

public DiagnosticSourceRule()
{
Name = "System.Diagnostics.DiagnosticSource Validator";
Description = "Ensure that the System.Diagnostics.DiagnosticSource version is not older than the version used by the Auto-Instrumentation";
}

// This constructor is used for test purpose.
protected DiagnosticSourceRule(IOtelLogger otelLogger)
: this()
{
logger = otelLogger;
}

internal override bool Evaluate()
{
// TODO: implement checking logic
string? olderDiagnosticSourcePackageVersion = null;

try
{
// NuGet package version is not available at runtime. AssemblyVersion is available but it only changes in major version updates.
// Thus using AssemblyFileVersion to compare whether the loaded version is older than that of auto instrumentation.
var loadedDiagnosticSourceFileVersion = GetVersionFromApp();
if (loadedDiagnosticSourceFileVersion != null)
{
var autoInstrumentationDiagnosticSourceFileVersion = GetVersionFromAutoInstrumentation();
if (loadedDiagnosticSourceFileVersion < autoInstrumentationDiagnosticSourceFileVersion)
{
olderDiagnosticSourcePackageVersion = loadedDiagnosticSourceFileVersion.ToString();
}
}
}
catch (Exception ex)
{
// Exception in evaluation should not throw or crash the process.
logger.Warning($"Rule Engine: Couldn't evaluate reference to System.Diagnostics.DiagnosticSource in an app. Exception: {ex}");
return false;
}

if (olderDiagnosticSourcePackageVersion != null)
{
logger.Error($"Rule Engine: Application has direct or indirect reference to older version of System.Diagnostics.DiagnosticSource.dll {olderDiagnosticSourcePackageVersion}.");
return false;
}

logger.Information("Rule Engine: DiagnosticSourceRule evaluation success.");
return true;
}

protected virtual Version? GetVersionFromApp()
{
// Look up for type with an assembly name, will load the library.
// The loaded version depends on the app's reference to the package.
var diagnosticSourceType = Type.GetType("System.Diagnostics.DiagnosticSource, System.Diagnostics.DiagnosticSource");
if (diagnosticSourceType != null)
{
var loadedDiagnosticSourceAssembly = Assembly.GetAssembly(diagnosticSourceType);
var loadedDiagnosticSourceAssemblyFileVersionAttribute = loadedDiagnosticSourceAssembly?.GetCustomAttribute<AssemblyFileVersionAttribute>();
var versionString = loadedDiagnosticSourceAssemblyFileVersionAttribute?.Version;
if (versionString != null)
{
return new Version(versionString);
}
}

return null;
}

protected virtual Version? GetVersionFromAutoInstrumentation()
{
var autoInstrumentationDiagnosticSourceLocation = Path.Combine(StartupHook.LoaderAssemblyLocation ?? string.Empty, "System.Diagnostics.DiagnosticSource.dll");
var autoInstrumentationDiagnosticSourceFileVersionInfo = FileVersionInfo.GetVersionInfo(autoInstrumentationDiagnosticSourceLocation);
var autoInstrumentationDiagnosticSourceFileVersion = new Version(autoInstrumentationDiagnosticSourceFileVersionInfo.FileVersion);
return autoInstrumentationDiagnosticSourceFileVersion;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// <copyright file="DiagnosticSourceRuleTests.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>

using OpenTelemetry.AutoInstrumentation.Logging;
using OpenTelemetry.AutoInstrumentation.RulesEngine;
using Xunit;

namespace OpenTelemetry.AutoInstrumentation.StartupHook.Tests;
public class DiagnosticSourceRuleTests
{
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
public static IEnumerable<object[]> RuleTestData =>
new List<object[]>
{
new object[] { "6.0.0.0", "7.0.0.0", "Rule Engine: Application has direct or indirect reference to older version of System.Diagnostics.DiagnosticSource.dll 6.0.0.0.", false },
new object[] { "8.0.0.0", "7.0.0.0", "Rule Engine: DiagnosticSourceRule evaluation success.", true },
new object[] { "7.0.0.0", "7.0.0.0", "Rule Engine: DiagnosticSourceRule evaluation success.", true },
new object[] { null, "7.0.0.0", "Rule Engine: DiagnosticSourceRule evaluation success.", true },
new object[] { "7.0.0.0", null, "Rule Engine: DiagnosticSourceRule evaluation success.", true },
new object[] { null, null, "Rule Engine: DiagnosticSourceRule evaluation success.", true },
};
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.

[Theory]
[MemberData(nameof(RuleTestData))]
public void DiagnosticSourceVersion(string appVersion, string autoInstrumentationVersion, string logMessage, bool result)
{
var logger = new TestLogger();
var rule = new TestableDiagnosticSourceRule(appVersion, autoInstrumentationVersion, logger);
Assert.Equal(result, rule.Evaluate());
Assert.Equal(logMessage, logger.LogRecords[0].Message);
}
}

internal class TestableDiagnosticSourceRule : DiagnosticSourceRule
{
private readonly string appVersion;
private readonly string autoInstrumentationVersion;

public TestableDiagnosticSourceRule(string appVersion, string autoInstrumentationVersion, IOtelLogger otelLogger)
: base(otelLogger)
{
this.appVersion = appVersion;
this.autoInstrumentationVersion = autoInstrumentationVersion;
}

protected override Version? GetVersionFromApp()
{
if (appVersion == null)
{
return null;
}

return new Version(appVersion);
}

protected override Version? GetVersionFromAutoInstrumentation()
{
if (autoInstrumentationVersion == null)
{
return null;
}

return new Version(autoInstrumentationVersion);
}
}