Skip to content
This repository has been archived by the owner on Feb 6, 2025. It is now read-only.

Commit

Permalink
Extracted plugins #1, refactoring, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
spetz committed Sep 12, 2019
1 parent 0bdf676 commit 7b3242f
Show file tree
Hide file tree
Showing 64 changed files with 568 additions and 326 deletions.
45 changes: 45 additions & 0 deletions Ntrada.sln
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ntrada.Core", "src\Ntrada.C
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ntrada.Extensions.RabbitMq", "extensions\Ntrada.Extensions.RabbitMq\Ntrada.Extensions.RabbitMq.csproj", "{4C88EF7A-90AC-4E3E-A819-2CC4CCC17402}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ntrada.Extensions.CustomErrors", "extensions\Ntrada.Extensions.CustomErrors\Ntrada.Extensions.CustomErrors.csproj", "{AB8D52BD-06EE-4465-90EC-C77A36FB1B23}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ntrada.Extensions.Cors", "extensions\Ntrada.Extensions.Cors\Ntrada.Extensions.Cors.csproj", "{29689A6F-5460-4100-B51A-78EF322DF5D5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ntrada.Extensions.Tracing", "extensions\Ntrada.Extensions.Tracing\Ntrada.Extensions.Tracing.csproj", "{CC9A3D1E-84DC-43A2-9BC1-BBFE0DF471AF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -136,6 +142,42 @@ Global
{4C88EF7A-90AC-4E3E-A819-2CC4CCC17402}.Release|x64.Build.0 = Release|Any CPU
{4C88EF7A-90AC-4E3E-A819-2CC4CCC17402}.Release|x86.ActiveCfg = Release|Any CPU
{4C88EF7A-90AC-4E3E-A819-2CC4CCC17402}.Release|x86.Build.0 = Release|Any CPU
{AB8D52BD-06EE-4465-90EC-C77A36FB1B23}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AB8D52BD-06EE-4465-90EC-C77A36FB1B23}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AB8D52BD-06EE-4465-90EC-C77A36FB1B23}.Debug|x64.ActiveCfg = Debug|Any CPU
{AB8D52BD-06EE-4465-90EC-C77A36FB1B23}.Debug|x64.Build.0 = Debug|Any CPU
{AB8D52BD-06EE-4465-90EC-C77A36FB1B23}.Debug|x86.ActiveCfg = Debug|Any CPU
{AB8D52BD-06EE-4465-90EC-C77A36FB1B23}.Debug|x86.Build.0 = Debug|Any CPU
{AB8D52BD-06EE-4465-90EC-C77A36FB1B23}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AB8D52BD-06EE-4465-90EC-C77A36FB1B23}.Release|Any CPU.Build.0 = Release|Any CPU
{AB8D52BD-06EE-4465-90EC-C77A36FB1B23}.Release|x64.ActiveCfg = Release|Any CPU
{AB8D52BD-06EE-4465-90EC-C77A36FB1B23}.Release|x64.Build.0 = Release|Any CPU
{AB8D52BD-06EE-4465-90EC-C77A36FB1B23}.Release|x86.ActiveCfg = Release|Any CPU
{AB8D52BD-06EE-4465-90EC-C77A36FB1B23}.Release|x86.Build.0 = Release|Any CPU
{29689A6F-5460-4100-B51A-78EF322DF5D5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{29689A6F-5460-4100-B51A-78EF322DF5D5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{29689A6F-5460-4100-B51A-78EF322DF5D5}.Debug|x64.ActiveCfg = Debug|Any CPU
{29689A6F-5460-4100-B51A-78EF322DF5D5}.Debug|x64.Build.0 = Debug|Any CPU
{29689A6F-5460-4100-B51A-78EF322DF5D5}.Debug|x86.ActiveCfg = Debug|Any CPU
{29689A6F-5460-4100-B51A-78EF322DF5D5}.Debug|x86.Build.0 = Debug|Any CPU
{29689A6F-5460-4100-B51A-78EF322DF5D5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{29689A6F-5460-4100-B51A-78EF322DF5D5}.Release|Any CPU.Build.0 = Release|Any CPU
{29689A6F-5460-4100-B51A-78EF322DF5D5}.Release|x64.ActiveCfg = Release|Any CPU
{29689A6F-5460-4100-B51A-78EF322DF5D5}.Release|x64.Build.0 = Release|Any CPU
{29689A6F-5460-4100-B51A-78EF322DF5D5}.Release|x86.ActiveCfg = Release|Any CPU
{29689A6F-5460-4100-B51A-78EF322DF5D5}.Release|x86.Build.0 = Release|Any CPU
{CC9A3D1E-84DC-43A2-9BC1-BBFE0DF471AF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CC9A3D1E-84DC-43A2-9BC1-BBFE0DF471AF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CC9A3D1E-84DC-43A2-9BC1-BBFE0DF471AF}.Debug|x64.ActiveCfg = Debug|Any CPU
{CC9A3D1E-84DC-43A2-9BC1-BBFE0DF471AF}.Debug|x64.Build.0 = Debug|Any CPU
{CC9A3D1E-84DC-43A2-9BC1-BBFE0DF471AF}.Debug|x86.ActiveCfg = Debug|Any CPU
{CC9A3D1E-84DC-43A2-9BC1-BBFE0DF471AF}.Debug|x86.Build.0 = Debug|Any CPU
{CC9A3D1E-84DC-43A2-9BC1-BBFE0DF471AF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CC9A3D1E-84DC-43A2-9BC1-BBFE0DF471AF}.Release|Any CPU.Build.0 = Release|Any CPU
{CC9A3D1E-84DC-43A2-9BC1-BBFE0DF471AF}.Release|x64.ActiveCfg = Release|Any CPU
{CC9A3D1E-84DC-43A2-9BC1-BBFE0DF471AF}.Release|x64.Build.0 = Release|Any CPU
{CC9A3D1E-84DC-43A2-9BC1-BBFE0DF471AF}.Release|x86.ActiveCfg = Release|Any CPU
{CC9A3D1E-84DC-43A2-9BC1-BBFE0DF471AF}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{BD735FBA-1D9C-45AD-8BDD-1E89B059E4CC} = {10BC7168-BE16-4A3C-A7CC-F122C83E08F5}
Expand All @@ -146,5 +188,8 @@ Global
{B606AE04-E5B7-4912-AEB1-CCEDD3CBA536} = {6E8C9C7B-7249-41CD-A466-C11A8644400B}
{78C6AEEB-522C-485B-843B-16ADB8A8157F} = {10BC7168-BE16-4A3C-A7CC-F122C83E08F5}
{4C88EF7A-90AC-4E3E-A819-2CC4CCC17402} = {6E8C9C7B-7249-41CD-A466-C11A8644400B}
{AB8D52BD-06EE-4465-90EC-C77A36FB1B23} = {6E8C9C7B-7249-41CD-A466-C11A8644400B}
{29689A6F-5460-4100-B51A-78EF322DF5D5} = {6E8C9C7B-7249-41CD-A466-C11A8644400B}
{CC9A3D1E-84DC-43A2-9BC1-BBFE0DF471AF} = {6E8C9C7B-7249-41CD-A466-C11A8644400B}
EndGlobalSection
EndGlobal
46 changes: 46 additions & 0 deletions extensions/Ntrada.Extensions.Cors/CorsExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Linq;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Ntrada.Core;

namespace Ntrada.Extensions.Cors
{
public class CorsExtension : IExtension
{
public string Name => "cors";
public string Description => "Cross-Origin Resource Sharing";

public void Add(IServiceCollection services, IOptionsProvider optionsProvider)
{
var options = optionsProvider.GetForExtension<CorsOptions>(Name);
services.AddCors(cors =>
{
var allowedHeaders = options.AllowedHeaders ?? Enumerable.Empty<string>();
var allowedMethods = options.AllowedMethods ?? Enumerable.Empty<string>();
var allowedOrigins = options.AllowedOrigins ?? Enumerable.Empty<string>();
var exposedHeaders = options.ExposedHeaders ?? Enumerable.Empty<string>();
cors.AddPolicy("CorsPolicy", builder =>
{
if (options.AllowCredentials)
{
builder.AllowCredentials();
}
else
{
builder.DisallowCredentials();
}

builder.WithHeaders(allowedHeaders.ToArray())
.WithMethods(allowedMethods.ToArray())
.WithOrigins(allowedOrigins.ToArray())
.WithExposedHeaders(exposedHeaders.ToArray());
});
});
}

public void Use(IApplicationBuilder app, IOptionsProvider optionsProvider)
{
app.UseCors("CorsPolicy");
}
}
}
14 changes: 14 additions & 0 deletions extensions/Ntrada.Extensions.Cors/CorsOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Collections.Generic;
using Ntrada.Core;

namespace Ntrada.Extensions.Cors
{
public class CorsOptions : IOptions
{
public bool AllowCredentials { get; set; }
public IEnumerable<string> AllowedOrigins { get; set; }
public IEnumerable<string> AllowedMethods { get; set; }
public IEnumerable<string> AllowedHeaders { get; set; }
public IEnumerable<string> ExposedHeaders { get; set; }
}
}
11 changes: 11 additions & 0 deletions extensions/Ntrada.Extensions.Cors/Ntrada.Extensions.Cors.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Ntrada.Core\Ntrada.Core.csproj" />
</ItemGroup>

</Project>
14 changes: 14 additions & 0 deletions extensions/Ntrada.Extensions.Cors/cors.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cors:
allowCredentials: true
allowedOrigins:
- '*'
allowedMethods:
- post
- delete
allowedHeaders:
- '*'
exposedHeaders:
- Request-ID
- Resource-ID
- Trace-ID
- Total-Count
24 changes: 24 additions & 0 deletions extensions/Ntrada.Extensions.CustomErrors/CustomErrorsExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Ntrada.Core;

namespace Ntrada.Extensions.CustomErrors
{
public class CustomErrorsExtension : IExtension
{
public string Name => "customErrors";
public string Description => "Custom errors handler";

public void Add(IServiceCollection services, IOptionsProvider optionsProvider)
{
var options = optionsProvider.GetForExtension<CustomErrorsOptions>(Name);
services.AddSingleton(options);
services.AddScoped<ErrorHandlerMiddleware>();
}

public void Use(IApplicationBuilder app, IOptionsProvider optionsProvider)
{
app.UseMiddleware<ErrorHandlerMiddleware>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Ntrada.Core;

namespace Ntrada.Extensions.CustomErrors
{
public class CustomErrorsOptions : IOptions
{
public bool IncludeExceptionMessage { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
using Newtonsoft.Json;
using Ntrada.Core;

namespace Ntrada.Middleware
namespace Ntrada.Extensions.CustomErrors
{
public class ErrorHandlerMiddleware : IMiddleware
{
private readonly CustomErrorsOptions _options;
private readonly ILogger<ErrorHandlerMiddleware> _logger;

public ErrorHandlerMiddleware(ILogger<ErrorHandlerMiddleware> logger)
public ErrorHandlerMiddleware(CustomErrorsOptions options, ILogger<ErrorHandlerMiddleware> logger)
{
_options = options;
_logger = logger;
}

Expand All @@ -30,11 +32,11 @@ public async Task InvokeAsync(HttpContext context, RequestDelegate next)
}
}

private static Task HandleErrorAsync(HttpContext context, Exception exception)
private Task HandleErrorAsync(HttpContext context, Exception exception)
{
var errorCode = "error";
var statusCode = HttpStatusCode.BadRequest;
var message = exception.Message;
var message = _options.IncludeExceptionMessage ? exception.Message : "There was an error.";
var response = new
{
errors = new[]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Ntrada.Core\Ntrada.Core.csproj" />
</ItemGroup>

</Project>
2 changes: 2 additions & 0 deletions extensions/Ntrada.Extensions.CustomErrors/customErrors.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
customErrors:
includeExceptionMessage: true
1 change: 0 additions & 1 deletion extensions/Ntrada.Extensions.Jwt/JwtExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public void Add(IServiceCollection services, IOptionsProvider optionsProvider)

public void Use(IApplicationBuilder app, IOptionsProvider optionsProvider)
{
app.UseAuthentication();
}
}
}
1 change: 0 additions & 1 deletion extensions/Ntrada.Extensions.Jwt/JwtOptions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using Ntrada.Core;

namespace Ntrada.Extensions.Jwt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Jaeger.Samplers;
using OpenTracing;

namespace Ntrada.Tracing
namespace Ntrada.Extensions.Tracing
{
public class DefaultTracer
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using OpenTracing;
using OpenTracing.Tag;

namespace Ntrada.Tracing
namespace Ntrada.Extensions.Tracing
{
internal sealed class JaegerHttpMiddleware
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Jaeger" Version="0.3.4" />
<PackageReference Include="OpenTracing" Version="0.12.0" />
<PackageReference Include="OpenTracing.Contrib.Grpc" Version="0.1.0" />
<PackageReference Include="OpenTracing.Contrib.NetCore" Version="0.6.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Ntrada.Core\Ntrada.Core.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,29 @@
using Jaeger.Samplers;
using Jaeger.Senders;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Ntrada.Core;
using OpenTracing;
using OpenTracing.Util;

namespace Ntrada.Tracing
namespace Ntrada.Extensions.Tracing
{
public static class Extensions
public class TracingExtension : IExtension
{
private const string SectionName = "jaeger";
private const string RegistryName = "tracing.jaeger";

public static IServiceCollection AddJaeger(this IServiceCollection services)
public string Name => "tracing";
public string Description => "Open Tracing using Jaeger";
public void Add(IServiceCollection services, IOptionsProvider optionsProvider)
{
var options = new JaegerOptions();
using (var scope = services.BuildServiceProvider())
{
var configuration = scope.GetService<IConfiguration>();
configuration.GetSection("jaeger").Bind(options);
}

var options = optionsProvider.GetForExtension<TracingOptions>("tracing");
services.AddOpenTracing();
services.AddSingleton(options);
if (!options.Enabled)
if (options.UseEmptyTracer)
{
var defaultTracer = DefaultTracer.Create();
services.AddSingleton(defaultTracer);
return services;
return;
}

services.AddSingleton<ITracer>(sp =>
Expand All @@ -54,22 +49,14 @@ public static IServiceCollection AddJaeger(this IServiceCollection services)

return tracer;
});

return services;
}

public static IApplicationBuilder UseJaeger(this IApplicationBuilder app)
{
JaegerOptions options;
using (var scope = app.ApplicationServices.CreateScope())
{
options = scope.ServiceProvider.GetService<JaegerOptions>();
}

return options.Enabled ? app.UseMiddleware<JaegerHttpMiddleware>() : app;
public void Use(IApplicationBuilder app, IOptionsProvider optionsProvider)
{
app.UseMiddleware<JaegerHttpMiddleware>();
}

private static ISampler GetSampler(JaegerOptions options)
private static ISampler GetSampler(TracingOptions options)
{
switch (options.Sampler)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
namespace Ntrada.Tracing
using Ntrada.Core;

namespace Ntrada.Extensions.Tracing
{
public class JaegerOptions
public class TracingOptions : IOptions
{
public bool Enabled { get; set; }
public bool UseEmptyTracer { get; set; }
public string ServiceName { get; set; }
public string UdpHost { get; set; }
public int UdpPort { get; set; }
Expand Down
7 changes: 7 additions & 0 deletions extensions/Ntrada.Extensions.Tracing/tracing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
tracing:
serviceName: ntrada
udpHost: localhost
udpPort: 6831
maxPacketSize: 0
sampler: const
useEmptyTracer: false
Loading

0 comments on commit 7b3242f

Please sign in to comment.