RDOSRequestCounter 1.0.5
See the version list below for details.
dotnet add package RDOSRequestCounter --version 1.0.5
NuGet\Install-Package RDOSRequestCounter -Version 1.0.5
<PackageReference Include="RDOSRequestCounter" Version="1.0.5" />
<PackageVersion Include="RDOSRequestCounter" Version="1.0.5" />
<PackageReference Include="RDOSRequestCounter" />
paket add RDOSRequestCounter --version 1.0.5
#r "nuget: RDOSRequestCounter, 1.0.5"
#:package RDOSRequestCounter@1.0.5
#addin nuget:?package=RDOSRequestCounter&version=1.0.5
#tool nuget:?package=RDOSRequestCounter&version=1.0.5
RDOS.RequestCounter
Library .NET 5.0 để đếm request và lưu trữ vào PostgreSQL một cách bất đồng bộ, không làm chậm middleware pipeline.
Tính năng chính
- ✅ Non-blocking Middleware: Middleware không đợi database insert
- ✅ Async Background Processing: Xử lý request data trong background service
- ✅ PostgreSQL Storage: Lưu trữ vào PostgreSQL với Entity Framework Core
- ✅ Batch Processing: Insert nhiều records cùng lúc để tối ưu performance
- ✅ Channel-based Queue: Sử dụng System.Threading.Channels cho async processing
- ✅ Configurable: Nhiều tùy chọn cấu hình linh hoạt
- ✅ Health Checks: Tích hợp health checks cho database
- ✅ Error Handling: Retry logic và error logging
- ✅ Metrics Endpoint: API để xem real-time metrics
Cài đặt
1. Thêm Package Reference
<PackageReference Include="RDOS.RequestCounter" Version="1.0.0" />
Lưu ý: Library được tối ưu cho .NET 5.0 và tương thích với .NET 5.0+.
2. Cấu hình Connection String
Trong appsettings.json
:
{
"ConnectionStrings": {
"RequestCounter": "Host=localhost;Database=requestdb;Username=your_user;Password=your_password"
}
}
3. Đăng ký Services
Trong Program.cs
:
using RDOS.RequestCounter.Extensions;
using RDOS.RequestCounter.Configuration;
var builder = WebApplication.CreateBuilder(args);
// Thiết lập connection string
RequestCounterConfig.SetConnectionString("Host=localhost;Database=requestdb;Username=your_user;Password=your_password");
// Thêm RequestCounter
builder.Services.AddRequestCounter();
var app = builder.Build();
// Sử dụng middleware
app.UseRequestCounter();
// Đảm bảo database được tạo
await app.EnsureRequestCounterDatabaseAsync();
app.Run();
Cấu hình nâng cao
Cách 1: Sử dụng static configuration
// Thiết lập connection string
RequestCounterConfig.SetConnectionString("your_connection_string");
// Cấu hình default options
RequestCounterConfig.ConfigureDefaultOptions(options =>
{
options.ChannelCapacity = 10000; // Kích thước buffer
options.BatchSize = 100; // Số records per batch
options.BatchTimeout = TimeSpan.FromSeconds(5); // Timeout cho batch
options.EnableResponseTimeTracking = true; // Theo dõi response time
options.EnableIpTracking = true; // Theo dõi IP address
options.EnableUserAgentTracking = true; // Theo dõi User Agent
options.IgnoredPaths = new[] { "/health", "/metrics" }; // Paths bỏ qua
options.MaxRetryAttempts = 3; // Số lần retry
});
// Đăng ký services
builder.Services.AddRequestCounter();
Cách 2: Cấu hình trực tiếp
builder.Services.AddRequestCounter(options =>
{
options.ConnectionString = "your_connection_string";
options.ChannelCapacity = 10000;
options.BatchSize = 100;
options.BatchTimeout = TimeSpan.FromSeconds(5);
options.EnableResponseTimeTracking = true;
options.EnableIpTracking = true;
options.EnableUserAgentTracking = true;
options.IgnoredPaths = new[] { "/health", "/metrics" };
options.MaxRetryAttempts = 3;
});
Database Schema
Library sẽ tự động tạo bảng monitoring_request_metrics
với các cột:
id
: Primary keytimestamp
: Thời gian requestmethod
: HTTP method (GET, POST, etc.)path
: Request pathstatus_code
: HTTP status coderesponse_time_ms
: Thời gian response (milliseconds)ip_address
: IP address của clientuser_agent
: User agent stringcontent_length
: Content lengthquery_string
: Query string (nếu enable)referer
: Referer header (nếu enable)created_at
: Thời gian tạo record
Metrics API
GET /metrics/request-counter
Xem real-time metrics với các query parameters:
from
: Thời gian bắt đầu (ISO 8601)to
: Thời gian kết thúc (ISO 8601)method
: Filter theo HTTP methodpath
: Filter theo path pattern
Ví dụ:
GET /metrics/request-counter?from=2024-01-01T00:00:00Z&method=GET
Response:
{
"totalRequests": 1500,
"averageResponseTime": 125.5,
"statusCodes": [
{ "statusCode": 200, "count": 1200 },
{ "statusCode": 404, "count": 200 },
{ "statusCode": 500, "count": 100 }
],
"methods": [
{ "method": "GET", "count": 800 },
{ "method": "POST", "count": 700 }
],
"period": {
"from": "2024-01-01T00:00:00Z",
"to": null
}
}
Performance Considerations
Tối ưu Database
- Indexes: Library tự động tạo indexes cho các cột thường query
- Batch Processing: Insert nhiều records cùng lúc
- Connection Pooling: Sử dụng EF Core connection pooling
- Async Operations: Tất cả database operations đều async
Tối ưu Application
- Channel Capacity: Điều chỉnh
ChannelCapacity
phù hợp với load - Batch Size: Tăng
BatchSize
cho high-throughput applications - Ignored Paths: Bỏ qua các path không cần thiết (health checks, etc.)
Health Checks
Library tự động đăng ký health check cho database:
// Kiểm tra health
app.MapHealthChecks("/health");
Error Handling
- Retry Logic: Tự động retry khi database insert thất bại
- Circuit Breaker: Tránh cascade failures
- Logging: Log errors với configurable level
- Graceful Degradation: Application vẫn hoạt động khi database down
Monitoring
Logs
Library sử dụng structured logging với các log levels:
Information
: Service start/stopDebug
: Batch processing detailsWarning
: Retry attemptsError
: Database failures
Metrics
Sử dụng metrics endpoint để monitor:
- Request volume
- Response times
- Error rates
- Status code distribution
Troubleshooting
Database Connection Issues
- Kiểm tra connection string
- Đảm bảo PostgreSQL đang chạy
- Kiểm tra network connectivity
- Verify user permissions
Performance Issues
- Tăng
BatchSize
vàChannelCapacity
- Optimize database indexes
- Monitor memory usage
- Check background service logs
Missing Data
- Kiểm tra
IgnoredPaths
configuration - Verify database permissions
- Check background service status
- Review error logs
License
MIT License
Support
Để được hỗ trợ, vui lòng tạo issue trên GitHub repository.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net5.0
- Microsoft.AspNetCore.Hosting.Abstractions (>= 2.2.0)
- Microsoft.AspNetCore.Http.Abstractions (>= 2.2.0)
- Microsoft.AspNetCore.Mvc.Core (>= 2.2.5)
- Microsoft.EntityFrameworkCore (>= 5.0.9)
- Microsoft.EntityFrameworkCore.Design (>= 5.0.9)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 5.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 5.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 5.0.0)
- Microsoft.Extensions.Options (>= 5.0.0)
- Npgsql.EntityFrameworkCore.PostgreSQL (>= 5.0.5.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.