Skip to content
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

Startup diagnostic serialization fails when diagnostic entries include System.Type properties #6829

Merged
merged 1 commit into from
Sep 1, 2023
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"TypeIndicator":{"SomeType":"NServiceBus.Core.Tests.OpenTelemetry.DiagnosticsWriterTests"}}
19 changes: 19 additions & 0 deletions src/NServiceBus.Core.Tests/OpenTelemetry/DiagnosticsWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,24 @@ public async Task ShouldWriteWhenDuplicateEntriesPresent()

Approver.Verify(output);
}

[Test]
public async Task ShouldWriteEntriesWithTypesUsingTheFullName()
{
var output = string.Empty;
var testWriter = new Func<string, CancellationToken, Task>((diagnosticOutput, _) =>
{
output = diagnosticOutput;
return Task.CompletedTask;
});
var diagnostics = new StartupDiagnosticEntries();
diagnostics.Add("TypeIndicator", new { SomeType = typeof(DiagnosticsWriterTests) });

var writer = new HostStartupDiagnosticsWriter(testWriter, true);

await writer.Write(diagnostics.entries);

Approver.Verify(output);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
using Logging;
Expand All @@ -27,7 +28,7 @@ public async Task Write(List<StartupDiagnosticEntries.StartupDiagnosticEntry> en

try
{
data = JsonSerializer.Serialize(dictionary);
data = JsonSerializer.Serialize(dictionary, diagnosticsOptions);
}
catch (Exception exception)
{
Expand Down Expand Up @@ -80,6 +81,26 @@ await diagnosticsWriter(data, cancellationToken)
readonly Func<string, CancellationToken, Task> diagnosticsWriter;
readonly bool isCustomWriter;

static readonly JsonSerializerOptions diagnosticsOptions = new()
{
Converters = { new TypeConverter() }
};

/// <summary>
/// By default System.Text.Json would throw with "Serialization and deserialization of 'System.Type' instances are not supported" which normally
/// would make sense because it can be considered unsafe to serialize and deserialize types. We add a custom converter here to make
/// sure when diagnostics entries accidentally use types it will just print the full name as a string. We never intent to read these things
/// back so this is a safe approach.
/// </summary>
sealed class TypeConverter : JsonConverter<Type>
{
// we never need to deserialize
public override Type Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => throw new NotImplementedException();

public override void Write(Utf8JsonWriter writer, Type value, JsonSerializerOptions options) => writer.WriteStringValue(value.FullName);
}


static readonly ILog logger = LogManager.GetLogger<HostStartupDiagnosticsWriter>();
}
}