Skip to content

Commit ee49a00

Browse files
authored
Merge pull request #52 from digma-ai/endpoints-method-and-params
2 parents ece517b + 4b357f8 commit ee49a00

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1737
-55
lines changed

.github/workflows/dotnet.yml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,28 @@ on:
88

99
jobs:
1010
build:
11-
12-
runs-on: ubuntu-latest
11+
runs-on: windows-latest
1312

1413
steps:
1514
- uses: actions/checkout@v3
1615

17-
- name: Setup .NET
16+
- name: Setup .NET SDKs
1817
uses: actions/setup-dotnet@v2
1918
with:
2019
dotnet-version: |
20+
5.0.x
2121
6.0.x
22+
7.0.x
23+
8.0.x
2224
9.0.x
25+
26+
- name: Setup MSBuild (for .NET Framework)
27+
uses: microsoft/[email protected]
2328

2429
- name: Restore dependencies
25-
run: dotnet restore
30+
run: |
31+
dotnet restore
32+
nuget restore ./src/Tests/AutoInstrumentation.ConsoleAppNetFramework/AutoInstrumentation.ConsoleAppNetFramework.csproj -SolutionDirectory .
2633
2734
- name: Build
2835
run: dotnet build --no-restore

.github/workflows/packman.yml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77
jobs:
88
publish:
99
name: publish nuget
10-
runs-on: ubuntu-latest
10+
runs-on: windows-latest
1111
steps:
1212
- name: Checkout
1313
uses: actions/checkout@v3
@@ -17,10 +17,17 @@ jobs:
1717
with:
1818
# Optionally strip `v` prefix
1919
strip_v: true
20-
- name: Setup .NET 9.0
20+
- name: Setup .NET SDKs
2121
uses: actions/setup-dotnet@v2
2222
with:
23-
dotnet-version: 9.0.x
23+
dotnet-version: |
24+
5.0.x
25+
6.0.x
26+
7.0.x
27+
8.0.x
28+
9.0.x
29+
- name: Setup MSBuild (for .NET Framework)
30+
uses: microsoft/[email protected]
2431
- uses: ./.github/workflows/composite/pack
2532
with:
2633
project: OpenTelemetry.Instrumentation.Digma
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<component name="ProjectRunConfigurationManager">
2+
<configuration default="false" name="AutoInstrumentation.WebAppDotNet (net8)" type="DotNetProject" factoryName=".NET Project">
3+
<option name="EXE_PATH" value="$PROJECT_DIR$/src/Tests/AutoInstrumentation.WebAppDotNet/bin/Debug/net8.0/AutoInstrumentation.WebAppDotNet.exe" />
4+
<option name="PROGRAM_PARAMETERS" value="" />
5+
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/src/Tests/AutoInstrumentation.WebAppDotNet/bin/Debug/net8.0" />
6+
<option name="PASS_PARENT_ENVS" value="1" />
7+
<option name="USE_EXTERNAL_CONSOLE" value="0" />
8+
<option name="USE_MONO" value="0" />
9+
<option name="RUNTIME_ARGUMENTS" value="" />
10+
<option name="PROJECT_PATH" value="$PROJECT_DIR$/src/Tests/AutoInstrumentation.WebAppDotNet/AutoInstrumentation.WebAppDotNet.csproj" />
11+
<option name="PROJECT_EXE_PATH_TRACKING" value="1" />
12+
<option name="PROJECT_ARGUMENTS_TRACKING" value="1" />
13+
<option name="PROJECT_WORKING_DIRECTORY_TRACKING" value="1" />
14+
<option name="PROJECT_KIND" value="DotNetCore" />
15+
<option name="PROJECT_TFM" value="net8.0" />
16+
<method v="2">
17+
<option name="Build" />
18+
</method>
19+
</configuration>
20+
</component>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<component name="ProjectRunConfigurationManager">
2+
<configuration default="false" name="AutoInstrumentation.WebAppDotNet (net9)" type="DotNetProject" factoryName=".NET Project">
3+
<option name="EXE_PATH" value="$PROJECT_DIR$/src/Tests/AutoInstrumentation.WebAppDotNet/bin/Debug/net9.0/AutoInstrumentation.WebAppDotNet.exe" />
4+
<option name="PROGRAM_PARAMETERS" value="" />
5+
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/src/Tests/AutoInstrumentation.WebAppDotNet/bin/Debug/net9.0" />
6+
<option name="PASS_PARENT_ENVS" value="1" />
7+
<option name="USE_EXTERNAL_CONSOLE" value="0" />
8+
<option name="USE_MONO" value="0" />
9+
<option name="RUNTIME_ARGUMENTS" value="" />
10+
<option name="PROJECT_PATH" value="$PROJECT_DIR$/src/Tests/AutoInstrumentation.WebAppDotNet/AutoInstrumentation.WebAppDotNet.csproj" />
11+
<option name="PROJECT_EXE_PATH_TRACKING" value="1" />
12+
<option name="PROJECT_ARGUMENTS_TRACKING" value="1" />
13+
<option name="PROJECT_WORKING_DIRECTORY_TRACKING" value="1" />
14+
<option name="PROJECT_KIND" value="DotNetCore" />
15+
<option name="PROJECT_TFM" value="net9.0" />
16+
<method v="2">
17+
<option name="Build" />
18+
</method>
19+
</configuration>
20+
</component>

OpenTelemetry.Instrumentation.Digma.sln

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoInstrumentation.UnitTes
1717
EndProject
1818
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoInstrumentation.IntegrationTests", "src\Tests\AutoInstrumentation.IntegrationTests\AutoInstrumentation.IntegrationTests.csproj", "{E4E70C20-C27F-4B8A-AD67-AD6070D3E217}"
1919
EndProject
20-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoInstrumentation.WindowsServiceSampleApp", "src\Tests\AutoInstrumentation.WindowsServiceSampleApp\AutoInstrumentation.WindowsServiceSampleApp.csproj", "{A270DB1B-DACB-4C22-B8C7-79A247D574F4}"
20+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoInstrumentation.WebAppDotNet", "src\Tests\AutoInstrumentation.WebAppDotNet\AutoInstrumentation.WebAppDotNet.csproj", "{A270DB1B-DACB-4C22-B8C7-79A247D574F4}"
2121
EndProject
2222
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoInstrumentation.ZeroCodeTests", "src\Tests\AutoInstrumentation.ZeroCodeTests\AutoInstrumentation.ZeroCodeTests.csproj", "{C05B4989-5E3A-43B9-8F6B-A7C3C13760E4}"
2323
EndProject
24+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoInstrumentation.ConsoleAppNetFramework", "src\Tests\AutoInstrumentation.ConsoleAppNetFramework\AutoInstrumentation.ConsoleAppNetFramework.csproj", "{84B7BFC2-45D7-4D11-96F0-68FAC4062883}"
25+
EndProject
26+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutoInstrumentation.ConsoleAppDotNet", "src\Tests\AutoInstrumentation.ConsoleAppDotNet\AutoInstrumentation.ConsoleAppDotNet.csproj", "{D112681B-6B71-4556-A138-6F68BC0A1AB1}"
27+
EndProject
2428
Global
2529
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2630
Debug|Any CPU = Debug|Any CPU
@@ -59,6 +63,14 @@ Global
5963
{C05B4989-5E3A-43B9-8F6B-A7C3C13760E4}.Debug|Any CPU.Build.0 = Debug|Any CPU
6064
{C05B4989-5E3A-43B9-8F6B-A7C3C13760E4}.Release|Any CPU.ActiveCfg = Release|Any CPU
6165
{C05B4989-5E3A-43B9-8F6B-A7C3C13760E4}.Release|Any CPU.Build.0 = Release|Any CPU
66+
{84B7BFC2-45D7-4D11-96F0-68FAC4062883}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
67+
{84B7BFC2-45D7-4D11-96F0-68FAC4062883}.Debug|Any CPU.Build.0 = Debug|Any CPU
68+
{84B7BFC2-45D7-4D11-96F0-68FAC4062883}.Release|Any CPU.ActiveCfg = Release|Any CPU
69+
{84B7BFC2-45D7-4D11-96F0-68FAC4062883}.Release|Any CPU.Build.0 = Release|Any CPU
70+
{D112681B-6B71-4556-A138-6F68BC0A1AB1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
71+
{D112681B-6B71-4556-A138-6F68BC0A1AB1}.Debug|Any CPU.Build.0 = Debug|Any CPU
72+
{D112681B-6B71-4556-A138-6F68BC0A1AB1}.Release|Any CPU.ActiveCfg = Release|Any CPU
73+
{D112681B-6B71-4556-A138-6F68BC0A1AB1}.Release|Any CPU.Build.0 = Release|Any CPU
6274
EndGlobalSection
6375
GlobalSection(SolutionProperties) = preSolution
6476
HideSolutionNode = FALSE
@@ -71,5 +83,7 @@ Global
7183
{E4E70C20-C27F-4B8A-AD67-AD6070D3E217} = {27F80A25-54CF-454F-857B-A0A5782D5A1C}
7284
{A270DB1B-DACB-4C22-B8C7-79A247D574F4} = {27F80A25-54CF-454F-857B-A0A5782D5A1C}
7385
{C05B4989-5E3A-43B9-8F6B-A7C3C13760E4} = {27F80A25-54CF-454F-857B-A0A5782D5A1C}
86+
{84B7BFC2-45D7-4D11-96F0-68FAC4062883} = {27F80A25-54CF-454F-857B-A0A5782D5A1C}
87+
{D112681B-6B71-4556-A138-6F68BC0A1AB1} = {27F80A25-54CF-454F-857B-A0A5782D5A1C}
7488
EndGlobalSection
7589
EndGlobal

src/OpenTelemetry.AutoInstrumentation.Digma/AutoInstrumentor.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class AutoInstrumentor : IDisposable
1414
private readonly SqlClientInstrumentation _sqlClientInstrumentation;
1515
private readonly VerticaInstrumentation _verticaInstrumentation;
1616
private readonly UserCodeInstrumentation _userCodeInstrumentation;
17+
private readonly AspNetInstrumentation _aspNetInstrumentation;
1718
private readonly HashSet<Assembly> _scannedAssemblies = new();
1819

1920
public AutoInstrumentor(Configuration configuration = null)
@@ -22,13 +23,16 @@ public AutoInstrumentor(Configuration configuration = null)
2223
_sqlClientInstrumentation = new SqlClientInstrumentation(_harmony);
2324
_verticaInstrumentation = new VerticaInstrumentation(_harmony);
2425
_userCodeInstrumentation = new UserCodeInstrumentation(_harmony, configuration);
26+
_aspNetInstrumentation = new AspNetInstrumentation();
2527
}
2628

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

34+
_aspNetInstrumentation.Instrument();
35+
3236
AppDomain.CurrentDomain.AssemblyLoad += (sender, args) =>
3337
{
3438
Logger.LogDebug($"Processing lazy-loaded {args.LoadedAssembly.FullName}");
@@ -71,5 +75,6 @@ private void ProcessAssembly(Assembly assembly)
7175
public void Dispose()
7276
{
7377
_harmony.UnpatchAll(_harmony.Id);
78+
_aspNetInstrumentation.Dispose();
7479
}
7580
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using Microsoft.AspNetCore.Http;
5+
using Microsoft.AspNetCore.Http.Features;
6+
using Microsoft.AspNetCore.Mvc.Controllers;
7+
using OpenTelemetry.AutoInstrumentation.Digma.Utils;
8+
9+
namespace OpenTelemetry.AutoInstrumentation.Digma.Instrumentation;
10+
11+
public class AspNetInstrumentation : IDisposable
12+
{
13+
private readonly DiagnosticSubscriber _observer = new();
14+
15+
public void Instrument()
16+
{
17+
try
18+
{
19+
_observer.Subscribe();
20+
}
21+
catch (Exception e)
22+
{
23+
Logger.LogError("Failed to subscribe DiagnosticSubscriber", e);
24+
}
25+
}
26+
27+
public void Dispose()
28+
{
29+
_observer.Dispose();
30+
}
31+
32+
private class DiagnosticSubscriber : IObserver<DiagnosticListener>, IDisposable
33+
{
34+
private readonly HttpEndpointDiagnosticObserver _diagnosticObserver = new();
35+
private readonly List<IDisposable> _subscriptions = new();
36+
37+
public void Subscribe()
38+
{
39+
var subscription = DiagnosticListener.AllListeners.Subscribe(this);
40+
_subscriptions.Add(subscription);
41+
}
42+
43+
public void OnCompleted()
44+
{
45+
}
46+
47+
public void OnError(Exception error)
48+
{
49+
}
50+
51+
public void OnNext(DiagnosticListener value)
52+
{
53+
if(value.Name != "Microsoft.AspNetCore")
54+
return;
55+
56+
try
57+
{
58+
var subscription = value.Subscribe(_diagnosticObserver);
59+
_subscriptions.Add(subscription);
60+
}
61+
catch (Exception e)
62+
{
63+
Logger.LogError("Failed to subscribe HttpEndpointDiagnosticObserver", e);
64+
}
65+
}
66+
67+
public void Dispose()
68+
{
69+
foreach (var subscription in _subscriptions)
70+
{
71+
subscription.Dispose();
72+
}
73+
74+
_subscriptions.Clear();
75+
}
76+
}
77+
78+
private class HttpEndpointDiagnosticObserver : IObserver<KeyValuePair<string, object?>>
79+
{
80+
public void OnCompleted()
81+
{
82+
}
83+
84+
public void OnError(Exception error)
85+
{
86+
}
87+
88+
public void OnNext(KeyValuePair<string, object?> pair)
89+
{
90+
if (pair.Key != "Microsoft.AspNetCore.Routing.EndpointMatched")
91+
return;
92+
try
93+
{
94+
var httpContext = (HttpContext) pair.Value;
95+
if (httpContext == null)
96+
return;
97+
98+
var endpointFeature = httpContext.Features?.Get<IEndpointFeature>();
99+
var descriptor = endpointFeature?.Endpoint.Metadata.GetMetadata<ControllerActionDescriptor>();
100+
if (descriptor == null)
101+
return;
102+
103+
var classFullName = descriptor.ControllerTypeInfo.FullName;
104+
var methodName = descriptor.MethodInfo.Name;
105+
var methodParams = DigmaSemanticConventions.BuildMethodParameterTypes(descriptor.MethodInfo);
106+
107+
Activity.Current?.SetTag(DigmaSemanticConventions.CodeNamespace, classFullName);
108+
Activity.Current?.SetTag(DigmaSemanticConventions.CodeFunction, methodName);
109+
Activity.Current?.SetTag(DigmaSemanticConventions.CodeFunctionParameterTypes, methodParams);
110+
}
111+
catch (Exception e)
112+
{
113+
Logger.LogError("Failed to enrich endpoint activity", e);
114+
}
115+
}
116+
}
117+
}

src/OpenTelemetry.AutoInstrumentation.Digma/Instrumentation/UserCodeInstrumentation.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public UserCodeInstrumentation(Harmony harmony, Configuration configuration = nu
2222
_harmony = harmony;
2323
_configuration = configuration ?? ConfigurationProvider.GetConfiguration();
2424

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

2828
public void Instrument(Assembly assembly)
@@ -110,11 +110,11 @@ private static void Finalizer(MethodBase __originalMethod, Activity __state, Exc
110110
if (__exception != null)
111111
{
112112
activity.RecordException(__exception);
113-
activity.SetStatus(ActivityStatusCode.Error);
113+
activity.SetErrorStatus();
114114
}
115115
else
116116
{
117-
activity.SetStatus(ActivityStatusCode.Ok);
117+
activity.SetOkStatus();
118118
}
119119
activity.Dispose();
120120
Logger.LogDebug($"Closed Activity: {activity.Source.Name}.{activity.OperationName}");

src/OpenTelemetry.AutoInstrumentation.Digma/Instrumentation/VerticaInstrumentation.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,13 @@ private static void Finalizer(MethodBase __originalMethod, Activity __state, Exc
111111
if (__exception != null)
112112
{
113113
activity.RecordException(__exception);
114-
activity.SetStatus(ActivityStatusCode.Error);
115-
}
114+
activity.SetErrorStatus();
115+
}
116+
else
117+
{
118+
activity.SetOkStatus();
119+
}
120+
116121
activity.Dispose();
117122
Logger.LogDebug($"Closed Activity: {activity.Source.Name}.{activity.OperationName}");
118123
}

src/OpenTelemetry.AutoInstrumentation.Digma/OpenTelemetry.AutoInstrumentation.Digma.csproj

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<IsPackable>true</IsPackable>
55
<TargetFrameworks>net47;net5.0;net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
6-
<LangVersion>10</LangVersion>
6+
<LangVersion>latest</LangVersion>
77
<PackageId>OpenTel.AutoInstrumentation.Digma</PackageId>
88
<RootNamespace>OpenTelemetry.AutoInstrumentation.Digma</RootNamespace>
99
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
@@ -19,10 +19,17 @@
1919

2020
<ItemGroup>
2121
<PackageReference Include="Lib.Harmony" Version="2.3.5" />
22-
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="9.0.0" PrivateAssets="all" />
2322
</ItemGroup>
2423

2524
<ItemGroup Condition="'$(TargetFramework)' == 'net47'">
2625
<PackageReference Include="System.Text.Json" Version="9.0.0" />
26+
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="9.0.0" PrivateAssets="all" />
27+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="[2.2.0,)" PrivateAssets="all"/>
28+
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="[2.2.0,)" PrivateAssets="all"/>
29+
<PackageReference Include="Microsoft.AspNetCore.Routing.Abstractions" Version="[2.2.0,)" PrivateAssets="all"/>
30+
</ItemGroup>
31+
32+
<ItemGroup Condition="'$(TargetFramework)' != 'net47'">
33+
<FrameworkReference Include="Microsoft.AspNetCore.App" />
2734
</ItemGroup>
2835
</Project>

0 commit comments

Comments
 (0)