-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#145 adding env info endpoint, also adjusting content types on other …
…endpoints
- Loading branch information
Showing
21 changed files
with
299 additions
and
35 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
18 changes: 18 additions & 0 deletions
18
src/App.Metrics.Extensions.Middleware/Abstractions/IEnvironmentInfoResponseWriter.cs
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,18 @@ | ||
// <copyright file="IEnvironmentInfoResponseWriter.cs" company="Allan Hardy"> | ||
// Copyright (c) Allan Hardy. All rights reserved. | ||
// </copyright> | ||
|
||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using App.Metrics.Infrastructure; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace App.Metrics.Extensions.Middleware.Abstractions | ||
{ | ||
public interface IEnvironmentInfoResponseWriter | ||
{ | ||
string ContentType { get; } | ||
|
||
Task WriteAsync(HttpContext context, EnvironmentInfo environmentInfo, CancellationToken token = default(CancellationToken)); | ||
} | ||
} |
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
60 changes: 60 additions & 0 deletions
60
src/App.Metrics.Extensions.Middleware/EnvironmentInfoMiddleware.cs
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,60 @@ | ||
// <copyright file="EnvironmentInfoMiddleware.cs" company="Allan Hardy"> | ||
// Copyright (c) Allan Hardy. All rights reserved. | ||
// </copyright> | ||
|
||
using System; | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using App.Metrics.Extensions.Middleware.Abstractions; | ||
using App.Metrics.Extensions.Middleware.DependencyInjection.Options; | ||
using App.Metrics.Infrastructure; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace App.Metrics.Extensions.Middleware | ||
{ | ||
// ReSharper disable ClassNeverInstantiated.Global | ||
public class EnvironmentInfoMiddleware : AppMetricsMiddleware<AspNetMetricsOptions> | ||
// ReSharper restore ClassNeverInstantiated.Global | ||
{ | ||
private readonly EnvironmentInfoProvider _environmentInfoProvider; | ||
private readonly IEnvironmentInfoResponseWriter _environmentInfoResponseWriter; | ||
private readonly RequestDelegate _next; | ||
|
||
public EnvironmentInfoMiddleware( | ||
RequestDelegate next, | ||
AspNetMetricsOptions aspNetOptions, | ||
ILoggerFactory loggerFactory, | ||
IMetrics metrics, | ||
IEnvironmentInfoResponseWriter environmentInfoResponseWriter, | ||
EnvironmentInfoProvider environmentInfoProvider) | ||
: base(next, aspNetOptions, loggerFactory, metrics) | ||
{ | ||
_environmentInfoProvider = environmentInfoProvider; | ||
_environmentInfoResponseWriter = environmentInfoResponseWriter ?? throw new ArgumentNullException(nameof(environmentInfoResponseWriter)); | ||
_next = next ?? throw new ArgumentNullException(nameof(next)); | ||
} | ||
|
||
// ReSharper disable UnusedMember.Global | ||
public async Task Invoke(HttpContext context) | ||
// ReSharper restore UnusedMember.Global | ||
{ | ||
if (Options.EnvironmentInfoEndpointEnabled && Options.EnvironmentInfoEndpoint.IsPresent() && Options.EnvironmentInfoEndpoint == context.Request.Path) | ||
{ | ||
Logger.MiddlewareExecuting(GetType()); | ||
|
||
context.Response.Headers["Content-Type"] = new[] { _environmentInfoResponseWriter.ContentType }; | ||
context.SetNoCacheHeaders(); | ||
context.Response.StatusCode = (int)HttpStatusCode.OK; | ||
|
||
await _environmentInfoResponseWriter.WriteAsync(context, _environmentInfoProvider.Build(), context.RequestAborted).ConfigureAwait(false); | ||
|
||
Logger.MiddlewareExecuted(GetType()); | ||
|
||
return; | ||
} | ||
|
||
await _next(context); | ||
} | ||
} | ||
} |
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
26 changes: 26 additions & 0 deletions
26
src/App.Metrics.Extensions.Middleware/Internal/NoOpEnvironmentInfoResponseWriter.cs
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,26 @@ | ||
// <copyright file="NoOpEnvironmentInfoResponseWriter.cs" company="Allan Hardy"> | ||
// Copyright (c) Allan Hardy. All rights reserved. | ||
// </copyright> | ||
|
||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using App.Metrics.Core.Internal; | ||
using App.Metrics.Extensions.Middleware.Abstractions; | ||
using App.Metrics.Infrastructure; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace App.Metrics.Extensions.Middleware.Internal | ||
{ | ||
[AppMetricsExcludeFromCodeCoverage] | ||
internal class NoOpEnvironmentInfoResponseWriter : IEnvironmentInfoResponseWriter | ||
{ | ||
/// <inheritdoc /> | ||
public string ContentType => "text/plain"; | ||
|
||
/// <inheritdoc /> | ||
public Task WriteAsync(HttpContext context, EnvironmentInfo environmentInfo, CancellationToken token = default(CancellationToken)) | ||
{ | ||
return context.Response.WriteAsync("No formatter has been registered. See App.Metrics.Formatters.Ascii for example.", token); | ||
} | ||
} | ||
} |
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
24 changes: 24 additions & 0 deletions
24
src/App.Metrics.Formatters.Ascii/AsciiEnvironmentInfoResponseWriter.cs
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 @@ | ||
// <copyright file="AsciiEnvironmentInfoResponseWriter.cs" company="Allan Hardy"> | ||
// Copyright (c) Allan Hardy. All rights reserved. | ||
// </copyright> | ||
|
||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using App.Metrics.Extensions.Middleware.Abstractions; | ||
using App.Metrics.Formatting.Humanize; | ||
using App.Metrics.Infrastructure; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace App.Metrics.Formatters.Ascii | ||
{ | ||
public class AsciiEnvironmentInfoResponseWriter : IEnvironmentInfoResponseWriter | ||
{ | ||
/// <inheritdoc /> | ||
public string ContentType => "text/plain; app.metrics=vnd.app.metrics.v1.environment.info;"; | ||
|
||
public Task WriteAsync(HttpContext context, EnvironmentInfo environmentInfo, CancellationToken token = default(CancellationToken)) | ||
{ | ||
return context.Response.WriteAsync(environmentInfo.Hummanize(), token); | ||
} | ||
} | ||
} |
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
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
13 changes: 13 additions & 0 deletions
13
src/App.Metrics.Formatters.Json/Abstractions/Serialization/IEnvironmentInfoSerializer.cs
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,13 @@ | ||
// <copyright file="IEnvironmentInfoSerializer.cs" company="Allan Hardy"> | ||
// Copyright (c) Allan Hardy. All rights reserved. | ||
// </copyright> | ||
|
||
namespace App.Metrics.Formatters.Json.Abstractions.Serialization | ||
{ | ||
public interface IEnvironmentInfoSerializer | ||
{ | ||
T Deserialize<T>(string value); | ||
|
||
string Serialize<T>(T value); | ||
} | ||
} |
Oops, something went wrong.