Skip to content

Commit f9fed94

Browse files
authored
Merge pull request #2252 from DuendeSoftware/jmdc/diagnostics
Add service for diagnostic data
2 parents d126cbf + e897432 commit f9fed94

File tree

6 files changed

+346
-30
lines changed

6 files changed

+346
-30
lines changed

identity-server/src/IdentityServer/Configuration/DependencyInjection/BuilderExtensions/Core.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,10 @@ public static IIdentityServerBuilder AddCoreServices(this IIdentityServerBuilder
236236
builder.Services.AddSingleton<IDiagnosticEntry, ClientInfoDiagnosticEntry>();
237237
builder.Services.AddSingleton<ResourceLoadedTracker>();
238238
builder.Services.AddSingleton<IDiagnosticEntry, ResourceInfoDiagnosticEntry>();
239-
builder.Services.AddSingleton(serviceProvider => new DiagnosticSummary(
239+
builder.Services.AddSingleton<DiagnosticSummary>();
240+
builder.Services.AddSingleton(serviceProvider => new DiagnosticDataService(
240241
DateTime.UtcNow,
241-
serviceProvider.GetServices<IDiagnosticEntry>(),
242-
serviceProvider.GetRequiredService<IdentityServerOptions>(),
243-
serviceProvider.GetRequiredService<ILoggerFactory>()));
242+
serviceProvider.GetServices<IDiagnosticEntry>()));
244243
builder.Services.AddHostedService<DiagnosticHostedService>();
245244

246245
return builder;
Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,21 @@
11
// Copyright (c) Duende Software. All rights reserved.
22
// See LICENSE in the project root for license information.
33

4-
using System.Buffers;
54
using System.Text;
6-
using System.Text.Json;
75
using Duende.IdentityServer.Configuration;
6+
using Duende.IdentityServer.Services;
87
using Microsoft.Extensions.Logging;
98

109
namespace Duende.IdentityServer.Licensing.V2.Diagnostics;
1110

12-
internal class DiagnosticSummary(DateTime serverStartTime, IEnumerable<IDiagnosticEntry> entries, IdentityServerOptions options, ILoggerFactory loggerFactory)
11+
internal class DiagnosticSummary(DiagnosticDataService diagnosticDataService, IdentityServerOptions options, ILoggerFactory loggerFactory)
1312
{
1413
private readonly ILogger _logger = loggerFactory.CreateLogger("Duende.IdentityServer.Diagnostics.Summary");
1514

1615
public async Task PrintSummary()
1716
{
18-
var bufferWriter = new ArrayBufferWriter<byte>();
19-
await using var writer = new Utf8JsonWriter(bufferWriter, new JsonWriterOptions { Indented = false });
20-
21-
writer.WriteStartObject();
22-
23-
var diagnosticContext = new DiagnosticContext(serverStartTime, DateTime.UtcNow);
24-
foreach (var diagnosticEntry in entries)
25-
{
26-
await diagnosticEntry.WriteAsync(diagnosticContext, writer);
27-
}
28-
29-
writer.WriteEndObject();
30-
31-
await writer.FlushAsync();
32-
33-
var span = bufferWriter.WrittenSpan;
17+
var jsonMemory = await diagnosticDataService.GetJsonBytesAsync();
18+
var span = jsonMemory.Span;
3419

3520
using var diagnosticActivity = Tracing.DiagnosticsActivitySource.StartActivity("DiagnosticSummary");
3621
var chunkSize = options.Diagnostics.ChunkSize;
@@ -47,7 +32,7 @@ public async Task PrintSummary()
4732
}
4833
else
4934
{
50-
_logger.DiagnosticSummaryLogged(1, 1, Encoding.UTF8.GetString(bufferWriter.WrittenSpan));
35+
_logger.DiagnosticSummaryLogged(1, 1, Encoding.UTF8.GetString(span));
5136
}
5237
}
5338
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) Duende Software. All rights reserved.
2+
// See LICENSE in the project root for license information.
3+
4+
5+
#nullable enable
6+
7+
using System.Buffers;
8+
using System.Text;
9+
using System.Text.Json;
10+
using Duende.IdentityServer.Licensing.V2.Diagnostics;
11+
12+
namespace Duende.IdentityServer.Services;
13+
14+
public class DiagnosticDataService
15+
{
16+
private readonly DateTime _serverStartTime;
17+
private readonly IEnumerable<IDiagnosticEntry> _entries;
18+
19+
internal DiagnosticDataService(DateTime serverStartTime, IEnumerable<IDiagnosticEntry> entries)
20+
{
21+
_serverStartTime = serverStartTime;
22+
_entries = entries;
23+
}
24+
25+
public async Task<ReadOnlyMemory<byte>> GetJsonBytesAsync()
26+
{
27+
var bufferWriter = new ArrayBufferWriter<byte>();
28+
await using var writer = new Utf8JsonWriter(bufferWriter, new JsonWriterOptions { Indented = false });
29+
30+
writer.WriteStartObject();
31+
32+
var diagnosticContext = new DiagnosticContext(_serverStartTime, DateTime.UtcNow);
33+
foreach (var diagnosticEntry in _entries)
34+
{
35+
await diagnosticEntry.WriteAsync(diagnosticContext, writer);
36+
}
37+
38+
writer.WriteEndObject();
39+
40+
await writer.FlushAsync();
41+
42+
return bufferWriter.WrittenMemory;
43+
}
44+
45+
public async Task<string> GetJsonStringAsync()
46+
{
47+
var bytes = await GetJsonBytesAsync();
48+
return Encoding.UTF8.GetString(bytes.Span);
49+
}
50+
}

identity-server/test/IdentityServer.UnitTests/Licensing/v2/DiagnosticHostedServiceTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Text.Json;
55
using Duende.IdentityServer.Configuration;
66
using Duende.IdentityServer.Licensing.V2.Diagnostics;
7+
using Duende.IdentityServer.Services;
78
using Microsoft.Extensions.Logging.Abstractions;
89
using Microsoft.Extensions.Options;
910

@@ -24,7 +25,8 @@ public async Task ExecuteAsync_ShouldNotThrowOperationCancelledException()
2425
secondDiagnosticEntry,
2526
thirdDiagnosticEntry
2627
};
27-
var diagnosticSummary = new DiagnosticSummary(DateTime.UtcNow, entries, new IdentityServerOptions(), new StubLoggerFactory(diagnosticSummaryLogger));
28+
var diagnosticService = new DiagnosticDataService(DateTime.UtcNow, entries);
29+
var diagnosticSummary = new DiagnosticSummary(diagnosticService, new IdentityServerOptions(), new StubLoggerFactory(diagnosticSummaryLogger));
2830

2931
var options = Options.Create(new IdentityServerOptions());
3032
var logger = new NullLogger<DiagnosticHostedService>();

identity-server/test/IdentityServer.UnitTests/Licensing/v2/DiagnosticSummaryTests.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Duende.IdentityServer.Configuration;
66
using Duende.IdentityServer.Events;
77
using Duende.IdentityServer.Licensing.V2.Diagnostics;
8+
using Duende.IdentityServer.Services;
89
using Microsoft.Extensions.Logging.Abstractions;
910
using Microsoft.Extensions.Logging.Testing;
1011

@@ -25,7 +26,8 @@ public async Task PrintSummary_ShouldCallWriteAsyncOnEveryDiagnosticEntry()
2526
secondDiagnosticEntry,
2627
thirdDiagnosticEntry
2728
};
28-
var summary = new DiagnosticSummary(DateTime.UtcNow, entries, new IdentityServerOptions(), new StubLoggerFactory(logger));
29+
var diagnosticService = new DiagnosticDataService(DateTime.UtcNow, entries);
30+
var summary = new DiagnosticSummary(diagnosticService, new IdentityServerOptions(), new StubLoggerFactory(logger));
2931

3032
await summary.PrintSummary();
3133

@@ -42,7 +44,8 @@ public async Task PrintSummary_ShouldChunkLargeOutput()
4244

4345
var logger = new FakeLogger<DiagnosticSummary>();
4446
var diagnosticEntry = new LongDiagnosticEntry { OutputLength = chunkSize * 2 };
45-
var summary = new DiagnosticSummary(DateTime.UtcNow, [diagnosticEntry], options, new StubLoggerFactory(logger));
47+
var diagnosticService = new DiagnosticDataService(DateTime.UtcNow, [diagnosticEntry]);
48+
var summary = new DiagnosticSummary(diagnosticService, options, new StubLoggerFactory(logger));
4649

4750
await summary.PrintSummary();
4851

@@ -61,7 +64,9 @@ public async Task PrintSummary_ShouldChunkLargeOutputOfMultibyteCharacters()
6164

6265
var logger = new FakeLogger<DiagnosticSummary>();
6366
var diagnosticEntry = new LongDiagnosticEntry { OutputLength = 2, OutputCharacter = '€' };
64-
var summary = new DiagnosticSummary(DateTime.UtcNow, [diagnosticEntry], options, new StubLoggerFactory(logger));
67+
var diagnosticService = new DiagnosticDataService(DateTime.UtcNow, [diagnosticEntry]);
68+
var summary = new DiagnosticSummary(diagnosticService, options, new StubLoggerFactory(logger));
69+
6570

6671
await summary.PrintSummary();
6772

@@ -76,7 +81,9 @@ public async Task PrintSummary_ShouldCreateChunksWithMaxSizeEightKB()
7681

7782
var logger = new FakeLogger<DiagnosticSummary>();
7883
var diagnosticEntry = new LongDiagnosticEntry { OutputLength = options.Diagnostics.ChunkSize * 2 };
79-
var summary = new DiagnosticSummary(DateTime.UtcNow, [diagnosticEntry], options, new StubLoggerFactory(logger));
84+
var diagnosticService = new DiagnosticDataService(DateTime.UtcNow, [diagnosticEntry]);
85+
var summary = new DiagnosticSummary(diagnosticService, options, new StubLoggerFactory(logger));
86+
8087

8188
await summary.PrintSummary();
8289
foreach (var entry in logger.Collector.GetSnapshot())
@@ -91,7 +98,8 @@ public async Task PrintSummary_ShouldIncludeLogEventId()
9198
var options = new IdentityServerOptions();
9299
var logger = new FakeLogger<DiagnosticSummary>();
93100
var diagnosticEntry = new LongDiagnosticEntry { OutputLength = 100000 };
94-
var summary = new DiagnosticSummary(DateTime.UtcNow, [diagnosticEntry], options, new StubLoggerFactory(logger));
101+
var diagnosticService = new DiagnosticDataService(DateTime.UtcNow, [diagnosticEntry]);
102+
var summary = new DiagnosticSummary(diagnosticService, options, new StubLoggerFactory(logger));
95103

96104
await summary.PrintSummary();
97105

0 commit comments

Comments
 (0)