Skip to content

Commit

Permalink
Support types in diagnostic entries (#6829)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmarbach authored Sep 1, 2023
1 parent f4b65d0 commit 267c25a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
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>();
}
}

0 comments on commit 267c25a

Please sign in to comment.