Skip to content

Commit

Permalink
Remove rule dedicated to S.D.DS validation
Browse files Browse the repository at this point in the history
  • Loading branch information
pjanotti committed Sep 13, 2023
1 parent 38da9b6 commit b33e0e1
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 203 deletions.
13 changes: 8 additions & 5 deletions build/Build.Steps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -557,18 +557,17 @@ DotNetBuildSettings BuildTestApplication(DotNetBuildSettings x) =>
.Executes(() =>
{
var netPath = TracerHomeDirectory / "net";
var ruleEngineJsonFilePath = netPath / "ruleEngine.json";
var ruleEngineJsonNugetFilePath = RootDirectory / "nuget" / "OpenTelemetry.AutoInstrumentation" / "contentFiles" / "any" / "any" / "RuleEngine.json";
var fileInfoList = new List<object>();
var files = Directory.GetFiles(netPath);
var fileInfoList = new List<object>(files.Length);

foreach (string file in files)
{
var fileName = Path.GetFileNameWithoutExtension(file);
var fileVersion = FileVersionInfo.GetVersionInfo(file).FileVersion;

if (fileName.StartsWith("OpenTelemetry.") && !fileName.StartsWith("OpenTelemetry.Api") && !fileName.StartsWith("OpenTelemetry.AutoInstrumentation"))
if (fileName == "System.Diagnostics.DiagnosticSource" ||
(fileName.StartsWith("OpenTelemetry.") && !fileName.StartsWith("OpenTelemetry.Api") && !fileName.StartsWith("OpenTelemetry.AutoInstrumentation")))
{
var fileVersion = FileVersionInfo.GetVersionInfo(file).FileVersion;
fileInfoList.Add(new
{
FileName = fileName,
Expand All @@ -579,7 +578,11 @@ DotNetBuildSettings BuildTestApplication(DotNetBuildSettings x) =>

JsonSerializerOptions options = new JsonSerializerOptions { WriteIndented = true };
string jsonContent = JsonSerializer.Serialize(fileInfoList, options);

var ruleEngineJsonFilePath = netPath / "ruleEngine.json";
File.WriteAllText(ruleEngineJsonFilePath, jsonContent);

var ruleEngineJsonNugetFilePath = RootDirectory / "nuget" / "OpenTelemetry.AutoInstrumentation" / "contentFiles" / "any" / "any" / "RuleEngine.json";
File.Delete(ruleEngineJsonNugetFilePath);
File.Copy(ruleEngineJsonFilePath, ruleEngineJsonNugetFilePath);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="InstrumentationAssemblyRule.cs" company="OpenTelemetry Authors">
// <copyright file="AssemblyFileVersionRule.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -21,14 +21,14 @@

namespace OpenTelemetry.AutoInstrumentation.RulesEngine;

internal class InstrumentationAssemblyRule : Rule
internal class AssemblyFileVersionRule : Rule
{
private static readonly IOtelLogger Logger = OtelLogging.GetLogger("StartupHook");

public InstrumentationAssemblyRule()
public AssemblyFileVersionRule()
{
Name = "Instrumentation Assembly Validator";
Description = "Ensure that the version of the OpenTelemetry Instrumentation libraries is not older than the version used by Automatic Instrumentation.";
Name = "Assembly File Version Validator";
Description = "Ensure that the version of key assemblies is not older than the version used by Automatic Instrumentation.";
}

internal override bool Evaluate()
Expand All @@ -41,6 +41,7 @@ internal override bool Evaluate()
var ruleEngineContent = File.ReadAllText(ruleEngineFileLocation);
var ruleFileInfoList = JsonSerializer.Deserialize<List<RuleFileInfo>>(ruleEngineContent);
var entryAssembly = Assembly.GetEntryAssembly();
Logger.Information($"Rule Engine: Entry Assembly: {entryAssembly?.FullName}");
var referencedAssemblies = entryAssembly?.GetReferencedAssemblies();

if (referencedAssemblies == null)
Expand All @@ -52,30 +53,33 @@ internal override bool Evaluate()
foreach (var referencedAssembly in referencedAssemblies)
{
var ruleFileInfo = ruleFileInfoList.FirstOrDefault(file => file.FileName == referencedAssembly.Name);
if (ruleFileInfo != null)
if (ruleFileInfo is null)
{
var autoInstrumentationFileVersion = new Version(ruleFileInfo.FileVersion);
// If the assembly is not directly referenced by the entry assembly, assume that it is okay to proceed.
continue;
}

var autoInstrumentationFileVersion = new Version(ruleFileInfo.FileVersion);

var appInstrumentationAssembly = Assembly.Load(referencedAssembly);
var appInstrumentationFileVersionInfo = FileVersionInfo.GetVersionInfo(appInstrumentationAssembly.Location);
var appInstrumentationFileVersion = new Version(appInstrumentationFileVersionInfo.FileVersion);
var appInstrumentationAssembly = Assembly.Load(referencedAssembly);
var appInstrumentationFileVersionInfo = FileVersionInfo.GetVersionInfo(appInstrumentationAssembly.Location);
var appInstrumentationFileVersion = new Version(appInstrumentationFileVersionInfo.FileVersion);

if (appInstrumentationFileVersion < autoInstrumentationFileVersion)
{
result = false;
Logger.Error($"Rule Engine: Application has direct or indirect reference to older version of Instrumentation library {ruleFileInfo.FileName} - {ruleFileInfo.FileVersion}.");
}
else
{
Logger.Information($"Rule Engine: Application has reference to Instrumentation library {ruleFileInfo.FileName} and loaded successfully.");
}
if (appInstrumentationFileVersion < autoInstrumentationFileVersion)
{
result = false;
Logger.Error($"Rule Engine: Application has direct or indirect reference to older version of assembly {ruleFileInfo.FileName} - {ruleFileInfo.FileVersion}.");
}
else
{
Logger.Information($"Rule Engine: Application has reference to assembly {ruleFileInfo.FileName} and loaded successfully.");
}
}
}
catch (Exception ex)
{
// Exception in rule evaluation should not impact the result of the rule.
Logger.Warning($"Rule Engine:Couldn't evaluate OpenTelemetry Instrumentation Evaluation. Exception: {ex}");
Logger.Warning($"Rule Engine: Couldn't evaluate assembly reference file version. Exception: {ex}");
}

return result;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ internal class RuleEngine
private readonly List<Rule> _otherRules = new()
{
new OpenTelemetrySdkMinimumVersionRule(),
new DiagnosticSourceRule(),
new InstrumentationAssemblyRule(),
new AssemblyFileVersionRule(),
new NativeProfilerDiagnosticsRule()
};

Expand Down

This file was deleted.

0 comments on commit b33e0e1

Please sign in to comment.