Skip to content

Commit b89d939

Browse files
Make framework version detection resilient to DI scenarios (#42)
* add DI sample * inspect target framework attribute if appdomain doesn't have info * fix tests
1 parent 52e15d7 commit b89d939

File tree

6 files changed

+180
-1
lines changed

6 files changed

+180
-1
lines changed

LightStep.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "examples", "examples", "{11
2121
EndProject
2222
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LightStep.CSharpAspectTestApp", "examples\LightStep.CSharpAspectTestApp\LightStep.CSharpAspectTestApp.csproj", "{1B83F7CF-E2AA-4A81-9C0B-2DA716D5C982}"
2323
EndProject
24+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LightStep.CSharpDITestApp", "examples\LightStep.CSharpDITestApp\LightStep.CSharpDITestApp.csproj", "{358E688A-D19E-4149-85CD-E804738C0C3F}"
25+
EndProject
2426
Global
2527
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2628
Debug|Any CPU = Debug|Any CPU
@@ -43,6 +45,10 @@ Global
4345
{1B83F7CF-E2AA-4A81-9C0B-2DA716D5C982}.Debug|Any CPU.Build.0 = Debug|Any CPU
4446
{1B83F7CF-E2AA-4A81-9C0B-2DA716D5C982}.Release|Any CPU.ActiveCfg = Release|Any CPU
4547
{1B83F7CF-E2AA-4A81-9C0B-2DA716D5C982}.Release|Any CPU.Build.0 = Release|Any CPU
48+
{358E688A-D19E-4149-85CD-E804738C0C3F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
49+
{358E688A-D19E-4149-85CD-E804738C0C3F}.Debug|Any CPU.Build.0 = Debug|Any CPU
50+
{358E688A-D19E-4149-85CD-E804738C0C3F}.Release|Any CPU.ActiveCfg = Release|Any CPU
51+
{358E688A-D19E-4149-85CD-E804738C0C3F}.Release|Any CPU.Build.0 = Release|Any CPU
4652
EndGlobalSection
4753
GlobalSection(SolutionProperties) = preSolution
4854
HideSolutionNode = FALSE
@@ -52,6 +58,7 @@ Global
5258
{2DB2D4A0-64AB-4C5B-ACB5-0C2EB76D756C} = {1359ACF9-43F2-4E09-94FD-32EB2C6239BA}
5359
{65A11CA6-E7E9-43B6-A36D-6B54EA9BD758} = {113EDBB5-081F-4581-B687-9293B4D74312}
5460
{1B83F7CF-E2AA-4A81-9C0B-2DA716D5C982} = {113EDBB5-081F-4581-B687-9293B4D74312}
61+
{358E688A-D19E-4149-85CD-E804738C0C3F} = {113EDBB5-081F-4581-B687-9293B4D74312}
5562
EndGlobalSection
5663
GlobalSection(ExtensibilityGlobals) = postSolution
5764
SolutionGuid = {0890664C-9CE4-4960-90A4-AD6565C970E3}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{358E688A-D19E-4149-85CD-E804738C0C3F}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>LightStep.CSharpDITestApp</RootNamespace>
11+
<AssemblyName>LightStep.CSharpDITestApp</AssemblyName>
12+
<TargetFrameworkVersion>v4.7.1</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<PlatformTarget>AnyCPU</PlatformTarget>
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<PlatformTarget>AnyCPU</PlatformTarget>
27+
<DebugType>pdbonly</DebugType>
28+
<Optimize>true</Optimize>
29+
<OutputPath>bin\Release\</OutputPath>
30+
<DefineConstants>TRACE</DefineConstants>
31+
<ErrorReport>prompt</ErrorReport>
32+
<WarningLevel>4</WarningLevel>
33+
</PropertyGroup>
34+
<ItemGroup>
35+
<Reference Include="Castle.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc">
36+
<HintPath>..\..\packages\Castle.Core.4.2.0\lib\net45\Castle.Core.dll</HintPath>
37+
<Private>True</Private>
38+
</Reference>
39+
<Reference Include="Castle.Windsor, Version=4.0.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc">
40+
<HintPath>..\..\packages\Castle.Windsor.4.1.1\lib\net45\Castle.Windsor.dll</HintPath>
41+
<Private>True</Private>
42+
</Reference>
43+
<Reference Include="OpenTracing, Version=0.12.0.0, Culture=neutral, PublicKeyToken=61503406977abdaf">
44+
<HintPath>..\..\packages\OpenTracing.0.12.0\lib\net45\OpenTracing.dll</HintPath>
45+
<Private>True</Private>
46+
</Reference>
47+
<Reference Include="System" />
48+
<Reference Include="System.Configuration" />
49+
<Reference Include="System.Core" />
50+
<Reference Include="System.Data" />
51+
<Reference Include="System.Runtime.Remoting" />
52+
<Reference Include="System.Web" />
53+
<Reference Include="System.Xml" />
54+
</ItemGroup>
55+
<ItemGroup>
56+
<Compile Include="Program.cs" />
57+
<Compile Include="Properties\AssemblyInfo.cs" />
58+
</ItemGroup>
59+
<ItemGroup>
60+
<None Include="packages.config" />
61+
</ItemGroup>
62+
<ItemGroup>
63+
<ProjectReference Include="..\..\src\LightStep\LightStep.csproj">
64+
<Project>{e5f75d2a-b882-46bb-a173-0f77e46498e2}</Project>
65+
<Name>LightStep</Name>
66+
</ProjectReference>
67+
</ItemGroup>
68+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
69+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
70+
Other similar extension points exist, see Microsoft.Common.targets.
71+
<Target Name="BeforeBuild">
72+
</Target>
73+
<Target Name="AfterBuild">
74+
</Target>
75+
-->
76+
</Project>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Threading;
3+
using Castle.MicroKernel.Registration;
4+
using Castle.Windsor;
5+
using OpenTracing;
6+
using LightStep;
7+
8+
namespace LightStep.CSharpDITestApp
9+
{
10+
internal class Program
11+
{
12+
public static void Main(string[] args)
13+
{
14+
// replace these options with your satellite and api key
15+
var tracerOptions = new Options("TEST_TOKEN", new SatelliteOptions("localhost", 9996, true));
16+
var container = new WindsorContainer();
17+
// during registration, pass your options to the concrete LightStep Tracer implementation
18+
container.Register(Component.For<ITracer>().ImplementedBy<Tracer>().DependsOn(Dependency.OnValue("options", tracerOptions)));
19+
var tracer = container.Resolve<ITracer>();
20+
21+
// create some spans
22+
for (var i = 0; i < 500; i++)
23+
using (var scope = tracer.BuildSpan("testParent").WithTag("testSpan", "true").StartActive(true))
24+
{
25+
scope.Span.Log("test");
26+
tracer.ActiveSpan.Log($"iteration {i}");
27+
Console.WriteLine("sleeping for a bit");
28+
Thread.Sleep(new Random().Next(5, 10));
29+
var innerSpan = tracer.BuildSpan("childSpan").Start();
30+
innerSpan.SetTag("innerTestTag", "true");
31+
Console.WriteLine("sleeping more...");
32+
Thread.Sleep(new Random().Next(10, 20));
33+
innerSpan.Finish();
34+
}
35+
36+
// note that OpenTracing.ITracer does not have flush as a method, so to manually flush you'll need to
37+
// get a cast of the tracer.
38+
Tracer t = (Tracer) tracer;
39+
t.Flush();
40+
}
41+
}
42+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Reflection;
2+
using System.Runtime.InteropServices;
3+
4+
// General Information about an assembly is controlled through the following
5+
// set of attributes. Change these attribute values to modify the information
6+
// associated with an assembly.
7+
[assembly: AssemblyTitle("LightStep.CSharpDITestApp")]
8+
[assembly: AssemblyDescription("")]
9+
[assembly: AssemblyConfiguration("")]
10+
[assembly: AssemblyCompany("")]
11+
[assembly: AssemblyProduct("LightStep.CSharpDITestApp")]
12+
[assembly: AssemblyCopyright("Copyright © 2018")]
13+
[assembly: AssemblyTrademark("")]
14+
[assembly: AssemblyCulture("")]
15+
16+
// Setting ComVisible to false makes the types in this assembly not visible
17+
// to COM components. If you need to access a type in this assembly from
18+
// COM, set the ComVisible attribute to true on that type.
19+
[assembly: ComVisible(false)]
20+
21+
// The following GUID is for the ID of the typelib if this project is exposed to COM
22+
[assembly: Guid("358E688A-D19E-4149-85CD-E804738C0C3F")]
23+
24+
// Version information for an assembly consists of the following four values:
25+
//
26+
// Major Version
27+
// Minor Version
28+
// Build Number
29+
// Revision
30+
//
31+
// You can specify all the values or you can default the Build and Revision Numbers
32+
// by using the '*' as shown below:
33+
// [assembly: AssemblyVersion("1.0.*")]
34+
[assembly: AssemblyVersion("1.0.0.0")]
35+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="Castle.Core" version="4.2.0" targetFramework="net471" />
4+
<package id="Castle.Windsor" version="4.1.1" targetFramework="net471" />
5+
<package id="OpenTracing" version="0.12.0" targetFramework="net471" />
6+
</packages>

src/LightStep/Options.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.Win32;
22
using System;
33
using System.Collections.Generic;
4+
using System.Linq;
45
using System.Reflection;
56
using System.Runtime.Versioning;
67

@@ -128,7 +129,19 @@ private static string GetComponentName()
128129
private static string GetPlatformVersion()
129130
{
130131
#if NET45
131-
return AppDomain.CurrentDomain.SetupInformation.TargetFrameworkName;
132+
var version = "";
133+
version = AppDomain.CurrentDomain.SetupInformation.TargetFrameworkName;
134+
// in unit testing scenarios, GetEntryAssembly returns null so make sure we aren't blowing up if this isn't available
135+
if (version == null && Assembly.GetEntryAssembly() != null)
136+
{
137+
TargetFrameworkAttribute tfa = (TargetFrameworkAttribute) Assembly.GetEntryAssembly().GetCustomAttributes(typeof(TargetFrameworkAttribute))
138+
.SingleOrDefault();
139+
if (tfa != null)
140+
{
141+
version = tfa.FrameworkName;
142+
}
143+
}
144+
return version;
132145
#elif NETSTANDARD2_0
133146
return Assembly.GetEntryAssembly()?.GetCustomAttribute<TargetFrameworkAttribute>()?.FrameworkName;
134147
#else

0 commit comments

Comments
 (0)