-
Notifications
You must be signed in to change notification settings - Fork 165
Trace Actuator Updates #1356
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Trace Actuator Updates #1356
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
154493c
Replace /trace and /httptrace with SpringBoot3-compatible /httpexchanges
TimHess 42752ce
drop HttpClientDesktopObserver
TimHess bcf8c27
Apply suggestions from code review
TimHess 025df11
Apply suggestions from code review
TimHess 7e31de4
Apply suggestions from code review
TimHess 97af1c5
PR feedback
TimHess ec98cea
Use DateTime.*Utc*Now.Date
TimHess 0f72484
Automated code cleanup from Steeltoe cibuild
aaf76b6
Add HTTP exchange test that asserts on JSON structure
bart-vmware b036ab2
update api spec, remove reflection from AspNetCoreHostingObserver, mo…
TimHess 4f048e7
address feedback
TimHess File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
src/Common/src/Common/Json/JsonIgnoreEmptyCollectionAttribute.cs
This file contains hidden or 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,14 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the Apache 2.0 License. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Text.Json; | ||
|
||
namespace Steeltoe.Common.Json; | ||
|
||
/// <summary> | ||
/// Indicates that a collection property should not be serialized if the collection is empty. Use | ||
/// <see cref="JsonSerializerOptionsExtensions.AddJsonIgnoreEmptyCollection" /> to register in <see cref="JsonSerializerOptions" />. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Property)] | ||
public sealed class JsonIgnoreEmptyCollectionAttribute : Attribute; |
48 changes: 48 additions & 0 deletions
48
src/Common/src/Common/Json/JsonSerializerOptionsExtensions.cs
This file contains hidden or 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,48 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the Apache 2.0 License. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Collections; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization.Metadata; | ||
|
||
namespace Steeltoe.Common.Json; | ||
|
||
public static class JsonSerializerOptionsExtensions | ||
{ | ||
/// <summary> | ||
/// Registers a contract modifier in the specified <see cref="JsonSerializerOptions" /> that skips serialization of empty collections marked with | ||
/// <see cref="JsonIgnoreEmptyCollectionAttribute" />. | ||
/// </summary> | ||
/// <param name="options"> | ||
/// The JSON serializer options to configure. | ||
/// </param> | ||
/// <returns> | ||
/// The incoming <paramref name="options" /> so that additional calls can be chained. | ||
/// </returns> | ||
public static JsonSerializerOptions AddJsonIgnoreEmptyCollection(this JsonSerializerOptions options) | ||
{ | ||
ArgumentNullException.ThrowIfNull(options); | ||
|
||
options.TypeInfoResolverChain.Insert(0, new DefaultJsonTypeInfoResolver | ||
{ | ||
Modifiers = | ||
{ | ||
SkipEmptyCollection | ||
} | ||
}); | ||
|
||
return options; | ||
} | ||
|
||
private static void SkipEmptyCollection(JsonTypeInfo info) | ||
{ | ||
foreach (JsonPropertyInfo property in info.Properties) | ||
{ | ||
if (property.AttributeProvider != null && property.AttributeProvider.IsDefined(typeof(JsonIgnoreEmptyCollectionAttribute), true)) | ||
{ | ||
property.ShouldSerialize = (_, value) => value is not IEnumerable enumerable || enumerable.Cast<object>().Any(); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
91 changes: 91 additions & 0 deletions
91
src/Management/src/Endpoint/Actuators/HttpExchanges/ConfigureHttpExchangesEndpointOptions.cs
This file contains hidden or 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,91 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the Apache 2.0 License. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Net.Http.Headers; | ||
using Steeltoe.Management.Endpoint.Configuration; | ||
|
||
namespace Steeltoe.Management.Endpoint.Actuators.HttpExchanges; | ||
|
||
internal sealed class ConfigureHttpExchangesEndpointOptions(IConfiguration configuration) | ||
: ConfigureEndpointOptions<HttpExchangesEndpointOptions>(configuration, ManagementInfoPrefix, "httpexchanges") | ||
{ | ||
private const string ManagementInfoPrefix = "management:endpoints:httpexchanges"; | ||
private const int DefaultCapacity = 100; | ||
|
||
private static readonly string[] DefaultAllowedRequestHeaders = | ||
[ | ||
HeaderNames.Accept, | ||
HeaderNames.AcceptCharset, | ||
HeaderNames.AcceptEncoding, | ||
HeaderNames.AcceptLanguage, | ||
HeaderNames.Allow, | ||
HeaderNames.CacheControl, | ||
HeaderNames.Connection, | ||
HeaderNames.ContentEncoding, | ||
HeaderNames.ContentLength, | ||
HeaderNames.ContentType, | ||
HeaderNames.Date, | ||
HeaderNames.DNT, | ||
HeaderNames.Expect, | ||
HeaderNames.Host, | ||
HeaderNames.MaxForwards, | ||
HeaderNames.Range, | ||
HeaderNames.SecWebSocketExtensions, | ||
HeaderNames.SecWebSocketVersion, | ||
HeaderNames.TE, | ||
HeaderNames.Trailer, | ||
HeaderNames.TransferEncoding, | ||
HeaderNames.Upgrade, | ||
HeaderNames.UserAgent, | ||
HeaderNames.Warning, | ||
HeaderNames.XRequestedWith, | ||
HeaderNames.XUACompatible | ||
]; | ||
|
||
private static readonly string[] DefaultAllowedResponseHeaders = | ||
[ | ||
HeaderNames.AcceptRanges, | ||
HeaderNames.Age, | ||
HeaderNames.Allow, | ||
HeaderNames.AltSvc, | ||
HeaderNames.Connection, | ||
HeaderNames.ContentDisposition, | ||
HeaderNames.ContentLanguage, | ||
HeaderNames.ContentLength, | ||
HeaderNames.ContentLocation, | ||
HeaderNames.ContentRange, | ||
HeaderNames.ContentType, | ||
HeaderNames.Date, | ||
HeaderNames.Expires, | ||
HeaderNames.LastModified, | ||
HeaderNames.Location, | ||
HeaderNames.Server, | ||
HeaderNames.TransferEncoding, | ||
HeaderNames.Upgrade, | ||
HeaderNames.XPoweredBy | ||
]; | ||
|
||
public override void Configure(HttpExchangesEndpointOptions options) | ||
{ | ||
ArgumentNullException.ThrowIfNull(options); | ||
|
||
base.Configure(options); | ||
|
||
if (options.Capacity < 1) | ||
{ | ||
options.Capacity = DefaultCapacity; | ||
} | ||
|
||
foreach (string defaultKey in DefaultAllowedRequestHeaders) | ||
{ | ||
options.RequestHeaders.Add(defaultKey); | ||
} | ||
|
||
foreach (string defaultKey in DefaultAllowedResponseHeaders) | ||
{ | ||
options.ResponseHeaders.Add(defaultKey); | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/Management/src/Endpoint/Actuators/HttpExchanges/EndpointServiceCollectionExtensions.cs
This file contains hidden or 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,37 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the Apache 2.0 License. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
using Steeltoe.Management.Diagnostics; | ||
|
||
namespace Steeltoe.Management.Endpoint.Actuators.HttpExchanges; | ||
|
||
public static class EndpointServiceCollectionExtensions | ||
{ | ||
/// <summary> | ||
/// Adds the HttpExchanges actuator to the service container. | ||
/// </summary> | ||
/// <param name="services"> | ||
/// The <see cref="IServiceCollection" /> to add services to. | ||
/// </param> | ||
/// <returns> | ||
/// The incoming <paramref name="services" /> so that additional calls can be chained. | ||
/// </returns> | ||
public static IServiceCollection AddHttpExchangesActuator(this IServiceCollection services) | ||
{ | ||
ArgumentNullException.ThrowIfNull(services); | ||
|
||
services.TryAddSingleton<IDiagnosticsManager, DiagnosticsManager>(); | ||
services.AddHostedService<DiagnosticsService>(); | ||
services.AddCommonActuatorServices(); | ||
services.AddHttpExchangesActuatorServices(); | ||
services.TryAddEnumerable(ServiceDescriptor.Singleton<IDiagnosticObserver, HttpExchangesDiagnosticObserver>()); | ||
|
||
services.TryAddSingleton<IHttpExchangesRepository>(provider => | ||
provider.GetServices<IDiagnosticObserver>().OfType<HttpExchangesDiagnosticObserver>().Single()); | ||
|
||
return services; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/Management/src/Endpoint/Actuators/HttpExchanges/HttpExchange.cs
This file contains hidden or 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,37 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the Apache 2.0 License. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Text.Json.Serialization; | ||
using System.Xml; | ||
|
||
namespace Steeltoe.Management.Endpoint.Actuators.HttpExchanges; | ||
|
||
public sealed class HttpExchange | ||
{ | ||
[JsonPropertyName("timeTaken")] | ||
public string SerializedTimeTaken => XmlConvert.ToString(TimeTaken); | ||
|
||
public DateTime Timestamp { get; } | ||
bart-vmware marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public HttpExchangePrincipal? Principal { get; } | ||
public HttpExchangeSession? Session { get; } | ||
public HttpExchangeRequest Request { get; } | ||
public HttpExchangeResponse Response { get; } | ||
|
||
[JsonIgnore] | ||
public TimeSpan TimeTaken { get; } | ||
|
||
public HttpExchange(HttpExchangeRequest request, HttpExchangeResponse response, DateTime timestamp, HttpExchangePrincipal? principal, | ||
HttpExchangeSession? session, TimeSpan timeTaken) | ||
{ | ||
ArgumentNullException.ThrowIfNull(request); | ||
ArgumentNullException.ThrowIfNull(response); | ||
|
||
Request = request; | ||
Response = response; | ||
Timestamp = timestamp; | ||
Principal = principal; | ||
Session = session; | ||
TimeTaken = timeTaken; | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.