Skip to content
This repository has been archived by the owner on Apr 28, 2022. It is now read-only.

Commit

Permalink
Added .NetFx Web API example (non .net core) (#96)
Browse files Browse the repository at this point in the history
 Added .NetFx Web API example (non .net core)

- Demonstrates that Jeager can be added to 'legacy' Web API projects
- Registers ITracer with the DI container (like the .net core example)

Signed-off-by: xdarsie <javier.darsie@gmail.com>
  • Loading branch information
xdarsie authored and Falco20019 committed Oct 3, 2018
1 parent 8793de7 commit 1bcdabb
Show file tree
Hide file tree
Showing 12 changed files with 405 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Jaeger.sln
Expand Up @@ -32,6 +32,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "examples", "examples", "{1A
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jaeger.Example.WebApi", "examples\Jaeger.Example.WebApi\Jaeger.Example.WebApi.csproj", "{258F726B-6E51-4F18-9FB6-41D09A836904}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jaeger.Example.WebApi.NetFx", "examples\Jaeger.Example.WebApi.NetFx\Jaeger.Example.WebApi.NetFx.csproj", "{4984D8D3-E8FE-4AC8-B285-AC041CB9B30F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -138,6 +140,16 @@ Global
{258F726B-6E51-4F18-9FB6-41D09A836904}.Release|x64.Build.0 = Release|Any CPU
{258F726B-6E51-4F18-9FB6-41D09A836904}.Release|x86.ActiveCfg = Release|Any CPU
{258F726B-6E51-4F18-9FB6-41D09A836904}.Release|x86.Build.0 = Release|Any CPU
{4984D8D3-E8FE-4AC8-B285-AC041CB9B30F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4984D8D3-E8FE-4AC8-B285-AC041CB9B30F}.Debug|x64.ActiveCfg = Debug|Any CPU
{4984D8D3-E8FE-4AC8-B285-AC041CB9B30F}.Debug|x64.Build.0 = Debug|Any CPU
{4984D8D3-E8FE-4AC8-B285-AC041CB9B30F}.Debug|x86.ActiveCfg = Debug|Any CPU
{4984D8D3-E8FE-4AC8-B285-AC041CB9B30F}.Debug|x86.Build.0 = Debug|Any CPU
{4984D8D3-E8FE-4AC8-B285-AC041CB9B30F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4984D8D3-E8FE-4AC8-B285-AC041CB9B30F}.Release|x64.ActiveCfg = Release|Any CPU
{4984D8D3-E8FE-4AC8-B285-AC041CB9B30F}.Release|x64.Build.0 = Release|Any CPU
{4984D8D3-E8FE-4AC8-B285-AC041CB9B30F}.Release|x86.ActiveCfg = Release|Any CPU
{4984D8D3-E8FE-4AC8-B285-AC041CB9B30F}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -154,6 +166,7 @@ Global
{B5C1039A-75E5-4F7F-BDB0-4A9EEA2716D5} = {B699D79B-C838-4636-B5FF-86FF6D571682}
{ABA4F1B1-F6D0-40A9-904B-EFA98A4C0411} = {4FB0DAC9-D8C6-4368-B48E-363FA85029A4}
{258F726B-6E51-4F18-9FB6-41D09A836904} = {1ADE2BBF-56C4-433D-9B1C-A532915058D3}
{4984D8D3-E8FE-4AC8-B285-AC041CB9B30F} = {1ADE2BBF-56C4-433D-9B1C-A532915058D3}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {83668757-63F5-48E3-A8E0-8AED6B7562D0}
Expand Down
32 changes: 32 additions & 0 deletions examples/Jaeger.Example.WebApi.NetFx/App_Start/AutofacConfig.cs
@@ -0,0 +1,32 @@
using Autofac;
using Autofac.Integration.WebApi;
using Jaeger.Samplers;
using OpenTracing;
using System.Reflection;
using System.Web.Http;

namespace Jaeger.Example.WebApi.NetFx
{
public class AutofacConfig
{
private static readonly Tracer tracer;

static AutofacConfig()
{
tracer = new Tracer.Builder("WebApi-NetFx")
.WithSampler(new ConstSampler(true))
.Build();
}

public static void Register(HttpConfiguration config)
{
var builder = new ContainerBuilder();

builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

builder.RegisterInstance(tracer).As<ITracer>();

config.DependencyResolver = new AutofacWebApiDependencyResolver(builder.Build());
}
}
}
13 changes: 13 additions & 0 deletions examples/Jaeger.Example.WebApi.NetFx/App_Start/WebApiConfig.cs
@@ -0,0 +1,13 @@
using System.Web.Http;

namespace Jaeger.Example.WebApi.NetFx
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API routes
config.MapHttpAttributeRoutes();
}
}
}
@@ -0,0 +1,34 @@
using OpenTracing;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Web.Http;

namespace Jaeger.Example.WebApi.NetFx.Controllers
{
[RoutePrefix("values")]
public class ValuesController : ApiController
{
private readonly ITracer _tracer;

public ValuesController(ITracer tracer)
{
_tracer = tracer;
}

[HttpGet]
[Route("")]
public IEnumerable<string> GetAll()
{
using (var scope1 = _tracer.BuildSpan("Controller-GetAll").StartActive(true))
{
using (var scope2 = _tracer.BuildSpan("DataSource-GetAll").StartActive(true))
{
Thread.Sleep(TimeSpan.FromSeconds(1));

return new string[] { "value1", "value2" };
}
}
}
}
}
1 change: 1 addition & 0 deletions examples/Jaeger.Example.WebApi.NetFx/Global.asax
@@ -0,0 +1 @@
<%@ Application Codebehind="Global.asax.cs" Inherits="Jaeger.Example.WebApi.NetFx.WebApiApplication" Language="C#" %>
13 changes: 13 additions & 0 deletions examples/Jaeger.Example.WebApi.NetFx/Global.asax.cs
@@ -0,0 +1,13 @@
using System.Web.Http;

namespace Jaeger.Example.WebApi.NetFx
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
GlobalConfiguration.Configure(AutofacConfig.Register);
}
}
}
@@ -0,0 +1,166 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.0\build\net46\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props" Condition="Exists('..\..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.0\build\net46\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props')" />
<Import Project="..\Directory.Build.props" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>
</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{4984D8D3-E8FE-4AC8-B285-AC041CB9B30F}</ProjectGuid>
<ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Jaeger.Example.WebApi.NetFx</RootNamespace>
<AssemblyName>Jaeger.Example.WebApi.NetFx</AssemblyName>
<TargetFrameworkVersion>v4.7.1</TargetFrameworkVersion>
<MvcBuildViews>false</MvcBuildViews>
<UseIISExpress>true</UseIISExpress>
<Use64BitIISExpress />
<IISExpressSSLPort />
<IISExpressAnonymousAuthentication />
<IISExpressWindowsAuthentication />
<IISExpressUseClassicPipelineMode />
<UseGlobalApplicationHostFile />
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Autofac, Version=4.8.1.0, Culture=neutral, PublicKeyToken=17863af14b0044da, processorArchitecture=MSIL">
<HintPath>..\..\packages\Autofac.4.8.1\lib\net45\Autofac.dll</HintPath>
</Reference>
<Reference Include="Autofac.Integration.WebApi, Version=4.2.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da, processorArchitecture=MSIL">
<HintPath>..\..\packages\Autofac.WebApi2.4.2.0\lib\net45\Autofac.Integration.WebApi.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.0\lib\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Microsoft.Extensions.Logging.Abstractions, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.Extensions.Logging.Abstractions.2.1.1\lib\netstandard2.0\Microsoft.Extensions.Logging.Abstractions.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="OpenTracing, Version=0.12.0.0, Culture=neutral, PublicKeyToken=61503406977abdaf, processorArchitecture=MSIL">
<HintPath>..\..\packages\OpenTracing.0.12.0\lib\net45\OpenTracing.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Net.Http.Formatting, Version=5.2.6.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.AspNet.WebApi.Client.5.2.6\lib\net45\System.Net.Http.Formatting.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.Remoting" />
<Reference Include="System.Web.Entity" />
<Reference Include="System.Web.ApplicationServices" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Web.Http, Version=5.2.6.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.AspNet.WebApi.Core.5.2.6\lib\net45\System.Web.Http.dll</HintPath>
</Reference>
<Reference Include="System.Web.Http.WebHost, Version=5.2.6.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.AspNet.WebApi.WebHost.5.2.6\lib\net45\System.Web.Http.WebHost.dll</HintPath>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Web" />
<Reference Include="System.Web.Abstractions" />
<Reference Include="System.Web.Routing" />
<Reference Include="System.Xml" />
<Reference Include="System.Configuration" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Net.Http">
</Reference>
<Reference Include="System.Net.Http.WebRequest">
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="App_Start\WebApiConfig.cs" />
<Compile Include="App_Start\AutofacConfig.cs" />
<Compile Include="Controllers\ValuesController.cs" />
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="Global.asax" />
<Content Include="Web.config" />
<Content Include="Web.Debug.config">
<DependentUpon>Web.config</DependentUpon>
</Content>
<Content Include="Web.Release.config">
<DependentUpon>Web.config</DependentUpon>
</Content>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Jaeger\Jaeger.csproj">
<Project>{aba4f1b1-f6d0-40a9-904b-efa98a4c0411}</Project>
<Name>Jaeger</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" Condition="false" />
<Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
<AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
</Target>
<ProjectExtensions>
<VisualStudio>
<FlavorProperties GUID="{349c5851-65df-11da-9384-00065b846f21}">
<WebProjectProperties>
<UseIIS>True</UseIIS>
<AutoAssignPort>True</AutoAssignPort>
<DevelopmentServerPort>57084</DevelopmentServerPort>
<DevelopmentServerVPath>/</DevelopmentServerVPath>
<IISUrl>http://localhost:57084/</IISUrl>
<NTLMAuthentication>False</NTLMAuthentication>
<UseCustomServer>False</UseCustomServer>
<CustomServerUrl>
</CustomServerUrl>
<SaveServerSettingsInUserFile>False</SaveServerSettingsInUserFile>
</WebProjectProperties>
</FlavorProperties>
</VisualStudio>
</ProjectExtensions>
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.0\build\net46\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.0\build\net46\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target> -->
</Project>
@@ -0,0 +1,7 @@
using System.Reflection;

[assembly: AssemblyTitle("Jaeger.Example.WebApi.NetFx")]
[assembly: AssemblyProduct("Jaeger.Example.WebApi.NetFx")]
[assembly: AssemblyCopyright("Copyright 2018 (c) The Jaeger Authors. Copyright 2018 (c) Chatham Financial Corp.")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
6 changes: 6 additions & 0 deletions examples/Jaeger.Example.WebApi.NetFx/Web.Debug.config
@@ -0,0 +1,6 @@
<?xml version="1.0"?>

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.web>
</system.web>
</configuration>
7 changes: 7 additions & 0 deletions examples/Jaeger.Example.WebApi.NetFx/Web.Release.config
@@ -0,0 +1,7 @@
<?xml version="1.0"?>

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
</system.web>
</configuration>

0 comments on commit 1bcdabb

Please sign in to comment.