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

perf(storage): Use ToListAsync() instead of ToArrayAsync() #36

Merged
merged 1 commit into from
Jan 29, 2024
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
8 changes: 4 additions & 4 deletions src/Mocha.Core/Storage/Jaeger/IJaegerSpanReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ namespace Mocha.Core.Storage.Jaeger;

public interface IJaegerSpanReader
{
Task<string[]> GetServicesAsync();
Task<IEnumerable<string>> GetServicesAsync();

Task<string[]> GetOperationsAsync(string serviceName);
Task<IEnumerable<string>> GetOperationsAsync(string serviceName);

Task<JaegerTrace[]> FindTracesAsync(JaegerTraceQueryParameters query);
Task<IEnumerable<JaegerTrace>> FindTracesAsync(JaegerTraceQueryParameters query);

Task<JaegerTrace[]> FindTracesAsync(string[] traceIDs, ulong? startTimeMinUnixNano = null, ulong? startTimeMaxUnixNano = null);
Task<IEnumerable<JaegerTrace>> FindTracesAsync(string[] traceIDs, ulong? startTimeMinUnixNano = null, ulong? startTimeMaxUnixNano = null);
}
18 changes: 9 additions & 9 deletions src/Mocha.Query.Jaeger/Controllers/TraceController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ namespace Mocha.Query.Jaeger.Controllers
public class TraceController(IJaegerSpanReader spanReader) : Controller
{
[HttpGet("services")]
public async Task<JaegerResponse<string[]>> GetSeries()
public async Task<JaegerResponse<IEnumerable<string>>> GetSeries()
{
return new(await spanReader.GetServicesAsync());
}

[HttpGet("services/{serviceName}/operations")]
public async Task<JaegerResponse<string[]>> GetOperations(string serviceName)
public async Task<JaegerResponse<IEnumerable<string>>> GetOperations(string serviceName)
{
return new(await spanReader.GetOperationsAsync(serviceName));
}

[HttpGet("traces")]
public async Task<JaegerResponse<JaegerTrace[]>> FindTraces([FromQuery] FindTracesRequest request)
public async Task<JaegerResponse<IEnumerable<JaegerTrace>>> FindTraces([FromQuery] FindTracesRequest request)
{
static ulong? ParseAsNanoseconds(string? input)
{
Expand Down Expand Up @@ -71,7 +71,7 @@ public async Task<JaegerResponse<JaegerTrace[]>> FindTraces([FromQuery] FindTrac
startTimeMax = now;
}

JaegerTrace[] traces;
IEnumerable<JaegerTrace> traces;

if (request.TraceID?.Any() ?? false)
{
Expand Down Expand Up @@ -99,26 +99,26 @@ public async Task<JaegerResponse<JaegerTrace[]>> FindTraces([FromQuery] FindTrac
}

JaegerResponseError? error = null;
if (traces.Length == 0)
if (traces.Any() is false)
{
error = new JaegerResponseError { Code = (int)HttpStatusCode.NotFound, Message = "trace not found" };
}

return new JaegerResponse<JaegerTrace[]>(traces) { Error = error };
return new JaegerResponse<IEnumerable<JaegerTrace>>(traces) { Error = error };
}

[HttpGet("traces/{traceID}")]
public async Task<JaegerResponse<JaegerTrace[]>> GetTrace(string traceID)
public async Task<JaegerResponse<IEnumerable<JaegerTrace>>> GetTrace(string traceID)
{
var traces = await spanReader.FindTracesAsync([traceID]);

JaegerResponseError? error = null;
if (traces.Length == 0)
if (traces.Any() is false)
{
error = new JaegerResponseError { Code = (int)HttpStatusCode.NotFound, Message = "trace not found" };
}

return new JaegerResponse<JaegerTrace[]>(traces) { Error = error };
return new JaegerResponse<IEnumerable<JaegerTrace>>(traces) { Error = error };
}
}
}
24 changes: 12 additions & 12 deletions src/Mocha.Storage/EntityFrameworkCore/Jaeger/EFJaegerSpanReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@ namespace Mocha.Storage.EntityFrameworkCore.Jaeger;

internal class EFJaegerSpanReader(IDbContextFactory<MochaContext> contextFactory) : IJaegerSpanReader
{
public async Task<string[]> GetServicesAsync()
public async Task<IEnumerable<string>> GetServicesAsync()
{
await using var context = await contextFactory.CreateDbContextAsync();
var services = await context.Spans.Select(s => s.ServiceName).Distinct().ToArrayAsync();
var services = await context.Spans.Select(s => s.ServiceName).Distinct().ToListAsync();
return services;
}

public async Task<string[]> GetOperationsAsync(string serviceName)
public async Task<IEnumerable<string>> GetOperationsAsync(string serviceName)
{
await using var context = await contextFactory.CreateDbContextAsync();
var operations = await context.Spans
.Where(s => s.ServiceName == serviceName)
.Select(s => s.SpanName)
.Distinct()
.ToArrayAsync();
.ToListAsync();
return operations;
}

public async Task<JaegerTrace[]> FindTracesAsync(JaegerTraceQueryParameters query)
public async Task<IEnumerable<JaegerTrace>> FindTracesAsync(JaegerTraceQueryParameters query)
{
await using var context = await contextFactory.CreateDbContextAsync();
var queryableSpans = context.Spans.AsQueryable();
Expand Down Expand Up @@ -92,7 +92,7 @@ join spanId in spanIds on span.SpanId equals spanId
return await QueryJaegerTracesAsync(queryableSpans, context);
}

public async Task<JaegerTrace[]> FindTracesAsync(
public async Task<IEnumerable<JaegerTrace>> FindTracesAsync(
string[]? traceIDs,
ulong? startTimeUnixNano,
ulong? endTimeUnixNano)
Expand All @@ -119,29 +119,29 @@ public async Task<JaegerTrace[]> FindTracesAsync(
return await QueryJaegerTracesAsync(queryableSpans, context);
}

private static async Task<JaegerTrace[]> QueryJaegerTracesAsync(
private static async Task<IEnumerable<JaegerTrace>> QueryJaegerTracesAsync(
IQueryable<EFSpan> queryableSpans,
MochaContext context)
{
var spans = await queryableSpans.ToArrayAsync();
var spans = await queryableSpans.ToListAsync();

var spanIds = spans.Select(s => s.SpanId).ToArray();

var spanAttributes = await context.SpanAttributes
.Where(a => spanIds.Contains(a.SpanId))
.ToArrayAsync();
.ToListAsync();

var resourceAttributes = await context.ResourceAttributes
.Where(a => spanIds.Contains(a.SpanId))
.ToArrayAsync();
.ToListAsync();

var spanEvents = await context.SpanEvents
.Where(e => spanIds.Contains(e.SpanId))
.ToArrayAsync();
.ToListAsync();

var spanEventAttributes = await context.SpanEventAttributes
.Where(a => spanIds.Contains(a.SpanId))
.ToArrayAsync();
.ToListAsync();

var jaegerTraces = spans.ToJaegerTraces(
spanAttributes, resourceAttributes, spanEvents, spanEventAttributes).ToArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ public async Task FindTracesAsync_TraceID()
now.AddMinutes(-2).ToUnixTimeNanoseconds(),
now.AddMinutes(2).ToUnixTimeNanoseconds());
Assert.Single(traces);
Assert.Equal("TraceId1", traces[0].TraceID);
Assert.Equal("TraceId1", traces.First().TraceID);
}

public void Dispose()
Expand Down
Loading