Browse Source
- Remove the custom OpenObserve client - Reference the OpenObserve modulepull/1599/head
22 changed files with 31 additions and 375 deletions
@ -1,6 +0,0 @@ |
|||
namespace LINGYUN.Abp.Logging.Serilog.OpenObserve.Models; |
|||
|
|||
internal static class OpenObserveConstants |
|||
{ |
|||
public const int ZO_QUERY_TIMEOUT = 600; |
|||
} |
|||
@ -1,40 +0,0 @@ |
|||
using System; |
|||
using System.Text.Json.Serialization; |
|||
|
|||
namespace LINGYUN.Abp.Logging.Serilog.OpenObserve.Models; |
|||
|
|||
[Serializable] |
|||
public class OpenObserveErrorResponse |
|||
{ |
|||
/// <summary>
|
|||
/// Numeric error code (see below)
|
|||
/// </summary>
|
|||
[JsonPropertyName("code")] |
|||
public int Code { get; set; } |
|||
/// <summary>
|
|||
/// Human-readable problem description
|
|||
/// </summary>
|
|||
[JsonPropertyName("message")] |
|||
public string Message { get; set; } = default!; |
|||
|
|||
/// <summary>
|
|||
/// Raw technical detail (omitted when hint/suggestions are present)
|
|||
/// </summary>
|
|||
[JsonPropertyName("error_detail")] |
|||
public string? ErrorDetail { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// One-line guidance on how to fix the error
|
|||
/// </summary>
|
|||
[JsonPropertyName("hint")] |
|||
public string? Hint { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// Closest valid alternatives (up to 3), ranked by similarity
|
|||
/// </summary>
|
|||
[JsonPropertyName("suggestions")] |
|||
public string? Suggestions { get; set; } |
|||
|
|||
[JsonPropertyName("trace_id")] |
|||
public string? TraceId { get; set; } |
|||
} |
|||
@ -1,14 +0,0 @@ |
|||
using System; |
|||
using System.Net.Http; |
|||
|
|||
namespace LINGYUN.Abp.Logging.Serilog.OpenObserve.Models; |
|||
|
|||
public class OpenObserveRequestException : Exception |
|||
{ |
|||
public HttpResponseMessage HttpResponseMessage { get; } |
|||
public OpenObserveRequestException(HttpResponseMessage responseMessage) |
|||
: base(responseMessage.ReasonPhrase) |
|||
{ |
|||
HttpResponseMessage = responseMessage; |
|||
} |
|||
} |
|||
@ -1,14 +0,0 @@ |
|||
using System; |
|||
|
|||
namespace LINGYUN.Abp.Logging.Serilog.OpenObserve.Models.Search; |
|||
|
|||
public class OpenObserveSearchException : Exception |
|||
{ |
|||
public int Code => Error.Code; |
|||
public OpenObserveErrorResponse Error { get; } |
|||
public OpenObserveSearchException(OpenObserveErrorResponse error) |
|||
: base(error.Message) |
|||
{ |
|||
Error = error; |
|||
} |
|||
} |
|||
@ -1,7 +0,0 @@ |
|||
namespace LINGYUN.Abp.Logging.Serilog.OpenObserve.Models.Search; |
|||
|
|||
public enum SearchAgentMode |
|||
{ |
|||
Default = 0, |
|||
Partition = 1, |
|||
} |
|||
@ -1,35 +0,0 @@ |
|||
using System.Text.Json.Serialization; |
|||
|
|||
namespace LINGYUN.Abp.Logging.Serilog.OpenObserve.Models.Search; |
|||
|
|||
public class SearchAgentOptions |
|||
{ |
|||
/// <summary>
|
|||
/// query execution mode: default (result cache path) or partition (streaming partition loop, see below)
|
|||
/// </summary>
|
|||
[JsonPropertyName("mode")] |
|||
public string? Mode { get; } |
|||
/// <summary>
|
|||
/// response format for hits: json, csv, or md_table
|
|||
/// </summary>
|
|||
[JsonPropertyName("output_format")] |
|||
public string? OutputFormat { get; } |
|||
public SearchAgentOptions( |
|||
SearchAgentMode mode = SearchAgentMode.Default, |
|||
SearchAgentOutputFormat outputFormat = SearchAgentOutputFormat.Json) |
|||
{ |
|||
Mode = mode switch |
|||
{ |
|||
SearchAgentMode.Default => "default", |
|||
SearchAgentMode.Partition => "partition", |
|||
_ => "default", |
|||
}; |
|||
OutputFormat = outputFormat switch |
|||
{ |
|||
SearchAgentOutputFormat.Json => "json", |
|||
SearchAgentOutputFormat.Csv => "csv", |
|||
SearchAgentOutputFormat.MdTable => "md_table", |
|||
_ => "json", |
|||
}; |
|||
} |
|||
} |
|||
@ -1,8 +0,0 @@ |
|||
namespace LINGYUN.Abp.Logging.Serilog.OpenObserve.Models.Search; |
|||
|
|||
public enum SearchAgentOutputFormat |
|||
{ |
|||
Json = 0, |
|||
Csv = 1, |
|||
MdTable = 2, |
|||
} |
|||
@ -1,41 +0,0 @@ |
|||
using System; |
|||
using System.Text.Json.Serialization; |
|||
|
|||
namespace LINGYUN.Abp.Logging.Serilog.OpenObserve.Models.Search; |
|||
|
|||
public class SearchQuery |
|||
{ |
|||
/// <summary>
|
|||
/// use SQL query data, and filter data by start_time and end_time, and default order by _timestamp, you can use order by override order, and fetch offset limit by form and size
|
|||
/// </summary>
|
|||
[JsonPropertyName("sql")] |
|||
public string Sql { get; set; } |
|||
/// <summary>
|
|||
/// unit: microseconds, filter data by time range, you need always provide this value
|
|||
/// </summary>
|
|||
[JsonPropertyName("start_time")] |
|||
public long StartTime { get; set; } |
|||
/// <summary>
|
|||
/// unit: microseconds, filter data by time range, you need always provide this value
|
|||
/// </summary>
|
|||
[JsonPropertyName("end_time")] |
|||
public long EndTime { get; set; } |
|||
/// <summary>
|
|||
/// offset in SQL
|
|||
/// </summary>
|
|||
[JsonPropertyName("from")] |
|||
public long From { get; set; } |
|||
/// <summary>
|
|||
/// limit in SQL
|
|||
/// </summary>
|
|||
[JsonPropertyName("size")] |
|||
public long Size { get; set; } |
|||
public SearchQuery(string sql, DateTime startTime, DateTime endTime, long from = 0, long size = 10) |
|||
{ |
|||
Sql = sql; |
|||
StartTime = startTime.ToMicroseconds(); |
|||
EndTime = endTime.ToMicroseconds(); |
|||
From = from; |
|||
Size = size; |
|||
} |
|||
} |
|||
@ -1,48 +0,0 @@ |
|||
using System.Text.Json.Serialization; |
|||
|
|||
namespace LINGYUN.Abp.Logging.Serilog.OpenObserve.Models.Search; |
|||
|
|||
public class SearchRequest |
|||
{ |
|||
/// <summary>
|
|||
/// query params
|
|||
/// </summary>
|
|||
[JsonPropertyName("query")] |
|||
public SearchQuery Query { get; } |
|||
/// <summary>
|
|||
/// default is empty, support: ui, dashboards, reports, alerts
|
|||
/// </summary>
|
|||
[JsonPropertyName("search_type")] |
|||
public string? SearchType { get; } |
|||
/// <summary>
|
|||
/// default value based on ZO_QUERY_TIMEOUT=600
|
|||
/// </summary>
|
|||
[JsonPropertyName("timeout")] |
|||
public int? Timeout { get; } |
|||
/// <summary>
|
|||
/// options for agent/MCP clients
|
|||
/// </summary>
|
|||
[JsonPropertyName("agent_options")] |
|||
public SearchAgentOptions? AgentOptions { get; } |
|||
public SearchRequest( |
|||
SearchQuery query, |
|||
SearchType? searchType = null, |
|||
int? timeout = OpenObserveConstants.ZO_QUERY_TIMEOUT, |
|||
SearchAgentOptions? agentOptions = null) |
|||
{ |
|||
Query = query; |
|||
Timeout = timeout; |
|||
AgentOptions = agentOptions; |
|||
if (searchType != null) |
|||
{ |
|||
SearchType = searchType switch |
|||
{ |
|||
Search.SearchType.UI => "ui", |
|||
Search.SearchType.Dashboards => "dashboards", |
|||
Search.SearchType.Reports => "reports", |
|||
Search.SearchType.Alerts => "alerts", |
|||
_ => null, |
|||
}; |
|||
} |
|||
} |
|||
} |
|||
@ -1,29 +0,0 @@ |
|||
using System; |
|||
using System.Text.Json.Serialization; |
|||
|
|||
namespace LINGYUN.Abp.Logging.Serilog.OpenObserve.Models.Search; |
|||
|
|||
[Serializable] |
|||
public class SearchResponse<THit> |
|||
{ |
|||
[JsonPropertyName("hits")] |
|||
public THit[] Hits { get; set; } = new THit[0]; |
|||
|
|||
[JsonPropertyName("total")] |
|||
public long Total { get; set; } |
|||
|
|||
[JsonPropertyName("from")] |
|||
public int From { get; set; } |
|||
|
|||
[JsonPropertyName("size")] |
|||
public int Size { get; set; } |
|||
|
|||
[JsonPropertyName("trace_id")] |
|||
public string? TraceId { get; set; } |
|||
|
|||
[JsonPropertyName("order_by")] |
|||
public string? OrderBy { get; set; } |
|||
|
|||
[JsonPropertyName("order_by_metadata")] |
|||
public string[][]? OrderByMetadata { get; set; } |
|||
} |
|||
@ -1,9 +0,0 @@ |
|||
namespace LINGYUN.Abp.Logging.Serilog.OpenObserve.Models.Search; |
|||
|
|||
public enum SearchType |
|||
{ |
|||
UI = 0, |
|||
Dashboards = 1, |
|||
Reports = 2, |
|||
Alerts = 3, |
|||
} |
|||
@ -1,8 +1,8 @@ |
|||
using System.Text.Json.Serialization; |
|||
|
|||
namespace LINGYUN.Abp.Logging.Serilog.OpenObserve.Models.Search; |
|||
namespace LINGYUN.Abp.Logging.Serilog.OpenObserve.Models; |
|||
|
|||
public class SearchTotal |
|||
public class SerilogTotal |
|||
{ |
|||
[JsonPropertyName("total")] |
|||
public long Total { get; set; } |
|||
@ -1,56 +0,0 @@ |
|||
using LINGYUN.Abp.Logging.Serilog.OpenObserve.Models; |
|||
using LINGYUN.Abp.Logging.Serilog.OpenObserve.Models.Search; |
|||
using Microsoft.Extensions.Options; |
|||
using System.Net.Http; |
|||
using System.Net.Http.Json; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Json; |
|||
|
|||
namespace LINGYUN.Abp.Logging.Serilog.OpenObserve; |
|||
|
|||
public partial class OpenObserveClient |
|||
{ |
|||
protected HttpClient HttpClient { get; } |
|||
protected IJsonSerializer JsonSerializer { get; } |
|||
protected AbpLoggingSerilogOpenObserveOptions Options { get; } |
|||
public OpenObserveClient( |
|||
HttpClient httpClient, |
|||
IJsonSerializer jsonSerializer, |
|||
IOptions<AbpLoggingSerilogOpenObserveOptions> options) |
|||
{ |
|||
HttpClient = httpClient; |
|||
Options = options.Value; |
|||
JsonSerializer = jsonSerializer; |
|||
} |
|||
|
|||
public async virtual Task<SearchResponse<THint>> SearchAsync<THint>( |
|||
SearchQuery query, |
|||
SearchType? searchType = null, |
|||
int? timeout = OpenObserveConstants.ZO_QUERY_TIMEOUT, |
|||
SearchAgentOptions? agentOptions = null, |
|||
CancellationToken cancellationToken = default) |
|||
{ |
|||
var httpResponse = await HttpClient.PostAsJsonAsync( |
|||
$"/api/{Options.Organization}/_search", |
|||
new SearchRequest(query, searchType, timeout, agentOptions), |
|||
cancellationToken); |
|||
|
|||
if (httpResponse.IsSuccessStatusCode) |
|||
{ |
|||
var responseContent = await httpResponse.Content.ReadAsStringAsync(cancellationToken); |
|||
return JsonSerializer.Deserialize<SearchResponse<THint>>(responseContent); |
|||
} |
|||
|
|||
var statusCode = (int)httpResponse.StatusCode; |
|||
if (statusCode >= 400 && statusCode < 500) |
|||
{ |
|||
var responseContent = await httpResponse.Content.ReadAsStringAsync(cancellationToken); |
|||
var errorResponse = JsonSerializer.Deserialize<OpenObserveErrorResponse>(responseContent); |
|||
|
|||
throw new OpenObserveSearchException(errorResponse); |
|||
} |
|||
|
|||
throw new OpenObserveRequestException(httpResponse); |
|||
} |
|||
} |
|||
@ -1,28 +0,0 @@ |
|||
using LINGYUN.Abp.Logging.Serilog.OpenObserve; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
using System.Net.Http.Headers; |
|||
|
|||
namespace System.Net.Http; |
|||
|
|||
internal static class HttpClientOpenObserveExtensions |
|||
{ |
|||
private const string HttpClientName = "__Abp_OpenObserve_Client"; |
|||
public static IServiceCollection AddOpenObserveHttpClient(this IServiceCollection services) |
|||
{ |
|||
services.AddHttpClient<OpenObserveClient>( |
|||
HttpClientName, |
|||
(sp, client) => |
|||
{ |
|||
var options = sp.GetRequiredService<IOptions<AbpLoggingSerilogOpenObserveOptions>>().Value; |
|||
|
|||
client.BaseAddress = new Uri(options.Endpoint); |
|||
if (!options.AccessToken.IsNullOrWhiteSpace() && AuthenticationHeaderValue.TryParse(options.AccessToken, out var authenticationHeader)) |
|||
{ |
|||
client.DefaultRequestHeaders.Authorization = authenticationHeader; |
|||
} |
|||
}); |
|||
|
|||
return services; |
|||
} |
|||
} |
|||
Loading…
Reference in new issue