mirror of
https://github.com/openobserve/openobserve.git
synced 2026-11-23 00:04:52 +00:00
Design at: #13343 ## What `_search` gains an optional `agent_options.output_format` parameter (`json` | `csv` | `md_table`, default `json`). When set to `csv`/`md_table`, the response's `hits` array is rendered as a single compact string block: ```json { "query": { "sql": "...", "start_time": ..., "end_time": ... }, "agent_options": { "output_format": "csv" } } ``` Response then carries three new optional fields (never serialized otherwise): `format` (the format actually used), `data` (the formatted block), `advisory` (note about server-side formatting decisions), with `columns` reporting the column order and `hits` emptied. ## Why Agent/MCP consumers pay per token, and a JSON hits array repeats every key on every row — the repeated keys, not whitespace, are the dominant cost (minify only gets to ~78%). csv removes them: ~40% fewer tokens than minified JSON on typical log results, letting the same context budget hold ~1.7x more rows. `md_table` (~8% more than csv) aligns better for small result sets. This is the first piece of the Agent query-optimization (AX) track; `agent_options` is the extension point for the follow-ups (token-budget pagination, query budget/continuation). ## Design - **Serialization edge only**: formatting runs in the `_search` handler after `SearchService::cache::search` returns. Engine, partition scheduling and result cache always see original JSON hits; cached entries never contain formatted strings, and the cache key is unchanged, so a UI (json) query and an agent (csv) query share the same cache entry. - **Model-friendly escaping**: newlines inside cells become a literal `\n` so no record ever spans physical lines (multi-line RFC 4180 quoted cells are a known LLM parsing hazard); commas/quotes still get standard csv quoting so rows stay machine-parsable. `md_table` escapes `|`. Nested objects/arrays are minified JSON in the cell. - **Column order**: server-provided `columns` when present, otherwise first-seen key order with `_timestamp` promoted to the front. - **Sparse fallback**: results with >= 20 columns and > 50% empty cells (typical `select *` on a wide stream) fall back to ndjson — per-row keys are cheaper and less error-prone there than a mostly-empty grid. The fallback is announced via `advisory`, never silent. Non-object rows fall back the same way. - **Zero impact when unset**: requests without `agent_options` produce byte-for-byte identical responses. The remaining 9 files changed are mechanical `agent_options: None` additions to internal `Request` struct literals. - The `_search` `x-o2-mcp` tool description now mentions the option, and `SearchAgentOptions` flows into the generated OpenAPI/MCP tool schema automatically. ## Testing - 9 unit tests in `agent_format.rs`: json no-op, csv basics + `_timestamp` promotion, newline/comma/quote/nested escaping, missing/null cells, server column order, md_table pipe escaping, sparse fallback, non-object fallback, empty hits. - Verified against a running local instance: default path unchanged (no new keys), csv/md_table output with all escaping cases, sparse stream (32 cols, 90% empty) falls back to ndjson with advisory, invalid `output_format` returns 422 listing valid variants, `SearchAgentOptions` present in `/api-doc/openapi.json`. - `cargo check --workspace`, `cargo clippy -p openobserve-api-query`, `cargo fmt` clean.
98 lines
3.3 KiB
Rust
98 lines
3.3 KiB
Rust
// Copyright 2026 OpenObserve Inc.
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Affero General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
use std::{fs, path::Path};
|
|
|
|
use async_trait::async_trait;
|
|
use axum::extract::Query;
|
|
use config::{TIMESTAMP_COL_NAME, get_config, meta::search};
|
|
use hashbrown::HashMap;
|
|
|
|
use crate::{
|
|
cli::data::{Context, cli::Cli},
|
|
common::utils::http::{
|
|
get_search_event_context_from_request, get_search_type_from_request,
|
|
get_stream_type_from_request,
|
|
},
|
|
service::search as SearchService,
|
|
};
|
|
|
|
pub struct Export {}
|
|
|
|
#[async_trait]
|
|
impl Context for Export {
|
|
async fn operator(c: Cli) -> Result<bool, anyhow::Error> {
|
|
let cfg = get_config();
|
|
let map = HashMap::from([("type".to_string(), c.stream_type)]);
|
|
let query_map = Query(map);
|
|
let stream_type = get_stream_type_from_request(&query_map).unwrap_or_default();
|
|
|
|
let table = c.stream_name;
|
|
let search_type = match get_search_type_from_request(&query_map) {
|
|
Ok(v) => v,
|
|
Err(_) => return Ok(false),
|
|
};
|
|
let search_event_context = search_type
|
|
.as_ref()
|
|
.and_then(|event_type| get_search_event_context_from_request(event_type, &query_map));
|
|
let query = search::Query {
|
|
sql: format!("select * from {table} ORDER BY {TIMESTAMP_COL_NAME} ASC"),
|
|
from: 0,
|
|
size: cfg.limit.query_default_limit,
|
|
quick_mode: false,
|
|
query_type: "".to_owned(),
|
|
start_time: c.start_time,
|
|
end_time: c.end_time,
|
|
..Default::default()
|
|
};
|
|
|
|
let req = search::Request {
|
|
query,
|
|
encoding: search::RequestEncoding::Empty,
|
|
regions: vec![],
|
|
clusters: vec![],
|
|
timeout: 0,
|
|
search_type,
|
|
search_event_context,
|
|
use_cache: false,
|
|
clear_cache: false,
|
|
local_mode: None,
|
|
agent_options: None,
|
|
};
|
|
|
|
match SearchService::search("", &c.org, stream_type, None, &req).await {
|
|
Ok(res) => {
|
|
if c.file_type != "json" {
|
|
eprintln!("No other file types are implemented");
|
|
return Ok(false);
|
|
}
|
|
let path = Path::new(c.data.as_str());
|
|
fs::create_dir_all(path)?;
|
|
let file = fs::File::create(path.join(format!(
|
|
"{}.{}",
|
|
chrono::Local::now().timestamp_micros(),
|
|
c.file_type
|
|
)))?;
|
|
serde_json::to_writer_pretty(file, &res.hits)?;
|
|
Ok(true)
|
|
}
|
|
Err(e) => {
|
|
eprintln!("search error: {e:?}");
|
|
Ok(false)
|
|
}
|
|
}
|
|
}
|
|
}
|