//
// 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.
//
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text.Encodings.Web;
using System.Text.Json;
const string instrumentMethodAttributeName = "OpenTelemetry.AutoInstrumentation.Instrumentations.InstrumentMethodAttribute";
var thisFilePath = GetSourceFilePathName();
var solutionFolder = Path.Combine(thisFilePath, "..", "..", "..");
var autoInstrumentationLibPath = Path.Combine(solutionFolder, "bin", "tracer-home", "netcoreapp3.1", "OpenTelemetry.AutoInstrumentation.dll");
var autoInstrumentationLib = Assembly.LoadFrom(autoInstrumentationLibPath);
var assemblyInstrumentMethodAttributes = autoInstrumentationLib.DefinedTypes
.Where(type => InheritsFrom(type, instrumentMethodAttributeName)).Select(x => x.FullName);
var integrations = new Dictionary();
foreach (var typeInfo in autoInstrumentationLib.GetTypes())
{
foreach (var attribute in typeInfo.GetCustomAttributes()
.Where(a => assemblyInstrumentMethodAttributes.Contains(a.GetType().FullName)))
{
var integration = ConvertToIntegration(typeInfo.FullName, attribute);
if (!integrations.ContainsKey(integration.IntegrationName))
{
integrations.Add(
integration.IntegrationName,
new Integration
{
Name = integration.IntegrationName,
Type = integration.IntegartionType,
MethodReplacements = new List { integration.MethodReplacement }
});
}
else
{
var integration2 = integrations[integration.IntegrationName];
integration2.MethodReplacements.Add(integration.MethodReplacement);
}
}
}
var productionIntegrations = integrations.Where(x => x.Key != "StrongNamedValidation").Select(x => x.Value)
.OrderBy(x => x.Name).ToArray();
var testIntegrations = integrations.Where(x => x.Key == "StrongNamedValidation").Select(x => x.Value)
.OrderBy(x => x.Name).ToArray();
UpdateIntegrationFile(Path.Combine(solutionFolder, "integrations.json"), productionIntegrations);
UpdateIntegrationFile(Path.Combine(solutionFolder, "test", "IntegrationTests", "StrongNamedTestsIntegrations.json"), testIntegrations);
bool InheritsFrom(Type type, string baseType)
{
while (true)
{
if (type.FullName == baseType)
{
return true;
}
if (type.BaseType is null)
{
return false;
}
type = type.BaseType;
}
}
(string IntegartionType, string IntegrationName, MethodReplacement MethodReplacement) ConvertToIntegration(string wrapperTypeName, Attribute attribute)
{
var integrationName = GetPropertyValue("IntegrationName", attribute);
var integrationType = GetPropertyValue