Skip to content
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
12 changes: 9 additions & 3 deletions src/ObservaNet.Agent/ObservaNetClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ public ObservaNetClient(HttpClient httpClient)
_httpClient = httpClient;
}

public async Task SendLogAsync(LogIngestRequest request)
public async Task SendLogAsync(LogIngestRequest request, CancellationToken cancellationToken = default)
{
var response = await _httpClient.PostAsJsonAsync("/api/logs", request);
var response = await _httpClient.PostAsJsonAsync("/api/logs", request, cancellationToken);
response.EnsureSuccessStatusCode();
}
}

public async Task SendLogsAsync(IEnumerable<LogIngestRequest> requests, CancellationToken cancellationToken = default)
{
var response = await _httpClient.PostAsJsonAsync("/api/logs/batch", requests, cancellationToken);
response.EnsureSuccessStatusCode();
}
}
40 changes: 22 additions & 18 deletions src/ObservaNet.Api/Alerting/AlertService.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.Extensions.Configuration;
using ObservaNet.Api.Services;
using ObservaNet.Shared.Models;

Expand All @@ -7,68 +8,71 @@ public sealed class AlertService
{
private readonly ILogService _logService;
private readonly ILogger<AlertService> _logger;
private readonly IConfiguration _config;

// Regras com limiares configuráveis
private static readonly AlertRule HighErrorRate = new()
{
Name = "high-error-rate",
Description = "Taxa de erros (Error + Critical) acima de 20% no total do dia",
Description = "Taxa de erros (Error + Critical) acima do limiar configurado no dia",
Severity = AlertSeverity.Critical,
};

private static readonly AlertRule SlowRequestsDetected = new()
{
Name = "slow-requests-detected",
Description = "Presença de logs de Warning com prefixo SLOW REQUEST",
Description = "Requisições lentas (SLOW REQUEST) detectadas hoje",
Severity = AlertSeverity.Warning,
};

public AlertService(ILogService logService, ILogger<AlertService> logger)
public AlertService(ILogService logService, ILogger<AlertService> logger, IConfiguration config)
{
_logService = logService;
_logger = logger;
_config = config;
}

public async Task<IReadOnlyList<AlertFired>> EvaluateAsync()
{
var fired = new List<AlertFired>();
var fired = new List<AlertFired>();
var metrics = await _logService.GetMetricsAsync();

// ── Regra 1: taxa de erros ─────────────────────────────────────────────
var errors = metrics.ByLevel.GetValueOrDefault(nameof(ObservaLogLevel.Error), 0)
+ metrics.ByLevel.GetValueOrDefault(nameof(ObservaLogLevel.Critical), 0);
var total = metrics.ByLevel.Values.Sum();
var errorRateThreshold = _config.GetValue<double>("Alerting:ErrorRateThreshold", 0.20);
var errors = metrics.ByLevel.GetValueOrDefault(nameof(ObservaLogLevel.Error), 0)
+ metrics.ByLevel.GetValueOrDefault(nameof(ObservaLogLevel.Critical), 0);
var total = metrics.ByLevel.Values.Sum();

if (total > 0)
{
var errorRate = errors / (double)total;
if (errorRate >= 0.20)
if (errorRate >= errorRateThreshold)
{
var alert = new AlertFired
{
Rule = HighErrorRate,
Message = $"Taxa de erros é {errorRate:P0} ({errors}/{total} logs hoje).",
Message = $"Taxa de erros é {errorRate:P0} ({errors}/{total} logs hoje). Limiar: {errorRateThreshold:P0}.",
};
fired.Add(alert);
_logger.LogWarning("[ALERT] {Name}: {Message}", alert.Rule.Name, alert.Message);
}
}

// ── Regra 2: chamadas lentas ───────────────────────────────────────────
// ── Regra 2: chamadas lentas (somente logs com prefixo "SLOW REQUEST") ─
var slowResult = await _logService.QueryLogsAsync(
serviceName: null,
level: ObservaLogLevel.Warning,
from: DateTimeOffset.UtcNow.Date,
to: null,
page: 1,
pageSize: 1);
serviceName: null,
level: ObservaLogLevel.Warning,
from: DateTimeOffset.UtcNow.Date,
to: null,
page: 1,
pageSize: 1,
messageContains: "SLOW REQUEST");

if (slowResult.TotalCount > 0)
{
var alert = new AlertFired
{
Rule = SlowRequestsDetected,
Message = $"{slowResult.TotalCount} Warning(s) registrados hoje — verifique chamadas lentas.",
Message = $"{slowResult.TotalCount} requisição(ões) lenta(s) registrada(s) hoje.",
};
fired.Add(alert);
_logger.LogWarning("[ALERT] {Name}: {Message}", alert.Rule.Name, alert.Message);
Expand Down
16 changes: 15 additions & 1 deletion src/ObservaNet.Api/Endpoints/LogEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,21 @@ public static void MapLogEndpoints(this WebApplication app)

await logService.IngestLogAsync(request);
return Results.Created("/api/logs", null);
});
}).RequireRateLimiting("ingest");

app.MapPost("/api/logs/batch", async (List<LogIngestRequest> requests, ILogService logService) =>
{
var list = requests;
if (list.Count == 0)
return Results.BadRequest(new { error = "A lista de logs não pode ser vazia." });
if (list.Count > 500)
return Results.BadRequest(new { error = "Máximo de 500 logs por lote." });
if (list.Any(r => string.IsNullOrWhiteSpace(r.Message) || string.IsNullOrWhiteSpace(r.ServiceName)))
return Results.BadRequest(new { error = "Todos os logs devem ter Message e ServiceName." });

await logService.IngestBatchAsync(list);
return Results.Created("/api/logs/batch", new { count = list.Count });
}).RequireRateLimiting("ingest");

app.MapGet("/api/logs", async (ILogService logService, string? serviceName, ObservaLogLevel? level, DateTimeOffset? from, DateTimeOffset? to, int page = 1, int pageSize = 20) =>
{
Expand Down
13 changes: 6 additions & 7 deletions src/ObservaNet.Api/Middleware/PerformanceMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ public sealed class PerformanceMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger<PerformanceMiddleware> _logger;
private readonly int _thresholdMs;

// Limiar (ms) acima do qual o request é considerado lento
private const int SlowRequestThresholdMs = 500;

public PerformanceMiddleware(RequestDelegate next, ILogger<PerformanceMiddleware> logger)
public PerformanceMiddleware(RequestDelegate next, ILogger<PerformanceMiddleware> logger, IConfiguration config)
{
_next = next;
_logger = logger;
_next = next;
_logger = logger;
_thresholdMs = config.GetValue<int>("Performance:SlowRequestThresholdMs", 500);
}

public async Task InvokeAsync(HttpContext context)
Expand All @@ -31,7 +30,7 @@ public async Task InvokeAsync(HttpContext context)
var path = context.Request.Path;
var statusCode = context.Response.StatusCode;

if (ms >= SlowRequestThresholdMs)
if (ms >= _thresholdMs)
{
_logger.LogWarning(
"SLOW REQUEST {Method} {Path} responded {StatusCode} in {DurationMs} ms",
Expand Down
1 change: 1 addition & 0 deletions src/ObservaNet.Api/ObservaNet.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<PackageReference Include="Elastic.Clients.Elasticsearch" Version="8.17.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="10.0.7" />
<PackageReference Include="Scalar.AspNetCore" Version="2.5.0" />
<PackageReference Include="Serilog.AspNetCore" Version="10.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.1.1" />
</ItemGroup>
Expand Down
24 changes: 24 additions & 0 deletions src/ObservaNet.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using Elastic.Clients.Elasticsearch;
using Microsoft.AspNetCore.RateLimiting;
using Microsoft.EntityFrameworkCore;
using ObservaNet.Api.Alerting;
using ObservaNet.Api.Data;
using ObservaNet.Api.Endpoints;
using ObservaNet.Api.Middleware;
using ObservaNet.Api.Services;
using Scalar.AspNetCore;
using Serilog;
using System.Threading.RateLimiting;

Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
Expand All @@ -30,6 +33,21 @@
});
});

// ── Rate Limiting ────────────────────────────────────────────────────────────
var ingestPerMinute = builder.Configuration.GetValue<int>("RateLimit:IngestPerMinute", 300);
builder.Services.AddRateLimiter(options =>
{
options.AddFixedWindowLimiter("ingest", o =>
{
o.Window = TimeSpan.FromMinutes(1);
o.PermitLimit = ingestPerMinute;
o.QueueProcessingOrder = QueueProcessingOrder.OldestFirst;
o.QueueLimit = 0;
});
options.RejectionStatusCode = StatusCodes.Status429TooManyRequests;
});

// ── Storage backend ──────────────────────────────────────────────────────────
var esUri = builder.Configuration["Elasticsearch:Uri"];
if (!string.IsNullOrEmpty(esUri))
{
Expand Down Expand Up @@ -59,9 +77,15 @@
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
app.MapScalarApiReference(options =>
{
options.Title = "ObservaNet API";
options.Theme = ScalarTheme.DeepSpace;
});
}

app.UseCors();
app.UseRateLimiter();
app.UsePerformanceTracking();

if (!app.Environment.IsDevelopment())
Expand Down
26 changes: 21 additions & 5 deletions src/ObservaNet.Api/Services/EfLogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,31 @@ public async Task IngestLogAsync(LogIngestRequest request)
await _context.SaveChangesAsync();
}

public async Task IngestBatchAsync(IEnumerable<LogIngestRequest> requests)
{
var entries = requests.Select(r => new LogEntry
{
Id = Guid.NewGuid(),
ServiceName = r.ServiceName,
Message = r.Message,
Level = r.Level,
Exception = r.Exception,
Properties = r.Properties,
Timestamp = DateTimeOffset.UtcNow
});
_context.Logs.AddRange(entries);
await _context.SaveChangesAsync();
}

public async Task<LogQueryResponse> QueryLogsAsync(
string? serviceName, ObservaLogLevel? level,
DateTimeOffset? from, DateTimeOffset? to,
int page, int pageSize)
int page, int pageSize, string? messageContains = null)
{
var query = _context.Logs.AsNoTracking().AsQueryable();

if (!string.IsNullOrEmpty(serviceName))
{
var lowerName = serviceName.ToLower();
query = query.Where(l => l.ServiceName.ToLower() == lowerName);
}
query = query.Where(l => l.ServiceName.ToLower() == serviceName.ToLower());

if (level.HasValue)
query = query.Where(l => l.Level == level.Value);
Expand All @@ -51,6 +64,9 @@ public async Task<LogQueryResponse> QueryLogsAsync(
if (to.HasValue)
query = query.Where(l => l.Timestamp <= to.Value);

if (!string.IsNullOrEmpty(messageContains))
query = query.Where(l => l.Message.Contains(messageContains));

var totalCount = await query.CountAsync();
var logs = await query
.OrderByDescending(l => l.Timestamp)
Expand Down
39 changes: 32 additions & 7 deletions src/ObservaNet.Api/Services/ElasticsearchLogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,35 @@
.Id(entry.Id.ToString()));
}

public async Task IngestBatchAsync(IEnumerable<LogIngestRequest> requests)
{
var index = DailyIndex();
var entries = requests.Select(r => new LogEntry
{
Id = Guid.NewGuid(),
ServiceName = r.ServiceName,
Message = r.Message,
Level = r.Level,
Exception = r.Exception,
Properties = r.Properties,
Timestamp = DateTimeOffset.UtcNow,
}).ToList();

await _client.BulkAsync(b => b
.Index(index)
.IndexMany(entries, (op, e) => op.Id(e.Id.ToString())));
}

public async Task<LogQueryResponse> QueryLogsAsync(
string? serviceName, ObservaLogLevel? level,
DateTimeOffset? from, DateTimeOffset? to,
int page, int pageSize)
int page, int pageSize, string? messageContains = null)
{
// Clonar variáveis para captura segura nos lambdas
var svcFilter = serviceName;
var lvlFilter = level.HasValue ? (int?)((int)level.Value) : null;
var fromFilter = from;
var toFilter = to;
var msgFilter = messageContains;

var musts = new List<Action<QueryDescriptor<LogEntry>>>();

Expand All @@ -55,8 +74,8 @@

if (lvlFilter.HasValue)
{
var lvl = lvlFilter.Value;
musts.Add(m => m.Term(t => t.Field(f => f.Level).Value(lvl)));
var lvlStr = ((ObservaLogLevel)lvlFilter.Value).ToString();
musts.Add(m => m.Term(t => t.Field("level.keyword").Value(lvlStr)));

Check warning on line 78 in src/ObservaNet.Api/Services/ElasticsearchLogService.cs

View workflow job for this annotation

GitHub Actions / .NET Build & Test

Possible null reference argument for parameter 'field' in 'TermQueryDescriptor<LogEntry> TermQueryDescriptor<LogEntry>.Field(Field field)'.

Check warning on line 78 in src/ObservaNet.Api/Services/ElasticsearchLogService.cs

View workflow job for this annotation

GitHub Actions / .NET Build & Test

Possible null reference argument for parameter 'field' in 'TermQueryDescriptor<LogEntry> TermQueryDescriptor<LogEntry>.Field(Field field)'.
}

if (fromFilter.HasValue || toFilter.HasValue)
Expand All @@ -71,6 +90,12 @@
})));
}

if (!string.IsNullOrEmpty(msgFilter))
{
var msg = msgFilter;
musts.Add(m => m.Match(mt => mt.Field(f => f.Message).Query(msg)));
}

var response = await _client.SearchAsync<LogEntry>(s =>
{
s.Index(IndexPattern)
Expand Down Expand Up @@ -102,7 +127,7 @@
.Size(0)
.Aggregations(aggs =>
{
aggs.Add("by_level", a => a.Terms(t => t.Field(f => f.Level).Size(10)));
aggs.Add("by_level", a => a.Terms(t => t.Field("level.keyword").Size(10)));
aggs.Add("by_service", a => a.Terms(t => t.Field("serviceName.keyword").Size(100)));
aggs.Add("today", a => a.Filter(q => q.Range(r => r.DateRange(dr => dr
.Field(f => f.Timestamp)
Expand All @@ -112,10 +137,10 @@
);

var byLevel = new Dictionary<string, int>();
var levelBuckets = response.Aggregations?.GetLongTerms("by_level")?.Buckets;
var levelBuckets = response.Aggregations?.GetStringTerms("by_level")?.Buckets;
if (levelBuckets is not null)
foreach (var b in levelBuckets)
byLevel[((ObservaLogLevel)(int)b.Key).ToString()] = (int)b.DocCount;
byLevel[b.Key.ToString()!] = (int)b.DocCount;

var byService = new Dictionary<string, int>();
var serviceBuckets = response.Aggregations?.GetStringTerms("by_service")?.Buckets;
Expand Down
3 changes: 2 additions & 1 deletion src/ObservaNet.Api/Services/ILogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ namespace ObservaNet.Api.Services
public interface ILogService
{
Task IngestLogAsync(LogIngestRequest request);
Task<LogQueryResponse> QueryLogsAsync(string? serviceName, ObservaLogLevel? level, DateTimeOffset? from, DateTimeOffset? to, int page, int pageSize);
Task IngestBatchAsync(IEnumerable<LogIngestRequest> requests);
Task<LogQueryResponse> QueryLogsAsync(string? serviceName, ObservaLogLevel? level, DateTimeOffset? from, DateTimeOffset? to, int page, int pageSize, string? messageContains = null);
Task<LogMetrics> GetMetricsAsync();
}
}
Loading
Loading