-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3439be8
commit 865520f
Showing
13 changed files
with
929 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Worker"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Jaeger" Version="0.3.7" /> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.3" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Ocelot.Tracing.OpenTracing\Ocelot.Tracing.OpenTracing.csproj" /> | ||
<ProjectReference Include="..\..\src\Ocelot\Ocelot.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Content Update="appsettings.Development.json"> | ||
<ExcludeFromSingleFile>true</ExcludeFromSingleFile> | ||
</Content> | ||
<Content Update="appsettings.json"> | ||
<ExcludeFromSingleFile>true</ExcludeFromSingleFile> | ||
</Content> | ||
<Content Update="ocelot.json"> | ||
<ExcludeFromSingleFile>true</ExcludeFromSingleFile> | ||
</Content> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
namespace OcelotOpenTracing | ||
{ | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Hosting; | ||
using System.IO; | ||
using Ocelot.DependencyInjection; | ||
using Ocelot.Middleware; | ||
using Microsoft.Extensions.Logging; | ||
using Ocelot.Tracing.OpenTracing; | ||
using Jaeger; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using OpenTracing; | ||
using OpenTracing.Util; | ||
|
||
internal static class Program | ||
{ | ||
private static void Main(string[] args) | ||
{ | ||
Host.CreateDefaultBuilder() | ||
.ConfigureWebHostDefaults(webBuilder => | ||
{ | ||
webBuilder | ||
.UseContentRoot(Directory.GetCurrentDirectory()) | ||
.UseKestrel() | ||
.ConfigureAppConfiguration((hostingContext, config) => | ||
{ | ||
config | ||
.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath) | ||
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: false) | ||
.AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", | ||
optional: true, reloadOnChange: false) | ||
.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true) | ||
.AddEnvironmentVariables(); | ||
}) | ||
.ConfigureServices((context, services) => | ||
{ | ||
services.AddSingleton<ITracer>(sp => | ||
{ | ||
var loggerFactory = sp.GetService<ILoggerFactory>(); | ||
Configuration config = new Configuration(context.HostingEnvironment.ApplicationName, loggerFactory); | ||
var tracer = config.GetTracer(); | ||
GlobalTracer.Register(tracer); | ||
return tracer; | ||
}); | ||
services | ||
.AddOcelot() | ||
.AddOpenTracing(); | ||
}) | ||
.ConfigureLogging(logging => | ||
{ | ||
logging.AddConsole(); | ||
}) | ||
.Configure(app => | ||
{ | ||
app.UseOcelot().Wait(); | ||
}); | ||
}) | ||
.Build() | ||
.Run(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft": "Warning", | ||
"Microsoft.Hosting.Lifetime": "Information" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft": "Warning", | ||
"Microsoft.Hosting.Lifetime": "Information" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"ReRoutes": [ | ||
{ | ||
"HttpHandlerOptions": { | ||
"UseTracing": true | ||
}, | ||
"DownstreamPathTemplate": "/todos/{id}", | ||
"DownstreamScheme": "https", | ||
"DownstreamHostAndPorts": [ | ||
{ | ||
"Host": "jsonplaceholder.typicode.com", | ||
"Port": 443 | ||
} | ||
], | ||
"UpstreamPathTemplate": "/posts/{id}", | ||
"UpstreamHttpMethod": [ | ||
"Get" | ||
] | ||
} | ||
], | ||
"GlobalConfiguration": { | ||
"BaseUrl": "https://localhost:5000" | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/Ocelot.Tracing.OpenTracing/Ocelot.Tracing.OpenTracing.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
<Version>0.0.0-dev</Version> | ||
<Authors>Kjell-Åke Gafvelin</Authors> | ||
<Description>This package provides OpenTracing support to Ocelot.</Description> | ||
<PackageProjectUrl>https://github.com/ThreeMammals/Ocelot</PackageProjectUrl> | ||
<PackageTags>API Gateway;.NET core; OpenTracing</PackageTags> | ||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="OpenTracing" Version="0.12.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Ocelot\Ocelot.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace Ocelot.Tracing.OpenTracing | ||
{ | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
using Ocelot.DependencyInjection; | ||
using Ocelot.Logging; | ||
|
||
public static class OcelotBuilderExtensions | ||
{ | ||
public static IOcelotBuilder AddOpenTracing(this IOcelotBuilder builder) | ||
{ | ||
builder.Services.TryAddSingleton<ITracer, OpenTracingTracer>(); | ||
return builder; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
namespace Ocelot.Tracing.OpenTracing | ||
{ | ||
using global::OpenTracing; | ||
using global::OpenTracing.Propagation; | ||
using global::OpenTracing.Tag; | ||
using Microsoft.AspNetCore.Http; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
class OpenTracingTracer : Logging.ITracer | ||
{ | ||
private readonly ITracer _tracer; | ||
|
||
public OpenTracingTracer(ITracer tracer) | ||
{ | ||
_tracer = tracer ?? throw new ArgumentNullException(nameof(tracer)); | ||
} | ||
|
||
public void Event(HttpContext httpContext, string @event) | ||
{ | ||
} | ||
|
||
public async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, | ||
CancellationToken cancellationToken, | ||
Action<string> addTraceIdToRepo, | ||
Func<HttpRequestMessage, | ||
CancellationToken, | ||
Task<HttpResponseMessage>> baseSendAsync) | ||
{ | ||
using (IScope scope = _tracer.BuildSpan(request.RequestUri.AbsoluteUri).StartActive(finishSpanOnDispose: true)) | ||
{ | ||
var span = scope.Span; | ||
|
||
span.SetTag(Tags.SpanKind, Tags.SpanKindClient) | ||
.SetTag(Tags.HttpMethod, request.Method.Method) | ||
.SetTag(Tags.HttpUrl, request.RequestUri.OriginalString); | ||
|
||
addTraceIdToRepo(span.Context.SpanId); | ||
|
||
var headers = new Dictionary<string, string>(); | ||
|
||
_tracer.Inject(span.Context, BuiltinFormats.HttpHeaders, new TextMapInjectAdapter(headers)); | ||
|
||
foreach (var item in headers) | ||
{ | ||
request.Headers.Add(item.Key, item.Value); | ||
} | ||
|
||
try | ||
{ | ||
var response = await baseSendAsync(request, cancellationToken); | ||
|
||
span.SetTag(Tags.HttpStatus, (int)response.StatusCode); | ||
|
||
return response; | ||
} | ||
catch (HttpRequestException ex) | ||
{ | ||
Tags.Error.Set(scope.Span, true); | ||
|
||
span.Log(new Dictionary<string, object>(3) | ||
{ | ||
{ LogFields.Event, Tags.Error.Key }, | ||
{ LogFields.ErrorKind, ex.GetType().Name }, | ||
{ LogFields.ErrorObject, ex } | ||
}); | ||
throw; | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.