Skip to content
This repository was archived by the owner on May 3, 2020. It is now read-only.

InjectController.Event implemented as separate middleware. Updated NETCoreApp to 1.1.0. #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions test/Prometheus.Demo/InjestEventsMiddleware.cs
Original file line number Diff line number Diff line change
@@ -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> 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<dynamic>(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<InjestEventsMiddleware>());
return builder;
}
}
}
2 changes: 2 additions & 0 deletions test/Prometheus.Demo/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF

//app.UseDeveloperExceptionPage();

app.UseInjestEventsMiddleware();

app.UsePrometheusMiddleware();

app.UseStaticFiles();
Expand Down
30 changes: 15 additions & 15 deletions test/Prometheus.Demo/project.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -42,7 +42,7 @@

"runtimeOptions": {
"configProperties": {
"System.GC.Server": false
"System.GC.Server": true
}
},

Expand Down