init
This commit is contained in:
484
.gitignore
vendored
Normal file
484
.gitignore
vendored
Normal file
@ -0,0 +1,484 @@
|
|||||||
|
## Ignore Visual Studio temporary files, build results, and
|
||||||
|
## files generated by popular Visual Studio add-ons.
|
||||||
|
##
|
||||||
|
## Get latest from `dotnet new gitignore`
|
||||||
|
|
||||||
|
# dotenv files
|
||||||
|
.env
|
||||||
|
|
||||||
|
# User-specific files
|
||||||
|
*.rsuser
|
||||||
|
*.suo
|
||||||
|
*.user
|
||||||
|
*.userosscache
|
||||||
|
*.sln.docstates
|
||||||
|
|
||||||
|
# User-specific files (MonoDevelop/Xamarin Studio)
|
||||||
|
*.userprefs
|
||||||
|
|
||||||
|
# Mono auto generated files
|
||||||
|
mono_crash.*
|
||||||
|
|
||||||
|
# Build results
|
||||||
|
[Dd]ebug/
|
||||||
|
[Dd]ebugPublic/
|
||||||
|
[Rr]elease/
|
||||||
|
[Rr]eleases/
|
||||||
|
x64/
|
||||||
|
x86/
|
||||||
|
[Ww][Ii][Nn]32/
|
||||||
|
[Aa][Rr][Mm]/
|
||||||
|
[Aa][Rr][Mm]64/
|
||||||
|
bld/
|
||||||
|
[Bb]in/
|
||||||
|
[Oo]bj/
|
||||||
|
[Ll]og/
|
||||||
|
[Ll]ogs/
|
||||||
|
|
||||||
|
# Visual Studio 2015/2017 cache/options directory
|
||||||
|
.vs/
|
||||||
|
# Uncomment if you have tasks that create the project's static files in wwwroot
|
||||||
|
#wwwroot/
|
||||||
|
|
||||||
|
# Visual Studio 2017 auto generated files
|
||||||
|
Generated\ Files/
|
||||||
|
|
||||||
|
# MSTest test Results
|
||||||
|
[Tt]est[Rr]esult*/
|
||||||
|
[Bb]uild[Ll]og.*
|
||||||
|
|
||||||
|
# NUnit
|
||||||
|
*.VisualState.xml
|
||||||
|
TestResult.xml
|
||||||
|
nunit-*.xml
|
||||||
|
|
||||||
|
# Build Results of an ATL Project
|
||||||
|
[Dd]ebugPS/
|
||||||
|
[Rr]eleasePS/
|
||||||
|
dlldata.c
|
||||||
|
|
||||||
|
# Benchmark Results
|
||||||
|
BenchmarkDotNet.Artifacts/
|
||||||
|
|
||||||
|
# .NET
|
||||||
|
project.lock.json
|
||||||
|
project.fragment.lock.json
|
||||||
|
artifacts/
|
||||||
|
|
||||||
|
# Tye
|
||||||
|
.tye/
|
||||||
|
|
||||||
|
# ASP.NET Scaffolding
|
||||||
|
ScaffoldingReadMe.txt
|
||||||
|
|
||||||
|
# StyleCop
|
||||||
|
StyleCopReport.xml
|
||||||
|
|
||||||
|
# Files built by Visual Studio
|
||||||
|
*_i.c
|
||||||
|
*_p.c
|
||||||
|
*_h.h
|
||||||
|
*.ilk
|
||||||
|
*.meta
|
||||||
|
*.obj
|
||||||
|
*.iobj
|
||||||
|
*.pch
|
||||||
|
*.pdb
|
||||||
|
*.ipdb
|
||||||
|
*.pgc
|
||||||
|
*.pgd
|
||||||
|
*.rsp
|
||||||
|
*.sbr
|
||||||
|
*.tlb
|
||||||
|
*.tli
|
||||||
|
*.tlh
|
||||||
|
*.tmp
|
||||||
|
*.tmp_proj
|
||||||
|
*_wpftmp.csproj
|
||||||
|
*.log
|
||||||
|
*.tlog
|
||||||
|
*.vspscc
|
||||||
|
*.vssscc
|
||||||
|
.builds
|
||||||
|
*.pidb
|
||||||
|
*.svclog
|
||||||
|
*.scc
|
||||||
|
|
||||||
|
# Chutzpah Test files
|
||||||
|
_Chutzpah*
|
||||||
|
|
||||||
|
# Visual C++ cache files
|
||||||
|
ipch/
|
||||||
|
*.aps
|
||||||
|
*.ncb
|
||||||
|
*.opendb
|
||||||
|
*.opensdf
|
||||||
|
*.sdf
|
||||||
|
*.cachefile
|
||||||
|
*.VC.db
|
||||||
|
*.VC.VC.opendb
|
||||||
|
|
||||||
|
# Visual Studio profiler
|
||||||
|
*.psess
|
||||||
|
*.vsp
|
||||||
|
*.vspx
|
||||||
|
*.sap
|
||||||
|
|
||||||
|
# Visual Studio Trace Files
|
||||||
|
*.e2e
|
||||||
|
|
||||||
|
# TFS 2012 Local Workspace
|
||||||
|
$tf/
|
||||||
|
|
||||||
|
# Guidance Automation Toolkit
|
||||||
|
*.gpState
|
||||||
|
|
||||||
|
# ReSharper is a .NET coding add-in
|
||||||
|
_ReSharper*/
|
||||||
|
*.[Rr]e[Ss]harper
|
||||||
|
*.DotSettings.user
|
||||||
|
|
||||||
|
# TeamCity is a build add-in
|
||||||
|
_TeamCity*
|
||||||
|
|
||||||
|
# DotCover is a Code Coverage Tool
|
||||||
|
*.dotCover
|
||||||
|
|
||||||
|
# AxoCover is a Code Coverage Tool
|
||||||
|
.axoCover/*
|
||||||
|
!.axoCover/settings.json
|
||||||
|
|
||||||
|
# Coverlet is a free, cross platform Code Coverage Tool
|
||||||
|
coverage*.json
|
||||||
|
coverage*.xml
|
||||||
|
coverage*.info
|
||||||
|
|
||||||
|
# Visual Studio code coverage results
|
||||||
|
*.coverage
|
||||||
|
*.coveragexml
|
||||||
|
|
||||||
|
# NCrunch
|
||||||
|
_NCrunch_*
|
||||||
|
.*crunch*.local.xml
|
||||||
|
nCrunchTemp_*
|
||||||
|
|
||||||
|
# MightyMoose
|
||||||
|
*.mm.*
|
||||||
|
AutoTest.Net/
|
||||||
|
|
||||||
|
# Web workbench (sass)
|
||||||
|
.sass-cache/
|
||||||
|
|
||||||
|
# Installshield output folder
|
||||||
|
[Ee]xpress/
|
||||||
|
|
||||||
|
# DocProject is a documentation generator add-in
|
||||||
|
DocProject/buildhelp/
|
||||||
|
DocProject/Help/*.HxT
|
||||||
|
DocProject/Help/*.HxC
|
||||||
|
DocProject/Help/*.hhc
|
||||||
|
DocProject/Help/*.hhk
|
||||||
|
DocProject/Help/*.hhp
|
||||||
|
DocProject/Help/Html2
|
||||||
|
DocProject/Help/html
|
||||||
|
|
||||||
|
# Click-Once directory
|
||||||
|
publish/
|
||||||
|
|
||||||
|
# Publish Web Output
|
||||||
|
*.[Pp]ublish.xml
|
||||||
|
*.azurePubxml
|
||||||
|
# Note: Comment the next line if you want to checkin your web deploy settings,
|
||||||
|
# but database connection strings (with potential passwords) will be unencrypted
|
||||||
|
*.pubxml
|
||||||
|
*.publishproj
|
||||||
|
|
||||||
|
# Microsoft Azure Web App publish settings. Comment the next line if you want to
|
||||||
|
# checkin your Azure Web App publish settings, but sensitive information contained
|
||||||
|
# in these scripts will be unencrypted
|
||||||
|
PublishScripts/
|
||||||
|
|
||||||
|
# NuGet Packages
|
||||||
|
*.nupkg
|
||||||
|
# NuGet Symbol Packages
|
||||||
|
*.snupkg
|
||||||
|
# The packages folder can be ignored because of Package Restore
|
||||||
|
**/[Pp]ackages/*
|
||||||
|
# except build/, which is used as an MSBuild target.
|
||||||
|
!**/[Pp]ackages/build/
|
||||||
|
# Uncomment if necessary however generally it will be regenerated when needed
|
||||||
|
#!**/[Pp]ackages/repositories.config
|
||||||
|
# NuGet v3's project.json files produces more ignorable files
|
||||||
|
*.nuget.props
|
||||||
|
*.nuget.targets
|
||||||
|
|
||||||
|
# Microsoft Azure Build Output
|
||||||
|
csx/
|
||||||
|
*.build.csdef
|
||||||
|
|
||||||
|
# Microsoft Azure Emulator
|
||||||
|
ecf/
|
||||||
|
rcf/
|
||||||
|
|
||||||
|
# Windows Store app package directories and files
|
||||||
|
AppPackages/
|
||||||
|
BundleArtifacts/
|
||||||
|
Package.StoreAssociation.xml
|
||||||
|
_pkginfo.txt
|
||||||
|
*.appx
|
||||||
|
*.appxbundle
|
||||||
|
*.appxupload
|
||||||
|
|
||||||
|
# Visual Studio cache files
|
||||||
|
# files ending in .cache can be ignored
|
||||||
|
*.[Cc]ache
|
||||||
|
# but keep track of directories ending in .cache
|
||||||
|
!?*.[Cc]ache/
|
||||||
|
|
||||||
|
# Others
|
||||||
|
ClientBin/
|
||||||
|
~$*
|
||||||
|
*~
|
||||||
|
*.dbmdl
|
||||||
|
*.dbproj.schemaview
|
||||||
|
*.jfm
|
||||||
|
*.pfx
|
||||||
|
*.publishsettings
|
||||||
|
orleans.codegen.cs
|
||||||
|
|
||||||
|
# Including strong name files can present a security risk
|
||||||
|
# (https://github.com/github/gitignore/pull/2483#issue-259490424)
|
||||||
|
#*.snk
|
||||||
|
|
||||||
|
# Since there are multiple workflows, uncomment next line to ignore bower_components
|
||||||
|
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
|
||||||
|
#bower_components/
|
||||||
|
|
||||||
|
# RIA/Silverlight projects
|
||||||
|
Generated_Code/
|
||||||
|
|
||||||
|
# Backup & report files from converting an old project file
|
||||||
|
# to a newer Visual Studio version. Backup files are not needed,
|
||||||
|
# because we have git ;-)
|
||||||
|
_UpgradeReport_Files/
|
||||||
|
Backup*/
|
||||||
|
UpgradeLog*.XML
|
||||||
|
UpgradeLog*.htm
|
||||||
|
ServiceFabricBackup/
|
||||||
|
*.rptproj.bak
|
||||||
|
|
||||||
|
# SQL Server files
|
||||||
|
*.mdf
|
||||||
|
*.ldf
|
||||||
|
*.ndf
|
||||||
|
|
||||||
|
# Business Intelligence projects
|
||||||
|
*.rdl.data
|
||||||
|
*.bim.layout
|
||||||
|
*.bim_*.settings
|
||||||
|
*.rptproj.rsuser
|
||||||
|
*- [Bb]ackup.rdl
|
||||||
|
*- [Bb]ackup ([0-9]).rdl
|
||||||
|
*- [Bb]ackup ([0-9][0-9]).rdl
|
||||||
|
|
||||||
|
# Microsoft Fakes
|
||||||
|
FakesAssemblies/
|
||||||
|
|
||||||
|
# GhostDoc plugin setting file
|
||||||
|
*.GhostDoc.xml
|
||||||
|
|
||||||
|
# Node.js Tools for Visual Studio
|
||||||
|
.ntvs_analysis.dat
|
||||||
|
node_modules/
|
||||||
|
|
||||||
|
# Visual Studio 6 build log
|
||||||
|
*.plg
|
||||||
|
|
||||||
|
# Visual Studio 6 workspace options file
|
||||||
|
*.opt
|
||||||
|
|
||||||
|
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
|
||||||
|
*.vbw
|
||||||
|
|
||||||
|
# Visual Studio 6 auto-generated project file (contains which files were open etc.)
|
||||||
|
*.vbp
|
||||||
|
|
||||||
|
# Visual Studio 6 workspace and project file (working project files containing files to include in project)
|
||||||
|
*.dsw
|
||||||
|
*.dsp
|
||||||
|
|
||||||
|
# Visual Studio 6 technical files
|
||||||
|
*.ncb
|
||||||
|
*.aps
|
||||||
|
|
||||||
|
# Visual Studio LightSwitch build output
|
||||||
|
**/*.HTMLClient/GeneratedArtifacts
|
||||||
|
**/*.DesktopClient/GeneratedArtifacts
|
||||||
|
**/*.DesktopClient/ModelManifest.xml
|
||||||
|
**/*.Server/GeneratedArtifacts
|
||||||
|
**/*.Server/ModelManifest.xml
|
||||||
|
_Pvt_Extensions
|
||||||
|
|
||||||
|
# Paket dependency manager
|
||||||
|
.paket/paket.exe
|
||||||
|
paket-files/
|
||||||
|
|
||||||
|
# FAKE - F# Make
|
||||||
|
.fake/
|
||||||
|
|
||||||
|
# CodeRush personal settings
|
||||||
|
.cr/personal
|
||||||
|
|
||||||
|
# Python Tools for Visual Studio (PTVS)
|
||||||
|
__pycache__/
|
||||||
|
*.pyc
|
||||||
|
|
||||||
|
# Cake - Uncomment if you are using it
|
||||||
|
# tools/**
|
||||||
|
# !tools/packages.config
|
||||||
|
|
||||||
|
# Tabs Studio
|
||||||
|
*.tss
|
||||||
|
|
||||||
|
# Telerik's JustMock configuration file
|
||||||
|
*.jmconfig
|
||||||
|
|
||||||
|
# BizTalk build output
|
||||||
|
*.btp.cs
|
||||||
|
*.btm.cs
|
||||||
|
*.odx.cs
|
||||||
|
*.xsd.cs
|
||||||
|
|
||||||
|
# OpenCover UI analysis results
|
||||||
|
OpenCover/
|
||||||
|
|
||||||
|
# Azure Stream Analytics local run output
|
||||||
|
ASALocalRun/
|
||||||
|
|
||||||
|
# MSBuild Binary and Structured Log
|
||||||
|
*.binlog
|
||||||
|
|
||||||
|
# NVidia Nsight GPU debugger configuration file
|
||||||
|
*.nvuser
|
||||||
|
|
||||||
|
# MFractors (Xamarin productivity tool) working folder
|
||||||
|
.mfractor/
|
||||||
|
|
||||||
|
# Local History for Visual Studio
|
||||||
|
.localhistory/
|
||||||
|
|
||||||
|
# Visual Studio History (VSHistory) files
|
||||||
|
.vshistory/
|
||||||
|
|
||||||
|
# BeatPulse healthcheck temp database
|
||||||
|
healthchecksdb
|
||||||
|
|
||||||
|
# Backup folder for Package Reference Convert tool in Visual Studio 2017
|
||||||
|
MigrationBackup/
|
||||||
|
|
||||||
|
# Ionide (cross platform F# VS Code tools) working folder
|
||||||
|
.ionide/
|
||||||
|
|
||||||
|
# Fody - auto-generated XML schema
|
||||||
|
FodyWeavers.xsd
|
||||||
|
|
||||||
|
# VS Code files for those working on multiple tools
|
||||||
|
.vscode/*
|
||||||
|
!.vscode/settings.json
|
||||||
|
!.vscode/tasks.json
|
||||||
|
!.vscode/launch.json
|
||||||
|
!.vscode/extensions.json
|
||||||
|
*.code-workspace
|
||||||
|
|
||||||
|
# Local History for Visual Studio Code
|
||||||
|
.history/
|
||||||
|
|
||||||
|
# Windows Installer files from build outputs
|
||||||
|
*.cab
|
||||||
|
*.msi
|
||||||
|
*.msix
|
||||||
|
*.msm
|
||||||
|
*.msp
|
||||||
|
|
||||||
|
# JetBrains Rider
|
||||||
|
*.sln.iml
|
||||||
|
.idea/
|
||||||
|
|
||||||
|
##
|
||||||
|
## Visual studio for Mac
|
||||||
|
##
|
||||||
|
|
||||||
|
|
||||||
|
# globs
|
||||||
|
Makefile.in
|
||||||
|
*.userprefs
|
||||||
|
*.usertasks
|
||||||
|
config.make
|
||||||
|
config.status
|
||||||
|
aclocal.m4
|
||||||
|
install-sh
|
||||||
|
autom4te.cache/
|
||||||
|
*.tar.gz
|
||||||
|
tarballs/
|
||||||
|
test-results/
|
||||||
|
|
||||||
|
# Mac bundle stuff
|
||||||
|
*.dmg
|
||||||
|
*.app
|
||||||
|
|
||||||
|
# content below from: https://github.com/github/gitignore/blob/main/Global/macOS.gitignore
|
||||||
|
# General
|
||||||
|
.DS_Store
|
||||||
|
.AppleDouble
|
||||||
|
.LSOverride
|
||||||
|
|
||||||
|
# Icon must end with two \r
|
||||||
|
Icon
|
||||||
|
|
||||||
|
|
||||||
|
# Thumbnails
|
||||||
|
._*
|
||||||
|
|
||||||
|
# Files that might appear in the root of a volume
|
||||||
|
.DocumentRevisions-V100
|
||||||
|
.fseventsd
|
||||||
|
.Spotlight-V100
|
||||||
|
.TemporaryItems
|
||||||
|
.Trashes
|
||||||
|
.VolumeIcon.icns
|
||||||
|
.com.apple.timemachine.donotpresent
|
||||||
|
|
||||||
|
# Directories potentially created on remote AFP share
|
||||||
|
.AppleDB
|
||||||
|
.AppleDesktop
|
||||||
|
Network Trash Folder
|
||||||
|
Temporary Items
|
||||||
|
.apdisk
|
||||||
|
|
||||||
|
# content below from: https://github.com/github/gitignore/blob/main/Global/Windows.gitignore
|
||||||
|
# Windows thumbnail cache files
|
||||||
|
Thumbs.db
|
||||||
|
ehthumbs.db
|
||||||
|
ehthumbs_vista.db
|
||||||
|
|
||||||
|
# Dump file
|
||||||
|
*.stackdump
|
||||||
|
|
||||||
|
# Folder config file
|
||||||
|
[Dd]esktop.ini
|
||||||
|
|
||||||
|
# Recycle Bin used on file shares
|
||||||
|
$RECYCLE.BIN/
|
||||||
|
|
||||||
|
# Windows Installer files
|
||||||
|
*.cab
|
||||||
|
*.msi
|
||||||
|
*.msix
|
||||||
|
*.msm
|
||||||
|
*.msp
|
||||||
|
|
||||||
|
# Windows shortcuts
|
||||||
|
*.lnk
|
||||||
|
|
||||||
|
# Vim temporary swap files
|
||||||
|
*.swp
|
||||||
102
Controllers/LogsController.cs
Normal file
102
Controllers/LogsController.cs
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
using GetFromLoki.Models;
|
||||||
|
using GetFromLoki.Services;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace GetFromLoki.Controllers;
|
||||||
|
|
||||||
|
[ApiController]
|
||||||
|
[Route("api/[controller]")]
|
||||||
|
public class LogsController : ControllerBase
|
||||||
|
{
|
||||||
|
private readonly ILokiService _lokiService;
|
||||||
|
|
||||||
|
public LogsController(ILokiService lokiService)
|
||||||
|
{
|
||||||
|
_lokiService = lokiService;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Получить логи по лейблу
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="request">Параметры запроса</param>
|
||||||
|
/// <returns>Список логов с timestamp и message</returns>
|
||||||
|
[HttpPost("query")]
|
||||||
|
public async Task<ActionResult<List<LogEntry>>> GetLogs([FromBody] LogQueryRequest request)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (request.Labels.Count == 0)
|
||||||
|
{
|
||||||
|
return BadRequest("Должен быть указан хотя бы один лейбл");
|
||||||
|
}
|
||||||
|
|
||||||
|
var logs = await _lokiService.GetLogsAsync(request);
|
||||||
|
return Ok(logs);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return StatusCode(500, new { error = ex.Message });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Получить логи по лейблам (GET запрос)
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="labels">Лейблы в формате "key1=value1,key2=value2"</param>
|
||||||
|
/// <param name="startTime">Время начала (опционально)</param>
|
||||||
|
/// <param name="endTime">Время окончания (опционально)</param>
|
||||||
|
/// <param name="limit">Лимит записей (по умолчанию 100)</param>
|
||||||
|
/// <returns>Список логов с timestamp и message</returns>
|
||||||
|
[HttpGet("query")]
|
||||||
|
public async Task<ActionResult<List<LogEntry>>> GetLogs(
|
||||||
|
[FromQuery] string labels,
|
||||||
|
[FromQuery] DateTime? startTime = null,
|
||||||
|
[FromQuery] DateTime? endTime = null,
|
||||||
|
[FromQuery] int? limit = null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(labels))
|
||||||
|
{
|
||||||
|
return BadRequest("Labels обязателен для запроса");
|
||||||
|
}
|
||||||
|
|
||||||
|
var request = new LogQueryRequest
|
||||||
|
{
|
||||||
|
Labels = ParseLabels(labels),
|
||||||
|
StartTime = startTime,
|
||||||
|
EndTime = endTime,
|
||||||
|
Limit = limit
|
||||||
|
};
|
||||||
|
|
||||||
|
if (request.Labels.Count == 0)
|
||||||
|
{
|
||||||
|
return BadRequest("Не удалось распарсить лейблы");
|
||||||
|
}
|
||||||
|
|
||||||
|
var logs = await _lokiService.GetLogsAsync(request);
|
||||||
|
return Ok(logs);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return StatusCode(500, new { error = ex.Message });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Dictionary<string, string> ParseLabels(string labelsString)
|
||||||
|
{
|
||||||
|
var result = new Dictionary<string, string>();
|
||||||
|
var pairs = labelsString.Split(',', StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
|
||||||
|
foreach (var pair in pairs)
|
||||||
|
{
|
||||||
|
var parts = pair.Split('=', 2);
|
||||||
|
if (parts.Length == 2)
|
||||||
|
{
|
||||||
|
result[parts[0].Trim()] = parts[1].Trim();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
14
GetFromLoki.csproj
Normal file
14
GetFromLoki.csproj
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.0" />
|
||||||
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
36
Models/LokiModels.cs
Normal file
36
Models/LokiModels.cs
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
namespace GetFromLoki.Models;
|
||||||
|
|
||||||
|
// Модель для запроса логов
|
||||||
|
public class LogQueryRequest
|
||||||
|
{
|
||||||
|
public Dictionary<string, string> Labels { get; set; } = new();
|
||||||
|
public DateTime? StartTime { get; set; }
|
||||||
|
public DateTime? EndTime { get; set; }
|
||||||
|
public int? Limit { get; set; } = 100;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Модель для ответа с логами
|
||||||
|
public class LogEntry
|
||||||
|
{
|
||||||
|
public DateTime Timestamp { get; set; }
|
||||||
|
public string Message { get; set; } = string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Модель ответа Loki API
|
||||||
|
public class LokiQueryResponse
|
||||||
|
{
|
||||||
|
public string Status { get; set; } = string.Empty;
|
||||||
|
public LokiData Data { get; set; } = new();
|
||||||
|
}
|
||||||
|
|
||||||
|
public class LokiData
|
||||||
|
{
|
||||||
|
public string ResultType { get; set; } = string.Empty;
|
||||||
|
public List<LokiResult> Result { get; set; } = new();
|
||||||
|
}
|
||||||
|
|
||||||
|
public class LokiResult
|
||||||
|
{
|
||||||
|
public Dictionary<string, string> Stream { get; set; } = new();
|
||||||
|
public List<List<string>> Values { get; set; } = new();
|
||||||
|
}
|
||||||
24
Program.cs
Normal file
24
Program.cs
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
using GetFromLoki.Services;
|
||||||
|
|
||||||
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
|
// Add services to the container.
|
||||||
|
builder.Services.AddControllers();
|
||||||
|
builder.Services.AddEndpointsApiExplorer();
|
||||||
|
builder.Services.AddSwaggerGen();
|
||||||
|
|
||||||
|
// Register Loki service
|
||||||
|
builder.Services.AddHttpClient<ILokiService, LokiService>();
|
||||||
|
builder.Services.Configure<LokiOptions>(
|
||||||
|
builder.Configuration.GetSection("Loki"));
|
||||||
|
|
||||||
|
var app = builder.Build();
|
||||||
|
|
||||||
|
// Configure the HTTP request pipeline.
|
||||||
|
app.UseSwagger();
|
||||||
|
app.UseSwaggerUI();
|
||||||
|
|
||||||
|
app.UseAuthorization();
|
||||||
|
app.MapControllers();
|
||||||
|
|
||||||
|
app.Run();
|
||||||
122
README.md
Normal file
122
README.md
Normal file
@ -0,0 +1,122 @@
|
|||||||
|
# Get From Loki
|
||||||
|
|
||||||
|
Компактный REST API сервис на C# для получения логов из Grafana Loki.
|
||||||
|
|
||||||
|
## Описание
|
||||||
|
|
||||||
|
Сервис предоставляет REST API для запроса логов из Grafana Loki по лейблам и возвращает их в формате `{timestamp, message}`.
|
||||||
|
|
||||||
|
## Требования
|
||||||
|
|
||||||
|
- .NET 8.0
|
||||||
|
- Grafana Loki (работающий на http://localhost:3100)
|
||||||
|
|
||||||
|
## Запуск
|
||||||
|
|
||||||
|
1. Убедитесь, что Grafana Loki запущен на http://localhost:3100
|
||||||
|
2. Выполните команды:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
dotnet restore
|
||||||
|
dotnet run --urls http://0.0.0.0:5000
|
||||||
|
```
|
||||||
|
|
||||||
|
Сервис будет доступен по адресу: http://localhost:5000
|
||||||
|
|
||||||
|
## API Endpoints
|
||||||
|
|
||||||
|
### GET /api/logs/query
|
||||||
|
Получить логи по лейблам через GET запрос
|
||||||
|
|
||||||
|
**Параметры:**
|
||||||
|
- `labels` (обязательный) - лейблы в формате "key1=value1,key2=value2"
|
||||||
|
- `startTime` (опциональный) - время начала в формате ISO 8601
|
||||||
|
- `endTime` (опциональный) - время окончания в формате ISO 8601
|
||||||
|
- `limit` (опциональный) - лимит записей (по умолчанию 100)
|
||||||
|
|
||||||
|
**Примеры:**
|
||||||
|
```
|
||||||
|
GET /api/logs/query?labels=job=cron,service_name=system&limit=50
|
||||||
|
GET /api/logs/query?labels=app=myapp,level=error&startTime=2024-01-01T00:00:00Z&endTime=2024-01-01T23:59:59Z
|
||||||
|
```
|
||||||
|
|
||||||
|
### POST /api/logs/query
|
||||||
|
Получить логи по лейблам через POST запрос
|
||||||
|
|
||||||
|
**Тело запроса:**
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"labels": {
|
||||||
|
"job": "cron",
|
||||||
|
"service_name": "system",
|
||||||
|
"level": "info"
|
||||||
|
},
|
||||||
|
"startTime": "2024-01-01T00:00:00Z",
|
||||||
|
"endTime": "2024-01-01T23:59:59Z",
|
||||||
|
"limit": 100
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Примеры использования
|
||||||
|
|
||||||
|
### Простой запрос по одному лейблу:
|
||||||
|
```bash
|
||||||
|
curl "http://localhost:5000/api/logs/query?labels=job=cron&limit=10"
|
||||||
|
```
|
||||||
|
|
||||||
|
### Запрос по нескольким лейблам:
|
||||||
|
```bash
|
||||||
|
curl "http://localhost:5000/api/logs/query?labels=job=cron,service_name=system,level=info&limit=20"
|
||||||
|
```
|
||||||
|
|
||||||
|
### POST запрос с временным окном:
|
||||||
|
```bash
|
||||||
|
curl -X POST "http://localhost:5000/api/logs/query" \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-d '{
|
||||||
|
"labels": {
|
||||||
|
"app": "myapp",
|
||||||
|
"environment": "production"
|
||||||
|
},
|
||||||
|
"startTime": "2024-01-01T00:00:00Z",
|
||||||
|
"endTime": "2024-01-01T01:00:00Z",
|
||||||
|
"limit": 50
|
||||||
|
}'
|
||||||
|
```
|
||||||
|
|
||||||
|
## Ответ
|
||||||
|
|
||||||
|
Сервис возвращает массив объектов в формате:
|
||||||
|
```json
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"timestamp": "2024-01-01T12:00:00Z",
|
||||||
|
"message": "Log message content"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
```
|
||||||
|
|
||||||
|
## Конфигурация
|
||||||
|
|
||||||
|
Настройки подключения к Loki находятся в файле `appsettings.json`:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"Loki": {
|
||||||
|
"BaseUrl": "http://localhost:3100",
|
||||||
|
"QueryTimeout": 30
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## LogQL запросы
|
||||||
|
|
||||||
|
Сервис автоматически формирует LogQL запросы для Loki API:
|
||||||
|
|
||||||
|
- `{job="cron",service_name="system"}` - точное совпадение значений
|
||||||
|
- `{job!=""}` - существование лейбла (если значение не указано)
|
||||||
|
|
||||||
|
## Swagger UI
|
||||||
|
|
||||||
|
После запуска сервиса документация API доступна по адресу:
|
||||||
|
http://localhost:5000/swagger
|
||||||
8
Services/ILokiService.cs
Normal file
8
Services/ILokiService.cs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
using GetFromLoki.Models;
|
||||||
|
|
||||||
|
namespace GetFromLoki.Services;
|
||||||
|
|
||||||
|
public interface ILokiService
|
||||||
|
{
|
||||||
|
Task<List<LogEntry>> GetLogsAsync(LogQueryRequest request);
|
||||||
|
}
|
||||||
7
Services/LokiOptions.cs
Normal file
7
Services/LokiOptions.cs
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
namespace GetFromLoki.Services;
|
||||||
|
|
||||||
|
public class LokiOptions
|
||||||
|
{
|
||||||
|
public string BaseUrl { get; set; } = "http://localhost:3100";
|
||||||
|
public int QueryTimeout { get; set; } = 30;
|
||||||
|
}
|
||||||
113
Services/LokiService.cs
Normal file
113
Services/LokiService.cs
Normal file
@ -0,0 +1,113 @@
|
|||||||
|
using System.Text.Json;
|
||||||
|
using GetFromLoki.Models;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
|
namespace GetFromLoki.Services;
|
||||||
|
|
||||||
|
public class LokiService : ILokiService
|
||||||
|
{
|
||||||
|
private readonly HttpClient _httpClient;
|
||||||
|
private readonly LokiOptions _options;
|
||||||
|
|
||||||
|
public LokiService(HttpClient httpClient, IOptions<LokiOptions> options)
|
||||||
|
{
|
||||||
|
_httpClient = httpClient;
|
||||||
|
_options = options.Value;
|
||||||
|
_httpClient.BaseAddress = new Uri(_options.BaseUrl);
|
||||||
|
_httpClient.Timeout = TimeSpan.FromSeconds(_options.QueryTimeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<List<LogEntry>> GetLogsAsync(LogQueryRequest request)
|
||||||
|
{
|
||||||
|
var logs = new List<LogEntry>();
|
||||||
|
|
||||||
|
// Формируем LogQL запрос
|
||||||
|
var query = BuildLogQLQuery(request);
|
||||||
|
|
||||||
|
// Формируем URL с параметрами
|
||||||
|
var queryParams = new List<string>
|
||||||
|
{
|
||||||
|
$"query={Uri.EscapeDataString(query)}",
|
||||||
|
$"limit={request.Limit ?? 100}"
|
||||||
|
};
|
||||||
|
|
||||||
|
if (request.StartTime.HasValue)
|
||||||
|
{
|
||||||
|
var startTimeNs = ((DateTimeOffset)request.StartTime.Value).ToUnixTimeMilliseconds() * 1_000_000;
|
||||||
|
queryParams.Add($"start={startTimeNs}");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.EndTime.HasValue)
|
||||||
|
{
|
||||||
|
var endTimeNs = ((DateTimeOffset)request.EndTime.Value).ToUnixTimeMilliseconds() * 1_000_000;
|
||||||
|
queryParams.Add($"end={endTimeNs}");
|
||||||
|
}
|
||||||
|
|
||||||
|
var url = $"/loki/api/v1/query_range?{string.Join("&", queryParams)}";
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var response = await _httpClient.GetAsync(url);
|
||||||
|
response.EnsureSuccessStatusCode();
|
||||||
|
|
||||||
|
var jsonResponse = await response.Content.ReadAsStringAsync();
|
||||||
|
var lokiResponse = JsonSerializer.Deserialize<LokiQueryResponse>(
|
||||||
|
jsonResponse,
|
||||||
|
new JsonSerializerOptions { PropertyNameCaseInsensitive = true }
|
||||||
|
);
|
||||||
|
|
||||||
|
if (lokiResponse?.Data?.Result != null)
|
||||||
|
{
|
||||||
|
foreach (var result in lokiResponse.Data.Result)
|
||||||
|
{
|
||||||
|
foreach (var value in result.Values)
|
||||||
|
{
|
||||||
|
if (value.Count >= 2)
|
||||||
|
{
|
||||||
|
// Loki возвращает timestamp в наносекундах
|
||||||
|
if (long.TryParse(value[0], out var timestampNs))
|
||||||
|
{
|
||||||
|
var timestamp = DateTimeOffset.FromUnixTimeMilliseconds(timestampNs / 1000000).DateTime;
|
||||||
|
logs.Add(new LogEntry
|
||||||
|
{
|
||||||
|
Timestamp = timestamp,
|
||||||
|
Message = value[1]
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException($"Ошибка при получении логов из Loki: {ex.Message}", ex);
|
||||||
|
}
|
||||||
|
|
||||||
|
return logs.OrderBy(l => l.Timestamp).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string BuildLogQLQuery(LogQueryRequest request)
|
||||||
|
{
|
||||||
|
var query = "{";
|
||||||
|
var conditions = new List<string>();
|
||||||
|
|
||||||
|
// Добавляем все лейблы
|
||||||
|
foreach (var label in request.Labels)
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrEmpty(label.Value))
|
||||||
|
{
|
||||||
|
conditions.Add($"{label.Key}=\"{label.Value}\"");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
conditions.Add($"{label.Key}!=\"\"");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
query += string.Join(",", conditions);
|
||||||
|
query += "}";
|
||||||
|
|
||||||
|
return query;
|
||||||
|
}
|
||||||
|
}
|
||||||
13
appsettings.json
Normal file
13
appsettings.json
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Microsoft.AspNetCore": "Warning"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"AllowedHosts": "*",
|
||||||
|
"Loki": {
|
||||||
|
"BaseUrl": "http://localhost:3100",
|
||||||
|
"QueryTimeout": 30
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user