Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 11 additions & 4 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,28 @@ on:

jobs:
build:

runs-on: ubuntu-latest
runs-on: windows-latest

steps:
- uses: actions/checkout@v3

- name: Setup .NET
- name: Setup .NET SDKs
uses: actions/setup-dotnet@v2
with:
dotnet-version: |
5.0.x
6.0.x
7.0.x
8.0.x
9.0.x

- name: Setup MSBuild (for .NET Framework)
uses: microsoft/setup-msbuild@v1.1

- name: Restore dependencies
run: dotnet restore
run: |
dotnet restore
nuget restore ./src/Tests/AutoInstrumentation.ConsoleAppNetFramework/AutoInstrumentation.ConsoleAppNetFramework.csproj -SolutionDirectory .

- name: Build
run: dotnet build --no-restore
Expand Down
13 changes: 10 additions & 3 deletions .github/workflows/packman.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
jobs:
publish:
name: publish nuget
runs-on: ubuntu-latest
runs-on: windows-latest
steps:
- name: Checkout
uses: actions/checkout@v3
Expand All @@ -17,10 +17,17 @@ jobs:
with:
# Optionally strip `v` prefix
strip_v: true
- name: Setup .NET 9.0
- name: Setup .NET SDKs
uses: actions/setup-dotnet@v2
with:
dotnet-version: 9.0.x
dotnet-version: |
5.0.x
6.0.x
7.0.x
8.0.x
9.0.x
- name: Setup MSBuild (for .NET Framework)
uses: microsoft/setup-msbuild@v1.1
- uses: ./.github/workflows/composite/pack
with:
project: OpenTelemetry.Instrumentation.Digma
Expand Down
20 changes: 20 additions & 0 deletions .run/AutoInstrumentation.WindowsServiceSampleApp (net8).run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="AutoInstrumentation.WebAppDotNet (net8)" type="DotNetProject" factoryName=".NET Project">
<option name="EXE_PATH" value="$PROJECT_DIR$/src/Tests/AutoInstrumentation.WebAppDotNet/bin/Debug/net8.0/AutoInstrumentation.WebAppDotNet.exe" />
<option name="PROGRAM_PARAMETERS" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/src/Tests/AutoInstrumentation.WebAppDotNet/bin/Debug/net8.0" />
<option name="PASS_PARENT_ENVS" value="1" />
<option name="USE_EXTERNAL_CONSOLE" value="0" />
<option name="USE_MONO" value="0" />
<option name="RUNTIME_ARGUMENTS" value="" />
<option name="PROJECT_PATH" value="$PROJECT_DIR$/src/Tests/AutoInstrumentation.WebAppDotNet/AutoInstrumentation.WebAppDotNet.csproj" />
<option name="PROJECT_EXE_PATH_TRACKING" value="1" />
<option name="PROJECT_ARGUMENTS_TRACKING" value="1" />
<option name="PROJECT_WORKING_DIRECTORY_TRACKING" value="1" />
<option name="PROJECT_KIND" value="DotNetCore" />
<option name="PROJECT_TFM" value="net8.0" />
<method v="2">
<option name="Build" />
</method>
</configuration>
</component>
20 changes: 20 additions & 0 deletions .run/AutoInstrumentation.WindowsServiceSampleApp (net9).run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="AutoInstrumentation.WebAppDotNet (net9)" type="DotNetProject" factoryName=".NET Project">
<option name="EXE_PATH" value="$PROJECT_DIR$/src/Tests/AutoInstrumentation.WebAppDotNet/bin/Debug/net9.0/AutoInstrumentation.WebAppDotNet.exe" />
<option name="PROGRAM_PARAMETERS" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/src/Tests/AutoInstrumentation.WebAppDotNet/bin/Debug/net9.0" />
<option name="PASS_PARENT_ENVS" value="1" />
<option name="USE_EXTERNAL_CONSOLE" value="0" />
<option name="USE_MONO" value="0" />
<option name="RUNTIME_ARGUMENTS" value="" />
<option name="PROJECT_PATH" value="$PROJECT_DIR$/src/Tests/AutoInstrumentation.WebAppDotNet/AutoInstrumentation.WebAppDotNet.csproj" />
<option name="PROJECT_EXE_PATH_TRACKING" value="1" />
<option name="PROJECT_ARGUMENTS_TRACKING" value="1" />
<option name="PROJECT_WORKING_DIRECTORY_TRACKING" value="1" />
<option name="PROJECT_KIND" value="DotNetCore" />
<option name="PROJECT_TFM" value="net9.0" />
<method v="2">
<option name="Build" />
</method>
</configuration>
</component>
16 changes: 15 additions & 1 deletion OpenTelemetry.Instrumentation.Digma.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoInstrumentation.UnitTes
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoInstrumentation.IntegrationTests", "src\Tests\AutoInstrumentation.IntegrationTests\AutoInstrumentation.IntegrationTests.csproj", "{E4E70C20-C27F-4B8A-AD67-AD6070D3E217}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoInstrumentation.WindowsServiceSampleApp", "src\Tests\AutoInstrumentation.WindowsServiceSampleApp\AutoInstrumentation.WindowsServiceSampleApp.csproj", "{A270DB1B-DACB-4C22-B8C7-79A247D574F4}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoInstrumentation.WebAppDotNet", "src\Tests\AutoInstrumentation.WebAppDotNet\AutoInstrumentation.WebAppDotNet.csproj", "{A270DB1B-DACB-4C22-B8C7-79A247D574F4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoInstrumentation.ZeroCodeTests", "src\Tests\AutoInstrumentation.ZeroCodeTests\AutoInstrumentation.ZeroCodeTests.csproj", "{C05B4989-5E3A-43B9-8F6B-A7C3C13760E4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoInstrumentation.ConsoleAppNetFramework", "src\Tests\AutoInstrumentation.ConsoleAppNetFramework\AutoInstrumentation.ConsoleAppNetFramework.csproj", "{84B7BFC2-45D7-4D11-96F0-68FAC4062883}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoInstrumentation.ConsoleAppDotNet", "src\Tests\AutoInstrumentation.ConsoleAppDotNet\AutoInstrumentation.ConsoleAppDotNet.csproj", "{D112681B-6B71-4556-A138-6F68BC0A1AB1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -59,6 +63,14 @@ Global
{C05B4989-5E3A-43B9-8F6B-A7C3C13760E4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C05B4989-5E3A-43B9-8F6B-A7C3C13760E4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C05B4989-5E3A-43B9-8F6B-A7C3C13760E4}.Release|Any CPU.Build.0 = Release|Any CPU
{84B7BFC2-45D7-4D11-96F0-68FAC4062883}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{84B7BFC2-45D7-4D11-96F0-68FAC4062883}.Debug|Any CPU.Build.0 = Debug|Any CPU
{84B7BFC2-45D7-4D11-96F0-68FAC4062883}.Release|Any CPU.ActiveCfg = Release|Any CPU
{84B7BFC2-45D7-4D11-96F0-68FAC4062883}.Release|Any CPU.Build.0 = Release|Any CPU
{D112681B-6B71-4556-A138-6F68BC0A1AB1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D112681B-6B71-4556-A138-6F68BC0A1AB1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D112681B-6B71-4556-A138-6F68BC0A1AB1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D112681B-6B71-4556-A138-6F68BC0A1AB1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -71,5 +83,7 @@ Global
{E4E70C20-C27F-4B8A-AD67-AD6070D3E217} = {27F80A25-54CF-454F-857B-A0A5782D5A1C}
{A270DB1B-DACB-4C22-B8C7-79A247D574F4} = {27F80A25-54CF-454F-857B-A0A5782D5A1C}
{C05B4989-5E3A-43B9-8F6B-A7C3C13760E4} = {27F80A25-54CF-454F-857B-A0A5782D5A1C}
{84B7BFC2-45D7-4D11-96F0-68FAC4062883} = {27F80A25-54CF-454F-857B-A0A5782D5A1C}
{D112681B-6B71-4556-A138-6F68BC0A1AB1} = {27F80A25-54CF-454F-857B-A0A5782D5A1C}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class AutoInstrumentor : IDisposable
private readonly SqlClientInstrumentation _sqlClientInstrumentation;
private readonly VerticaInstrumentation _verticaInstrumentation;
private readonly UserCodeInstrumentation _userCodeInstrumentation;
private readonly AspNetInstrumentation _aspNetInstrumentation;
private readonly HashSet<Assembly> _scannedAssemblies = new();

public AutoInstrumentor(Configuration configuration = null)
Expand All @@ -22,13 +23,16 @@ public AutoInstrumentor(Configuration configuration = null)
_sqlClientInstrumentation = new SqlClientInstrumentation(_harmony);
_verticaInstrumentation = new VerticaInstrumentation(_harmony);
_userCodeInstrumentation = new UserCodeInstrumentation(_harmony, configuration);
_aspNetInstrumentation = new AspNetInstrumentation();
}

public AutoInstrumentor Instrument()
{
Logger.LogInfo("Sync Initialization started");
Logger.LogInfo("Env vars:\n"+string.Join("\n", EnvVars.GetAll().Select(x => $"{x.Key}={x.Value}")));

_aspNetInstrumentation.Instrument();

AppDomain.CurrentDomain.AssemblyLoad += (sender, args) =>
{
Logger.LogDebug($"Processing lazy-loaded {args.LoadedAssembly.FullName}");
Expand Down Expand Up @@ -71,5 +75,6 @@ private void ProcessAssembly(Assembly assembly)
public void Dispose()
{
_harmony.UnpatchAll(_harmony.Id);
_aspNetInstrumentation.Dispose();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc.Controllers;
using OpenTelemetry.AutoInstrumentation.Digma.Utils;

namespace OpenTelemetry.AutoInstrumentation.Digma.Instrumentation;

public class AspNetInstrumentation : IDisposable
{
private readonly DiagnosticSubscriber _observer = new();

public void Instrument()
{
try
{
_observer.Subscribe();
}
catch (Exception e)
{
Logger.LogError("Failed to subscribe DiagnosticSubscriber", e);
}
}

public void Dispose()
{
_observer.Dispose();
}

private class DiagnosticSubscriber : IObserver<DiagnosticListener>, IDisposable
{
private readonly HttpEndpointDiagnosticObserver _diagnosticObserver = new();
private readonly List<IDisposable> _subscriptions = new();

public void Subscribe()
{
var subscription = DiagnosticListener.AllListeners.Subscribe(this);
_subscriptions.Add(subscription);
}

public void OnCompleted()
{
}

public void OnError(Exception error)
{
}

public void OnNext(DiagnosticListener value)
{
if(value.Name != "Microsoft.AspNetCore")
return;

try
{
var subscription = value.Subscribe(_diagnosticObserver);
_subscriptions.Add(subscription);
}
catch (Exception e)
{
Logger.LogError("Failed to subscribe HttpEndpointDiagnosticObserver", e);
}
}

public void Dispose()
{
foreach (var subscription in _subscriptions)
{
subscription.Dispose();
}

_subscriptions.Clear();
}
}

private class HttpEndpointDiagnosticObserver : IObserver<KeyValuePair<string, object?>>

Check warning on line 78 in src/OpenTelemetry.AutoInstrumentation.Digma/Instrumentation/AspNetInstrumentation.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 78 in src/OpenTelemetry.AutoInstrumentation.Digma/Instrumentation/AspNetInstrumentation.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 78 in src/OpenTelemetry.AutoInstrumentation.Digma/Instrumentation/AspNetInstrumentation.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
public void OnCompleted()
{
}

public void OnError(Exception error)
{
}

public void OnNext(KeyValuePair<string, object?> pair)

Check warning on line 88 in src/OpenTelemetry.AutoInstrumentation.Digma/Instrumentation/AspNetInstrumentation.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 88 in src/OpenTelemetry.AutoInstrumentation.Digma/Instrumentation/AspNetInstrumentation.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 88 in src/OpenTelemetry.AutoInstrumentation.Digma/Instrumentation/AspNetInstrumentation.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
if (pair.Key != "Microsoft.AspNetCore.Routing.EndpointMatched")
return;
try
{
var httpContext = (HttpContext) pair.Value;
if (httpContext == null)
return;

var endpointFeature = httpContext.Features?.Get<IEndpointFeature>();
var descriptor = endpointFeature?.Endpoint.Metadata.GetMetadata<ControllerActionDescriptor>();
if (descriptor == null)
return;

var classFullName = descriptor.ControllerTypeInfo.FullName;
var methodName = descriptor.MethodInfo.Name;
var methodParams = DigmaSemanticConventions.BuildMethodParameterTypes(descriptor.MethodInfo);

Activity.Current?.SetTag(DigmaSemanticConventions.CodeNamespace, classFullName);
Activity.Current?.SetTag(DigmaSemanticConventions.CodeFunction, methodName);
Activity.Current?.SetTag(DigmaSemanticConventions.CodeFunctionParameterTypes, methodParams);
}
catch (Exception e)
{
Logger.LogError("Failed to enrich endpoint activity", e);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public UserCodeInstrumentation(Harmony harmony, Configuration configuration = nu
_harmony = harmony;
_configuration = configuration ?? ConfigurationProvider.GetConfiguration();

Logger.LogInfo("Configuration:\n" + _configuration.ToJson());
Logger.LogInfo("Configuration:\n" + ConfigurationProvider.ToJson(_configuration));
}

public void Instrument(Assembly assembly)
Expand Down Expand Up @@ -110,11 +110,11 @@ private static void Finalizer(MethodBase __originalMethod, Activity __state, Exc
if (__exception != null)
{
activity.RecordException(__exception);
activity.SetStatus(ActivityStatusCode.Error);
activity.SetErrorStatus();
}
else
{
activity.SetStatus(ActivityStatusCode.Ok);
activity.SetOkStatus();
}
activity.Dispose();
Logger.LogDebug($"Closed Activity: {activity.Source.Name}.{activity.OperationName}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,13 @@ private static void Finalizer(MethodBase __originalMethod, Activity __state, Exc
if (__exception != null)
{
activity.RecordException(__exception);
activity.SetStatus(ActivityStatusCode.Error);
}
activity.SetErrorStatus();
}
else
{
activity.SetOkStatus();
}

activity.Dispose();
Logger.LogDebug($"Closed Activity: {activity.Source.Name}.{activity.OperationName}");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<IsPackable>true</IsPackable>
<TargetFrameworks>net47;net5.0;net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
<LangVersion>10</LangVersion>
<LangVersion>latest</LangVersion>
<PackageId>OpenTel.AutoInstrumentation.Digma</PackageId>
<RootNamespace>OpenTelemetry.AutoInstrumentation.Digma</RootNamespace>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
Expand All @@ -19,10 +19,17 @@

<ItemGroup>
<PackageReference Include="Lib.Harmony" Version="2.3.5" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="9.0.0" PrivateAssets="all" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net47'">
<PackageReference Include="System.Text.Json" Version="9.0.0" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="9.0.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="[2.2.0,)" PrivateAssets="all"/>
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="[2.2.0,)" PrivateAssets="all"/>
<PackageReference Include="Microsoft.AspNetCore.Routing.Abstractions" Version="[2.2.0,)" PrivateAssets="all"/>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' != 'net47'">
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ public static void SetCodeTags(this Activity activity, MethodBase method)
activity?.SetTag(DigmaSemanticConventions.CodeNamespace, method.DeclaringType?.FullName);
activity?.SetTag(DigmaSemanticConventions.CodeFunction, method.Name);
}

public static void SetErrorStatus(this Activity activity)
{
#if NET6_0_OR_GREATER || NETFRAMEWORK
activity.SetStatus(ActivityStatusCode.Error);
#endif
}

public static void SetOkStatus(this Activity activity)
{
#if NET6_0_OR_GREATER || NETFRAMEWORK
activity.SetStatus(ActivityStatusCode.Ok);
#endif
}

// Taken from OpenTelemetry.Api
public static void RecordException(this Activity activity, Exception ex)
Expand Down
Loading
Loading