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

[release/9.0] Fix error adding log entry with no body #6762

Open
wants to merge 2 commits into
base: release/9.0
Choose a base branch
from
Open
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
56 changes: 56 additions & 0 deletions playground/Stress/Stress.ApiService/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
using System.Diagnostics;
using System.Text;
using System.Threading.Channels;
using Grpc.Core;
using Grpc.Net.Client;
using Microsoft.AspNetCore.Mvc;
using OpenTelemetry.Proto.Collector.Logs.V1;
using OpenTelemetry.Proto.Common.V1;
using OpenTelemetry.Proto.Logs.V1;
using OpenTelemetry.Proto.Resource.V1;
using Stress.ApiService;

var builder = WebApplication.CreateBuilder(args);
Expand Down Expand Up @@ -225,4 +231,54 @@ async IAsyncEnumerable<string> WriteOutput()
return $"Created duplicate span IDs.";
});

app.MapGet("/log-entry-nobody", async (IConfiguration configuration) =>
{
var channel = GrpcChannel.ForAddress(configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]!);

var resource = new Resource();
resource.Attributes.Add(new KeyValue { Key = "service.name", Value = new AnyValue { StringValue = configuration["OTEL_SERVICE_NAME"]! } });

foreach (var item in configuration["OTEL_RESOURCE_ATTRIBUTES"]!.Split(';'))
{
var headerParts = item.Split('=');

resource.Attributes.Add(new KeyValue { Key = headerParts[0], Value = new AnyValue { StringValue = headerParts[1] } });
}

var metadata = new Metadata();
foreach (var item in configuration["OTEL_EXPORTER_OTLP_HEADERS"]!.Split(';'))
{
var headerParts = item.Split('=');

metadata.Add(headerParts[0], headerParts[1]);
}

var client = new LogsService.LogsServiceClient(channel);
var response = await client.ExportAsync(new ExportLogsServiceRequest
{
ResourceLogs =
{
new ResourceLogs
{
Resource = resource,
ScopeLogs =
{
new ScopeLogs
{
Scope = new InstrumentationScope { Name = "NoBodySource" },
LogRecords =
{
new LogRecord
{
}
}
}
}
}
}
}, metadata);

return response.PartialSuccess?.RejectedLogRecords > 0 ? "Failure" : "Success";
});

app.Run();
7 changes: 7 additions & 0 deletions playground/Stress/Stress.ApiService/Stress.ApiService.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@
<ProjectReference Include="..\..\Playground.ServiceDefaults\Playground.ServiceDefaults.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Grpc.Tools" />
<Protobuf Include="..\..\..\src\Aspire.Dashboard\Otlp\**\*.proto">
<ProtoRoot>..\..\..\src\Aspire.Dashboard\Otlp</ProtoRoot>
</Protobuf>
</ItemGroup>

</Project>
4 changes: 3 additions & 1 deletion src/Aspire.Dashboard/Otlp/Model/OtlpLogEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ public OtlpLogEntry(LogRecord record, OtlpApplicationView logApp, OtlpScope scop
Flags = record.Flags;
Severity = MapSeverity(record.SeverityNumber);

Message = OtlpHelpers.TruncateString(record.Body.GetString(), context.Options.MaxAttributeLength);
Message = record.Body is { } body
? OtlpHelpers.TruncateString(body.GetString(), context.Options.MaxAttributeLength)
: string.Empty;
OriginalFormat = originalFormat;
SpanId = record.SpanId.ToHexString();
TraceId = record.TraceId.ToHexString();
Expand Down
41 changes: 41 additions & 0 deletions tests/Aspire.Dashboard.Tests/TelemetryRepositoryTests/LogTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,47 @@ public void AddLogs()
s => Assert.Equal("Log", s));
}

[Fact]
public void AddLogs_NoBody_EmptyMessage()
{
// Arrange
var repository = CreateRepository();

// Act
var addContext = new AddContext();
repository.AddLogs(addContext, new RepeatedField<ResourceLogs>()
{
new ResourceLogs
{
Resource = CreateResource(),
ScopeLogs =
{
new ScopeLogs
{
Scope = CreateScope("TestLogger"),
LogRecords = { CreateLogRecord(skipBody: true) }
}
}
}
});

// Assert
Assert.Equal(0, addContext.FailureCount);

var logs = repository.GetLogs(new GetLogsContext
{
ApplicationKey = null,
StartIndex = 0,
Count = 10,
Filters = []
});
Assert.Collection(logs.Items,
app =>
{
Assert.Equal("", app.Message);
});
}

[Fact]
public void AddLogs_MultipleOutOfOrder()
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Shared/Telemetry/TelemetryTestHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,13 @@ public static Span CreateSpan(string traceId, string spanId, DateTime startTime,
return span;
}

public static LogRecord CreateLogRecord(DateTime? time = null, string? message = null, SeverityNumber? severity = null, IEnumerable<KeyValuePair<string, string>>? attributes = null)
public static LogRecord CreateLogRecord(DateTime? time = null, string? message = null, SeverityNumber? severity = null, IEnumerable<KeyValuePair<string, string>>? attributes = null, bool? skipBody = null)
{
attributes ??= [new KeyValuePair<string, string>("{OriginalFormat}", "Test {Log}"), new KeyValuePair<string, string>("Log", "Value!")];

var logRecord = new LogRecord
{
Body = new AnyValue { StringValue = message ?? "Test Value!" },
Body = (skipBody ?? false) ? null : new AnyValue { StringValue = message ?? "Test Value!" },
TraceId = ByteString.CopyFrom(Convert.FromHexString("5465737454726163654964")),
SpanId = ByteString.CopyFrom(Convert.FromHexString("546573745370616e4964")),
TimeUnixNano = time != null ? DateTimeToUnixNanoseconds(time.Value) : 1000,
Expand Down