-
Notifications
You must be signed in to change notification settings - Fork 394
Diagnostic Summary Setup #2021
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
Diagnostic Summary Setup #2021
Changes from all commits
Commits
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
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
41 changes: 41 additions & 0 deletions
41
.../IdentityServer/Licensing/V2/Diagnostics/DiagnosticEntries/AssemblyInfoDiagnosticEntry.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,41 @@ | ||
| // Copyright (c) Duende Software. All rights reserved. | ||
| // See LICENSE in the project root for license information. | ||
|
|
||
| using System.Reflection; | ||
| using System.Runtime.Loader; | ||
| using System.Text.Json; | ||
|
|
||
| namespace Duende.IdentityServer.Licensing.V2.Diagnostics.DiagnosticEntries; | ||
|
|
||
| internal class AssemblyInfoDiagnosticEntry : IDiagnosticEntry | ||
| { | ||
| public Task WriteAsync(Utf8JsonWriter writer) | ||
| { | ||
| var assemblies = GetAssemblyInfo(); | ||
| writer.WriteStartObject("AssemblyInfo"); | ||
| writer.WriteNumber("AssemblyCount", assemblies.Count); | ||
|
|
||
| writer.WriteStartArray("Assemblies"); | ||
| foreach (var assembly in assemblies) | ||
| { | ||
| writer.WriteStartObject(); | ||
| writer.WriteString("Name", assembly.GetName().Name); | ||
| writer.WriteString("Version", assembly.GetName().Version?.ToString() ?? "Unknown"); | ||
| writer.WriteEndObject(); | ||
| } | ||
|
|
||
| writer.WriteEndArray(); | ||
| writer.WriteEndObject(); | ||
|
|
||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| private List<Assembly> GetAssemblyInfo() | ||
| { | ||
| var assemblies = AssemblyLoadContext.Default.Assemblies | ||
| .OrderBy(a => a.FullName) | ||
| .ToList(); | ||
|
|
||
| return assemblies; | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
identity-server/src/IdentityServer/Licensing/V2/Diagnostics/DiagnosticHostedService.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,28 @@ | ||
| // Copyright (c) Duende Software. All rights reserved. | ||
| // See LICENSE in the project root for license information. | ||
|
|
||
| using Duende.IdentityServer.Configuration; | ||
| using Microsoft.Extensions.Hosting; | ||
| using Microsoft.Extensions.Logging; | ||
| using Microsoft.Extensions.Options; | ||
|
|
||
| namespace Duende.IdentityServer.Licensing.V2.Diagnostics; | ||
|
|
||
| internal class DiagnosticHostedService(DiagnosticSummary diagnosticSummary, IOptions<IdentityServerOptions> options, ILogger<DiagnosticHostedService> logger) : BackgroundService | ||
| { | ||
| protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
| { | ||
| using var timer = new PeriodicTimer(options.Value.DiagnosticSummaryLogFrequency); | ||
| while (!stoppingToken.IsCancellationRequested && await timer.WaitForNextTickAsync(stoppingToken)) | ||
| { | ||
| try | ||
| { | ||
| await diagnosticSummary.PrintSummary(); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| logger.LogError(ex, "An error occurred while logging the diagnostic summary: {Message}", ex.Message); | ||
| } | ||
| } | ||
| } | ||
| } | ||
31 changes: 31 additions & 0 deletions
31
identity-server/src/IdentityServer/Licensing/V2/Diagnostics/DiagnosticSummary.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,31 @@ | ||
| // Copyright (c) Duende Software. All rights reserved. | ||
| // See LICENSE in the project root for license information. | ||
|
|
||
| using System.Buffers; | ||
| using System.Text; | ||
| using System.Text.Json; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace Duende.IdentityServer.Licensing.V2.Diagnostics; | ||
|
|
||
| internal class DiagnosticSummary(IEnumerable<IDiagnosticEntry> entries, ILogger<DiagnosticSummary> logger) | ||
| { | ||
| public async Task PrintSummary() | ||
| { | ||
| var bufferWriter = new ArrayBufferWriter<byte>(); | ||
| await using var writer = new Utf8JsonWriter(bufferWriter, new JsonWriterOptions { Indented = false }); | ||
|
|
||
| writer.WriteStartObject(); | ||
|
|
||
| foreach (var diagnosticEntry in entries) | ||
| { | ||
| await diagnosticEntry.WriteAsync(writer); | ||
| } | ||
|
|
||
| writer.WriteEndObject(); | ||
|
|
||
| await writer.FlushAsync(); | ||
|
|
||
| logger.LogInformation("{Message}", Encoding.UTF8.GetString(bufferWriter.WrittenSpan)); | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
identity-server/src/IdentityServer/Licensing/V2/Diagnostics/IDiagnosticEntry.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,11 @@ | ||
| // Copyright (c) Duende Software. All rights reserved. | ||
| // See LICENSE in the project root for license information. | ||
|
|
||
| using System.Text.Json; | ||
|
|
||
| namespace Duende.IdentityServer.Licensing.V2.Diagnostics; | ||
|
|
||
| internal interface IDiagnosticEntry | ||
| { | ||
| Task WriteAsync(Utf8JsonWriter writer); | ||
| } |
35 changes: 35 additions & 0 deletions
35
...entityServer.UnitTests/Licensing/v2/DiagnosticEntries/AssemblyInfoDiagnosticEntryTests.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,35 @@ | ||
| // Copyright (c) Duende Software. All rights reserved. | ||
| // See LICENSE in the project root for license information. | ||
|
|
||
| using System.Buffers; | ||
| using System.Text; | ||
| using System.Text.Json; | ||
| using Duende.IdentityServer.Licensing.V2.Diagnostics.DiagnosticEntries; | ||
|
|
||
| namespace IdentityServer.UnitTests.Licensing.V2.DiagnosticEntries; | ||
|
|
||
| public class AssemblyInfoDiagnosticEntryTests | ||
| { | ||
| [Fact] | ||
| public async Task WriteAsync_ShouldWriteAssemblyInfo() | ||
| { | ||
| var subject = new AssemblyInfoDiagnosticEntry(); | ||
| var bufferWriter = new ArrayBufferWriter<byte>(); | ||
| await using var writer = new Utf8JsonWriter(bufferWriter, new JsonWriterOptions { Indented = false }); | ||
| writer.WriteStartObject(); | ||
|
|
||
| await subject.WriteAsync(writer); | ||
|
|
||
| writer.WriteEndObject(); | ||
| await writer.FlushAsync(); | ||
| var json = Encoding.UTF8.GetString(bufferWriter.WrittenSpan); | ||
| var jsonDocument = JsonDocument.Parse(json); | ||
| var assemblyInfo = jsonDocument.RootElement.GetProperty("AssemblyInfo"); | ||
| assemblyInfo.GetProperty("AssemblyCount").ValueKind.ShouldBe(JsonValueKind.Number); | ||
| var assemblies = assemblyInfo.GetProperty("Assemblies"); | ||
| assemblies.ValueKind.ShouldBe(JsonValueKind.Array); | ||
| var firstEntry = assemblies.EnumerateArray().First(); | ||
| firstEntry.GetProperty("Name").ValueKind.ShouldBe(JsonValueKind.String); | ||
| firstEntry.GetProperty("Version").ValueKind.ShouldBe(JsonValueKind.String); | ||
| } | ||
| } |
43 changes: 43 additions & 0 deletions
43
identity-server/test/IdentityServer.UnitTests/Licensing/v2/DiagnosticSummaryTests.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,43 @@ | ||
| // Copyright (c) Duende Software. All rights reserved. | ||
| // See LICENSE in the project root for license information. | ||
|
|
||
| using System.Text.Json; | ||
| using Duende.IdentityServer.Licensing.V2.Diagnostics; | ||
| using Microsoft.Extensions.Logging.Abstractions; | ||
|
|
||
| namespace IdentityServer.UnitTests.Licensing.V2; | ||
|
|
||
| public class DiagnosticSummaryTests | ||
| { | ||
| [Fact] | ||
| public async Task PrintSummary_ShouldCallWriteAsyncOnEveryDiagnosticEntry() | ||
| { | ||
| var fakeLogger = new NullLogger<DiagnosticSummary>(); | ||
| var firstDiagnosticEntry = new TestDiagnosticEntry(); | ||
| var secondDiagnosticEntry = new TestDiagnosticEntry(); | ||
| var thirdDiagnosticEntry = new TestDiagnosticEntry(); | ||
| var entries = new List<IDiagnosticEntry> | ||
| { | ||
| firstDiagnosticEntry, | ||
| secondDiagnosticEntry, | ||
| thirdDiagnosticEntry | ||
| }; | ||
| var summary = new DiagnosticSummary(entries, fakeLogger); | ||
|
|
||
| await summary.PrintSummary(); | ||
|
|
||
| firstDiagnosticEntry.WasCalled.ShouldBeTrue(); | ||
| secondDiagnosticEntry.WasCalled.ShouldBeTrue(); | ||
| thirdDiagnosticEntry.WasCalled.ShouldBeTrue(); | ||
| } | ||
|
|
||
| private class TestDiagnosticEntry : IDiagnosticEntry | ||
| { | ||
| public bool WasCalled { get; private set; } | ||
| public Task WriteAsync(Utf8JsonWriter writer) | ||
| { | ||
| WasCalled = true; | ||
| return Task.CompletedTask; | ||
| } | ||
| } | ||
| } |
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.