From a91e59c8389898bc33303202447f5c6ecae922fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D1=83=D1=84=D1=80=D0=B8=D0=B5=D0=B2=20=D0=90?= =?UTF-8?q?=D0=BB=D0=B5=D0=BA=D1=81=D0=B5=D0=B9?= Date: Thu, 16 Feb 2017 21:46:52 +0300 Subject: [PATCH] InjectController.Event implemented as separate middleware. Updated NETCoreApp to 1.1.0. --- .../Prometheus.Demo/InjestEventsMiddleware.cs | 75 +++++++++++++++++++ test/Prometheus.Demo/Startup.cs | 2 + test/Prometheus.Demo/project.json | 30 ++++---- 3 files changed, 92 insertions(+), 15 deletions(-) create mode 100644 test/Prometheus.Demo/InjestEventsMiddleware.cs diff --git a/test/Prometheus.Demo/InjestEventsMiddleware.cs b/test/Prometheus.Demo/InjestEventsMiddleware.cs new file mode 100644 index 0000000..fdc568e --- /dev/null +++ b/test/Prometheus.Demo/InjestEventsMiddleware.cs @@ -0,0 +1,75 @@ +using System; +using System.IO; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using Newtonsoft.Json; +using Prometheus.Demo.Controllers; + +namespace Prometheus.Demo +{ + public class InjestEventsMiddleware + { + private readonly HttpClient _client = new HttpClient(new HttpClientHandler + { + MaxConnectionsPerServer = int.MaxValue, + UseProxy = false, // Perf sensitive + UseCookies = false + }); + + private readonly Uri _proxyEndpointUri; + + private readonly JsonSerializer _serializer = new JsonSerializer(); + + public InjestEventsMiddleware(RequestDelegate next, IOptions settings) + { + if (!string.IsNullOrEmpty(settings.Value.ProxyFor)) + { + _proxyEndpointUri = new Uri(settings.Value.ProxyFor + "/ingest/data"); + } + } + + public async Task Invoke(HttpContext httpContext) + { + if (_proxyEndpointUri == null) + return; + + Payload payload; + using (var sr = new StreamReader(httpContext.Request.Body, Encoding.UTF8)) + { + payload = (Payload) _serializer.Deserialize(sr, typeof(Payload)); + } + + if (string.IsNullOrEmpty(payload.Data)) + { + return; + } + + var data = JsonConvert.DeserializeObject(payload.Data); + + using (var message = new HttpRequestMessage(HttpMethod.Post, _proxyEndpointUri)) + { + message.Content = new StringContent(JsonConvert.SerializeObject(payload), Encoding.UTF8, "application/json"); + + using (var response = await _client.SendAsync(message)) + { + response.EnsureSuccessStatusCode(); + } + } + } + } + + public static class InjestEventsMiddlewareExtensions + { + public static IApplicationBuilder UseInjestEventsMiddleware(this IApplicationBuilder builder) + { + builder + .Map("/injest/event", app => app.UseMiddleware()); + return builder; + } + } +} \ No newline at end of file diff --git a/test/Prometheus.Demo/Startup.cs b/test/Prometheus.Demo/Startup.cs index 09f28a0..e815432 100644 --- a/test/Prometheus.Demo/Startup.cs +++ b/test/Prometheus.Demo/Startup.cs @@ -61,6 +61,8 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF //app.UseDeveloperExceptionPage(); + app.UseInjestEventsMiddleware(); + app.UsePrometheusMiddleware(); app.UseStaticFiles(); diff --git a/test/Prometheus.Demo/project.json b/test/Prometheus.Demo/project.json index 344c55b..7efb306 100644 --- a/test/Prometheus.Demo/project.json +++ b/test/Prometheus.Demo/project.json @@ -1,26 +1,26 @@ { "dependencies": { "Microsoft.NETCore.App": { - "version": "1.0.1", + "version": "1.1.0", "type": "platform" }, - "Microsoft.AspNetCore.Diagnostics": "1.0.0", - "Microsoft.AspNetCore.Mvc": "1.0.1", "Microsoft.AspNetCore.Razor.Tools": { "version": "1.0.0-preview2-final", "type": "build" }, - "Microsoft.AspNetCore.Routing": "1.0.1", - "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0", - "Microsoft.AspNetCore.Server.Kestrel": "1.0.1", - "Microsoft.AspNetCore.StaticFiles": "1.0.0", - "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0", - "Microsoft.Extensions.Configuration.Json": "1.0.0", - "Microsoft.Extensions.Logging": "1.0.0", - "Microsoft.Extensions.Logging.Console": "1.0.0", - "Microsoft.Extensions.Logging.Debug": "1.0.0", - "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0", - "Prometheus.AspNetCore": "1.0.0-*" + "Prometheus.AspNetCore": "1.0.0-*", + "Microsoft.AspNetCore.Routing": "1.1.0", + "Microsoft.AspNetCore.Server.IISIntegration": "1.1.0", + "Microsoft.AspNetCore.Server.Kestrel": "1.1.0", + "Microsoft.AspNetCore.StaticFiles": "1.1.0", + "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.1.0", + "Microsoft.Extensions.Configuration.Json": "1.1.0", + "Microsoft.Extensions.Logging": "1.1.0", + "Microsoft.Extensions.Logging.Console": "1.1.0", + "Microsoft.Extensions.Logging.Debug": "1.1.0", + "Microsoft.Extensions.Options.ConfigurationExtensions": "1.1.0", + "Microsoft.AspNetCore.Diagnostics": "1.1.0", + "Microsoft.AspNetCore.Mvc": "1.1.1" }, "tools": { @@ -42,7 +42,7 @@ "runtimeOptions": { "configProperties": { - "System.GC.Server": false + "System.GC.Server": true } },