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 operator::insert::PreparedMetricInserts;
43use otel_arrow_rust::proto::opentelemetry::collector::metrics::v1::ExportMetricsServiceRequest;
44use pipeline::{GreptimePipelineParams, Pipeline, PipelineInfo, PipelineVersion, PipelineWay};
45use serde_json::Value;
46use session::context::{QueryContext, QueryContextRef};
47use table::metadata::TableInfoRef;
48
49#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
50pub struct DashboardDefinition {
51 pub name: String,
52 pub definition: String,
53}
54
55use crate::error::Result;
56use crate::http::jaeger::QueryTraceParams;
57use crate::influxdb::InfluxdbRequest;
58use crate::opentsdb::codec::DataPoint;
59pub type OpentsdbProtocolHandlerRef = Arc<dyn OpentsdbProtocolHandler + Send + Sync>;
60pub type InfluxdbLineProtocolHandlerRef = Arc<dyn InfluxdbLineProtocolHandler + Send + Sync>;
61pub type PromStoreProtocolHandlerRef = Arc<dyn PromStoreProtocolHandler + Send + Sync>;
62pub type OpenTelemetryProtocolHandlerRef = Arc<dyn OpenTelemetryProtocolHandler + Send + Sync>;
63pub type PipelineHandlerRef = Arc<dyn PipelineHandler + Send + Sync>;
64pub type LogQueryHandlerRef = Arc<dyn LogQueryHandler + Send + Sync>;
65pub type JaegerQueryHandlerRef = Arc<dyn JaegerQueryHandler + Send + Sync>;
66
67#[derive(Debug, Default, Clone)]
68pub struct TraceIngestOutcome {
69 pub write_cost: usize,
70 pub accepted_spans: usize,
71 pub rejected_spans: usize,
72 pub error_message: Option<String>,
73}
74
75#[derive(Debug, Default, Clone)]
77pub struct MetricsIngestOutcome {
78 pub write_cost: usize,
79 pub accepted_data_points: i64,
80 pub rejected_data_points: i64,
81 pub error_message: Option<String>,
82}
83
84#[async_trait]
85pub trait InfluxdbLineProtocolHandler {
86 async fn exec(&self, request: InfluxdbRequest, ctx: QueryContextRef) -> Result<Output>;
87}
88
89#[async_trait]
90pub trait OpentsdbProtocolHandler {
91 async fn preflight(&self, data_points: &[DataPoint], ctx: QueryContextRef) -> Result<()>;
93
94 async fn exec(&self, data_points: Vec<DataPoint>, ctx: QueryContextRef) -> Result<usize>;
95
96 async fn exec_batch(&self, data_points: Vec<DataPoint>, ctx: QueryContextRef) -> Result<usize> {
100 self.exec(data_points, ctx).await
101 }
102}
103
104pub struct PromStoreResponse {
105 pub content_type: HeaderValue,
106 pub content_encoding: HeaderValue,
107 pub resp_metrics: HashMap<String, Value>,
108 pub body: Vec<u8>,
109}
110
111#[async_trait]
112pub trait PromStoreProtocolHandler {
113 async fn prepare_metric_write(
116 &self,
117 _request: RowInsertRequests,
118 _ctx: QueryContextRef,
119 ) -> Result<PreparedMetricInserts> {
120 crate::error::NotSupportedSnafu {
121 feat: "metric write preparation",
122 }
123 .fail()
124 }
125
126 async fn write_prepared_metric(
128 &self,
129 _request: PreparedMetricInserts,
130 _ctx: QueryContextRef,
131 ) -> Result<Output> {
132 crate::error::NotSupportedSnafu {
133 feat: "prepared metric ingestion",
134 }
135 .fail()
136 }
137
138 async fn write_exemplars_with_sources(
140 &self,
141 request: RowInsertRequests,
142 ctx: QueryContextRef,
143 _sources: &[TableInfoRef],
144 ) -> Result<Output> {
145 self.write_exemplars_prepared(request, ctx).await
146 }
147
148 async fn check_exemplar_sources(
150 &self,
151 _sources: &std::collections::BTreeSet<String>,
152 _ctx: QueryContextRef,
153 ) -> Result<()> {
154 crate::error::NotSupportedSnafu {
155 feat: "exemplar source authorization",
156 }
157 .fail()
158 }
159
160 async fn write_exemplars_prepared(
161 &self,
162 _request: RowInsertRequests,
163 _ctx: QueryContextRef,
164 ) -> Result<Output> {
165 crate::error::NotSupportedSnafu {
166 feat: "exemplar ingestion",
167 }
168 .fail()
169 }
170
171 async fn pre_write(&self, request: &RowInsertRequests, ctx: QueryContextRef) -> Result<()>;
173
174 async fn write_prepared(
176 &self,
177 request: RowInsertRequests,
178 ctx: QueryContextRef,
179 with_metric_engine: bool,
180 ) -> Result<Output>;
181
182 async fn write(
184 &self,
185 request: RowInsertRequests,
186 ctx: QueryContextRef,
187 with_metric_engine: bool,
188 ) -> Result<Output>;
189
190 async fn write_all(
192 &self,
193 requests: Vec<(QueryContextRef, RowInsertRequests)>,
194 with_metric_engine: bool,
195 ) -> Result<Vec<Result<Output>>>;
196
197 async fn read(&self, request: ReadRequest, ctx: QueryContextRef) -> Result<PromStoreResponse>;
199}
200
201#[async_trait]
202pub trait OpenTelemetryProtocolHandler: PipelineHandler {
203 async fn metrics(
205 &self,
206 request: ExportMetricsServiceRequest,
207 ctx: QueryContextRef,
208 ) -> Result<MetricsIngestOutcome>;
209
210 async fn traces(
212 &self,
213 pipeline_handler: PipelineHandlerRef,
214 request: ExportTraceServiceRequest,
215 pipeline: PipelineWay,
216 pipeline_params: GreptimePipelineParams,
217 table_name: String,
218 ctx: QueryContextRef,
219 ) -> Result<TraceIngestOutcome>;
220
221 async fn logs(
222 &self,
223 pipeline_handler: PipelineHandlerRef,
224 request: ExportLogsServiceRequest,
225 pipeline: PipelineWay,
226 pipeline_params: GreptimePipelineParams,
227 table_name: String,
228 ctx: QueryContextRef,
229 ) -> Result<Vec<Output>>;
230}
231
232#[async_trait]
240pub trait PipelineHandler {
241 async fn insert(&self, input: RowInsertRequests, ctx: QueryContextRef) -> Result<Output>;
242
243 async fn insert_all(
245 &self,
246 inputs: Vec<(QueryContextRef, RowInsertRequests)>,
247 ) -> Result<Vec<Result<Output>>>;
248
249 fn check_pipeline_query_permission(&self, query_ctx: &QueryContextRef) -> Result<()>;
250
251 async fn get_pipeline(
258 &self,
259 name: &str,
260 version: PipelineVersion,
261 query_ctx: QueryContextRef,
262 ) -> Result<Arc<Pipeline>>;
263
264 async fn insert_pipeline(
265 &self,
266 name: &str,
267 content_type: &str,
268 pipeline: &str,
269 query_ctx: QueryContextRef,
270 ) -> Result<PipelineInfo>;
271
272 async fn delete_pipeline(
273 &self,
274 name: &str,
275 version: PipelineVersion,
276 query_ctx: QueryContextRef,
277 ) -> Result<Option<()>>;
278
279 async fn get_table(
280 &self,
281 table: &str,
282 query_ctx: &QueryContext,
283 ) -> std::result::Result<Option<Arc<table::Table>>, catalog::error::Error>;
284
285 fn build_pipeline(&self, pipeline: &str) -> Result<Pipeline>;
287
288 async fn get_pipeline_str(
290 &self,
291 name: &str,
292 version: PipelineVersion,
293 query_ctx: QueryContextRef,
294 ) -> Result<(String, TimestampNanosecond)>;
295}
296
297pub type DashboardHandlerRef = Arc<dyn DashboardHandler + Send + Sync>;
299
300#[async_trait]
301pub trait DashboardHandler {
302 async fn save(&self, name: &str, definition: &str, ctx: QueryContextRef) -> Result<()>;
303
304 async fn list(&self, ctx: QueryContextRef) -> Result<Vec<DashboardDefinition>>;
305
306 async fn delete(&self, name: &str, ctx: QueryContextRef) -> Result<()>;
307}
308
309#[async_trait]
311pub trait LogQueryHandler {
312 async fn query(&self, query: LogQuery, ctx: QueryContextRef) -> Result<Output>;
314
315 fn catalog_manager(&self, ctx: &QueryContext) -> Result<&dyn CatalogManager>;
317}
318
319#[async_trait]
321pub trait JaegerQueryHandler {
322 async fn get_services(&self, ctx: QueryContextRef) -> Result<Output>;
324
325 async fn get_operations(
327 &self,
328 ctx: QueryContextRef,
329 service_name: &str,
330 span_kind: Option<&str>,
331 ) -> Result<Output>;
332
333 async fn get_trace(
338 &self,
339 ctx: QueryContextRef,
340 trace_id: &str,
341 start_time: Option<i64>,
342 end_time: Option<i64>,
343 limit: Option<usize>,
344 ) -> Result<Output>;
345
346 async fn find_traces(
348 &self,
349 ctx: QueryContextRef,
350 query_params: QueryTraceParams,
351 ) -> Result<Output>;
352}
353
354#[async_trait]
360pub trait CopyInHandler: Send + Sync {
361 async fn copy_in_table(
366 &self,
367 catalog: &str,
368 schema: &str,
369 table: &str,
370 query_ctx: QueryContextRef,
371 ) -> Result<Option<TableInfoRef>>;
372
373 async fn copy_in_insert(
376 &self,
377 requests: RowInsertRequests,
378 query_ctx: QueryContextRef,
379 ) -> Result<Output>;
380}
381
382pub type CopyInHandlerRef = Arc<dyn CopyInHandler + Send + Sync>;