1pub mod grpc;
27pub mod sql;
28
29use std::collections::HashMap;
30use std::sync::Arc;
31
32use api::prom_store::remote::ReadRequest;
33use api::v1::RowInsertRequests;
34use async_trait::async_trait;
35use catalog::CatalogManager;
36use common_query::Output;
37use datatypes::timestamp::TimestampNanosecond;
38use headers::HeaderValue;
39use log_query::LogQuery;
40use opentelemetry_proto::tonic::collector::logs::v1::ExportLogsServiceRequest;
41use opentelemetry_proto::tonic::collector::trace::v1::ExportTraceServiceRequest;
42use otel_arrow_rust::proto::opentelemetry::collector::metrics::v1::ExportMetricsServiceRequest;
43use pipeline::{GreptimePipelineParams, Pipeline, PipelineInfo, PipelineVersion, PipelineWay};
44use serde_json::Value;
45use session::context::{QueryContext, QueryContextRef};
46use table::metadata::TableInfoRef;
47
48#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
49pub struct DashboardDefinition {
50 pub name: String,
51 pub definition: String,
52}
53
54use crate::error::Result;
55use crate::http::jaeger::QueryTraceParams;
56use crate::influxdb::InfluxdbRequest;
57use crate::opentsdb::codec::DataPoint;
58pub type OpentsdbProtocolHandlerRef = Arc<dyn OpentsdbProtocolHandler + Send + Sync>;
59pub type InfluxdbLineProtocolHandlerRef = Arc<dyn InfluxdbLineProtocolHandler + Send + Sync>;
60pub type PromStoreProtocolHandlerRef = Arc<dyn PromStoreProtocolHandler + Send + Sync>;
61pub type OpenTelemetryProtocolHandlerRef = Arc<dyn OpenTelemetryProtocolHandler + Send + Sync>;
62pub type PipelineHandlerRef = Arc<dyn PipelineHandler + Send + Sync>;
63pub type LogQueryHandlerRef = Arc<dyn LogQueryHandler + Send + Sync>;
64pub type JaegerQueryHandlerRef = Arc<dyn JaegerQueryHandler + Send + Sync>;
65
66#[derive(Debug, Default, Clone)]
67pub struct TraceIngestOutcome {
68 pub write_cost: usize,
69 pub accepted_spans: usize,
70 pub rejected_spans: usize,
71 pub error_message: Option<String>,
72}
73
74#[derive(Debug, Default, Clone)]
76pub struct MetricsIngestOutcome {
77 pub write_cost: usize,
78 pub accepted_data_points: i64,
79 pub rejected_data_points: i64,
80 pub error_message: Option<String>,
81}
82
83#[async_trait]
84pub trait InfluxdbLineProtocolHandler {
85 async fn exec(&self, request: InfluxdbRequest, ctx: QueryContextRef) -> Result<Output>;
86}
87
88#[async_trait]
89pub trait OpentsdbProtocolHandler {
90 async fn preflight(&self, data_points: &[DataPoint], ctx: QueryContextRef) -> Result<()>;
92
93 async fn exec(&self, data_points: Vec<DataPoint>, ctx: QueryContextRef) -> Result<usize>;
94
95 async fn exec_batch(&self, data_points: Vec<DataPoint>, ctx: QueryContextRef) -> Result<usize> {
99 self.exec(data_points, ctx).await
100 }
101}
102
103pub struct PromStoreResponse {
104 pub content_type: HeaderValue,
105 pub content_encoding: HeaderValue,
106 pub resp_metrics: HashMap<String, Value>,
107 pub body: Vec<u8>,
108}
109
110#[async_trait]
111pub trait PromStoreProtocolHandler {
112 async fn pre_write(&self, request: &RowInsertRequests, ctx: QueryContextRef) -> Result<()>;
114
115 async fn write_prepared(
117 &self,
118 request: RowInsertRequests,
119 ctx: QueryContextRef,
120 with_metric_engine: bool,
121 ) -> Result<Output>;
122
123 async fn write(
125 &self,
126 request: RowInsertRequests,
127 ctx: QueryContextRef,
128 with_metric_engine: bool,
129 ) -> Result<Output>;
130
131 async fn write_all(
133 &self,
134 requests: Vec<(QueryContextRef, RowInsertRequests)>,
135 with_metric_engine: bool,
136 ) -> Result<Vec<Result<Output>>>;
137
138 async fn read(&self, request: ReadRequest, ctx: QueryContextRef) -> Result<PromStoreResponse>;
140}
141
142#[async_trait]
143pub trait OpenTelemetryProtocolHandler: PipelineHandler {
144 async fn metrics(
146 &self,
147 request: ExportMetricsServiceRequest,
148 ctx: QueryContextRef,
149 ) -> Result<MetricsIngestOutcome>;
150
151 async fn traces(
153 &self,
154 pipeline_handler: PipelineHandlerRef,
155 request: ExportTraceServiceRequest,
156 pipeline: PipelineWay,
157 pipeline_params: GreptimePipelineParams,
158 table_name: String,
159 ctx: QueryContextRef,
160 ) -> Result<TraceIngestOutcome>;
161
162 async fn logs(
163 &self,
164 pipeline_handler: PipelineHandlerRef,
165 request: ExportLogsServiceRequest,
166 pipeline: PipelineWay,
167 pipeline_params: GreptimePipelineParams,
168 table_name: String,
169 ctx: QueryContextRef,
170 ) -> Result<Vec<Output>>;
171}
172
173#[async_trait]
181pub trait PipelineHandler {
182 async fn insert(&self, input: RowInsertRequests, ctx: QueryContextRef) -> Result<Output>;
183
184 async fn insert_all(
186 &self,
187 inputs: Vec<(QueryContextRef, RowInsertRequests)>,
188 ) -> Result<Vec<Result<Output>>>;
189
190 fn check_pipeline_query_permission(&self, query_ctx: &QueryContextRef) -> Result<()>;
191
192 async fn get_pipeline(
199 &self,
200 name: &str,
201 version: PipelineVersion,
202 query_ctx: QueryContextRef,
203 ) -> Result<Arc<Pipeline>>;
204
205 async fn insert_pipeline(
206 &self,
207 name: &str,
208 content_type: &str,
209 pipeline: &str,
210 query_ctx: QueryContextRef,
211 ) -> Result<PipelineInfo>;
212
213 async fn delete_pipeline(
214 &self,
215 name: &str,
216 version: PipelineVersion,
217 query_ctx: QueryContextRef,
218 ) -> Result<Option<()>>;
219
220 async fn get_table(
221 &self,
222 table: &str,
223 query_ctx: &QueryContext,
224 ) -> std::result::Result<Option<Arc<table::Table>>, catalog::error::Error>;
225
226 fn build_pipeline(&self, pipeline: &str) -> Result<Pipeline>;
228
229 async fn get_pipeline_str(
231 &self,
232 name: &str,
233 version: PipelineVersion,
234 query_ctx: QueryContextRef,
235 ) -> Result<(String, TimestampNanosecond)>;
236}
237
238pub type DashboardHandlerRef = Arc<dyn DashboardHandler + Send + Sync>;
240
241#[async_trait]
242pub trait DashboardHandler {
243 async fn save(&self, name: &str, definition: &str, ctx: QueryContextRef) -> Result<()>;
244
245 async fn list(&self, ctx: QueryContextRef) -> Result<Vec<DashboardDefinition>>;
246
247 async fn delete(&self, name: &str, ctx: QueryContextRef) -> Result<()>;
248}
249
250#[async_trait]
252pub trait LogQueryHandler {
253 async fn query(&self, query: LogQuery, ctx: QueryContextRef) -> Result<Output>;
255
256 fn catalog_manager(&self, ctx: &QueryContext) -> Result<&dyn CatalogManager>;
258}
259
260#[async_trait]
262pub trait JaegerQueryHandler {
263 async fn get_services(&self, ctx: QueryContextRef) -> Result<Output>;
265
266 async fn get_operations(
268 &self,
269 ctx: QueryContextRef,
270 service_name: &str,
271 span_kind: Option<&str>,
272 ) -> Result<Output>;
273
274 async fn get_trace(
279 &self,
280 ctx: QueryContextRef,
281 trace_id: &str,
282 start_time: Option<i64>,
283 end_time: Option<i64>,
284 limit: Option<usize>,
285 ) -> Result<Output>;
286
287 async fn find_traces(
289 &self,
290 ctx: QueryContextRef,
291 query_params: QueryTraceParams,
292 ) -> Result<Output>;
293}
294
295#[async_trait]
301pub trait CopyInHandler: Send + Sync {
302 async fn copy_in_table(
307 &self,
308 catalog: &str,
309 schema: &str,
310 table: &str,
311 query_ctx: QueryContextRef,
312 ) -> Result<Option<TableInfoRef>>;
313
314 async fn copy_in_insert(
317 &self,
318 requests: RowInsertRequests,
319 query_ctx: QueryContextRef,
320 ) -> Result<Output>;
321}
322
323pub type CopyInHandlerRef = Arc<dyn CopyInHandler + Send + Sync>;