Skip to content

Commit

Permalink
Add binding redirection sample app (#198)
Browse files Browse the repository at this point in the history
  • Loading branch information
dszmigielski authored Jun 25, 2021
1 parent d4f91dc commit f5c13fb
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 21 deletions.
33 changes: 31 additions & 2 deletions Datadog.Trace.sln
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTracingLibrary", "sampl
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTelemetry.AutoInstrumentation.ClrProfiler.Managed.Core", "src\OpenTelemetry.AutoInstrumentation.ClrProfiler.Managed.Core\OpenTelemetry.AutoInstrumentation.ClrProfiler.Managed.Core.csproj", "{8BA1CCCC-B07C-46DD-9D54-0B98B3001100}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BindingRedirect", "samples\BindingRedirect\BindingRedirect.csproj", "{94089CFF-FD2A-45EF-A496-874569882E81}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OldReference", "samples\OldReference\OldReference.csproj", "{4C10E5C1-E212-4744-B92E-49A3AFC314FC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -183,6 +187,30 @@ Global
{8BA1CCCC-B07C-46DD-9D54-0B98B3001100}.Release|x64.Build.0 = Release|Any CPU
{8BA1CCCC-B07C-46DD-9D54-0B98B3001100}.Release|x86.ActiveCfg = Release|Any CPU
{8BA1CCCC-B07C-46DD-9D54-0B98B3001100}.Release|x86.Build.0 = Release|Any CPU
{94089CFF-FD2A-45EF-A496-874569882E81}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{94089CFF-FD2A-45EF-A496-874569882E81}.Debug|Any CPU.Build.0 = Debug|Any CPU
{94089CFF-FD2A-45EF-A496-874569882E81}.Debug|x64.ActiveCfg = Debug|Any CPU
{94089CFF-FD2A-45EF-A496-874569882E81}.Debug|x64.Build.0 = Debug|Any CPU
{94089CFF-FD2A-45EF-A496-874569882E81}.Debug|x86.ActiveCfg = Debug|Any CPU
{94089CFF-FD2A-45EF-A496-874569882E81}.Debug|x86.Build.0 = Debug|Any CPU
{94089CFF-FD2A-45EF-A496-874569882E81}.Release|Any CPU.ActiveCfg = Release|Any CPU
{94089CFF-FD2A-45EF-A496-874569882E81}.Release|Any CPU.Build.0 = Release|Any CPU
{94089CFF-FD2A-45EF-A496-874569882E81}.Release|x64.ActiveCfg = Release|Any CPU
{94089CFF-FD2A-45EF-A496-874569882E81}.Release|x64.Build.0 = Release|Any CPU
{94089CFF-FD2A-45EF-A496-874569882E81}.Release|x86.ActiveCfg = Release|Any CPU
{94089CFF-FD2A-45EF-A496-874569882E81}.Release|x86.Build.0 = Release|Any CPU
{4C10E5C1-E212-4744-B92E-49A3AFC314FC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4C10E5C1-E212-4744-B92E-49A3AFC314FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4C10E5C1-E212-4744-B92E-49A3AFC314FC}.Debug|x64.ActiveCfg = Debug|Any CPU
{4C10E5C1-E212-4744-B92E-49A3AFC314FC}.Debug|x64.Build.0 = Debug|Any CPU
{4C10E5C1-E212-4744-B92E-49A3AFC314FC}.Debug|x86.ActiveCfg = Debug|Any CPU
{4C10E5C1-E212-4744-B92E-49A3AFC314FC}.Debug|x86.Build.0 = Debug|Any CPU
{4C10E5C1-E212-4744-B92E-49A3AFC314FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4C10E5C1-E212-4744-B92E-49A3AFC314FC}.Release|Any CPU.Build.0 = Release|Any CPU
{4C10E5C1-E212-4744-B92E-49A3AFC314FC}.Release|x64.ActiveCfg = Release|Any CPU
{4C10E5C1-E212-4744-B92E-49A3AFC314FC}.Release|x64.Build.0 = Release|Any CPU
{4C10E5C1-E212-4744-B92E-49A3AFC314FC}.Release|x86.ActiveCfg = Release|Any CPU
{4C10E5C1-E212-4744-B92E-49A3AFC314FC}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -195,10 +223,11 @@ Global
{D141BD06-DD95-4CAF-85CD-657116E0DAD4} = {BAF8F246-3645-42AD-B1D0-0F7EAFBAB34A}
{2A172931-3439-4563-A6E5-525924DC2F76} = {BAF8F246-3645-42AD-B1D0-0F7EAFBAB34A}
{6690361C-CCD8-42C4-A5CF-82D554B03A3D} = {9E5F0022-0A50-40BF-AC6A-C3078585ECAB}
{8BA1CCCC-B07C-46DD-9D54-0B98B3001100} = {9E5F0022-0A50-40BF-AC6A-C3078585ECAB}
{7EA51D34-8E0E-4548-8A7F-5C6F9C3533AF} = {BAF8F246-3645-42AD-B1D0-0F7EAFBAB34A}
{7220BDBB-084D-40B3-A710-19E3C7147185} = {BAF8F246-3645-42AD-B1D0-0F7EAFBAB34A}
{6AFC7B3D-DD3A-4EB1-97D9-D59F0D29C6B8} = {BAF8F246-3645-42AD-B1D0-0F7EAFBAB34A}
{8BA1CCCC-B07C-46DD-9D54-0B98B3001100} = {9E5F0022-0A50-40BF-AC6A-C3078585ECAB}
{94089CFF-FD2A-45EF-A496-874569882E81} = {BAF8F246-3645-42AD-B1D0-0F7EAFBAB34A}
{4C10E5C1-E212-4744-B92E-49A3AFC314FC} = {BAF8F246-3645-42AD-B1D0-0F7EAFBAB34A}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {160A1D00-1F5B-40F8-A155-621B4459D78F}
Expand Down
18 changes: 0 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,6 @@ It would be good to have a checkpoint here where the instrumented application is
- https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/src/OpenTelemetry/README.md#activity-source
- https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/examples/Console/TestHttpClient.cs

## Findings

### Required reference to System.Diagnostics.DiagnosticSource

The instrumented application has to include same version of the `System.Diagnostics.DiagnosticSource` as the SDK. Current version is `5.0.1`.

Include package reference directly into `.csproj`:

```xml
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="5.0.1" />
```

... or use Package Manager Console to install package:

```powershell
Install-Package System.Diagnostics.DiagnosticSource -Version 5.0.1 -ProjectName MyProjectName
```

## Testing

### Additional setup for Windows
Expand Down
33 changes: 32 additions & 1 deletion docs/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,20 @@ Enable instrumentation for a specific user:

## Configure custom instrumentation

The instrumented application has to include same version of the `System.Diagnostics.DiagnosticSource` as the SDK. Current version is `5.0.1`.

Include package reference directly into `.csproj`:

```xml
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="5.0.1" />
```

... or use Package Manager Console to install package:

```powershell
Install-Package System.Diagnostics.DiagnosticSource -Version 5.0.1 -ProjectName MyProjectName
```

For adding manual instrumentation the variable `OTEL_DOTNET_TRACER_LOAD_AT_STARTUP` should be set to `false` and the tracer should be initialized by the application itself. Here is an example of creating the tracer:

```csharp
Expand Down Expand Up @@ -300,6 +314,23 @@ Use the environment variables `OTEL_PROFILER_EXCLUDE_PROCESSES` and `OTEL_PROFIL
to include/exclude applications from the tracing auto-instrumentation.
These are ";" delimited lists that control the inclusion/exclusion of processes.
### No proper relatioship between spans
On .NET Framework strong name signing can force multiple versions of the same assembly being loaded on the same process. This causes a separate hierarchy of Activity objects. If you are referencing packages in your application that use different version of the `System.Diagnostics.DiagnosticSource` than the `OpenTelemetry.Api` used by autoinstrumentation (`5.0.1`) you have to explicitly reference the `System.Diagnostics.DiagnosticSource` package in the correct version in your application (see [custom instrumentation section](#configure-custom-instrumentation)). This will cause automatic binding redirection to occur resolving the issue. If automatic binding redirection is [disabled](https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/how-to-enable-and-disable-automatic-binding-redirection) you can also manually add binding redirection to the `App.config` file:
```xml
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="cc7b13ffcd" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.1" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
```
### Investigating other issues
If none of the suggestions above solves your issue, detailed logs are necessary.
Expand All @@ -311,4 +342,4 @@ If needed, change the default location by updating the environment variable `OTE
On Linux, the default log location is `/var/log/opentelemetry/dotnet/`
On Windows, the default log location is `%ProgramData%\\OpenTelemetry .NET AutoInstrumentation\logs\`
Compress the whole folder to capture the multiple log files and send the compressed folder to us.
After obtaining the logs, remember to remove the environment variable `OTEL_TRACE_DEBUG` to avoid unnecessary overhead.
After obtaining the logs, remember to remove the environment variable `OTEL_TRACE_DEBUG` to avoid unnecessary overhead.
14 changes: 14 additions & 0 deletions samples/BindingRedirect/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
22 changes: 22 additions & 0 deletions samples/BindingRedirect/BindingRedirect.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net472</TargetFrameworks>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNet.TelemetryCorrelation" Version="1.0.8" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="5.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\OldReference\OldReference.csproj" />
</ItemGroup>

<ItemGroup>
<Reference Include="System.Net.Http" />
</ItemGroup>

</Project>
13 changes: 13 additions & 0 deletions samples/BindingRedirect/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Threading.Tasks;
using OldReference;

namespace BindingRedirect
{
public class Program
{
public static async Task Main(string[] args)
{
await InstrumentedHttpCall.GetAsync("https://www.google.com");
}
}
}
34 changes: 34 additions & 0 deletions samples/OldReference/InstrumentedHttpCall.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;

namespace OldReference
{
public static class InstrumentedHttpCall
{
private static readonly ActivitySource MyActivitySource = new("OpenTelemetry.AutoInstrumentation.BindingRedirect");

public static async Task GetAsync(string url)
{
Console.WriteLine(">>>>>>>>>>>>>>>>>>>>>>> System.Diagnostics.DiagnosticSource assemblies loaded:");
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
var loaded = assemblies
.Where(assembly => assembly.FullName.Contains("System.Diagnostics.DiagnosticSource"))
.Select(assembly => $">>>>>>>>>>>>>>>>>>>>>>> {assembly.FullName}");

Console.WriteLine(string.Join("\n", loaded));

using (var activity = MyActivitySource.StartActivity("RunAsync"))
{
activity?.SetTag("foo", "bar");

using var client = new HttpClient();
Console.WriteLine($"Calling {url}");
await client.GetAsync(url);
Console.WriteLine($"Called {url}");
}
}
}
}
16 changes: 16 additions & 0 deletions samples/OldReference/OldReference.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
<Reference Include="System.Net.Http"/>
</ItemGroup>

</Project>

0 comments on commit f5c13fb

Please sign in to comment.