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::{
45 GreptimePipelineParams, Pipeline, PipelineInfo, PipelineOptions, PipelineVersion, PipelineWay,
46};
47use serde_json::Value;
48use session::context::{QueryContext, QueryContextRef};
49use table::metadata::TableInfoRef;
50
51#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
52pub struct DashboardDefinition {
53 pub name: String,
54 pub definition: String,
55}
56
57use crate::error::Result;
58use crate::http::jaeger::QueryTraceParams;
59use crate::influxdb::InfluxdbRequest;
60use crate::opentsdb::codec::DataPoint;
61use crate::request_memory_limiter::ServerMemoryLimiter;
62pub type OpentsdbProtocolHandlerRef = Arc<dyn OpentsdbProtocolHandler + Send + Sync>;
63pub type InfluxdbLineProtocolHandlerRef = Arc<dyn InfluxdbLineProtocolHandler + Send + Sync>;
64pub type PromStoreProtocolHandlerRef = Arc<dyn PromStoreProtocolHandler + Send + Sync>;
65pub type OpenTelemetryProtocolHandlerRef = Arc<dyn OpenTelemetryProtocolHandler + Send + Sync>;
66pub type PipelineHandlerRef = Arc<dyn PipelineHandler + Send + Sync>;
67pub type LogQueryHandlerRef = Arc<dyn LogQueryHandler + Send + Sync>;
68pub type JaegerQueryHandlerRef = Arc<dyn JaegerQueryHandler + Send + Sync>;
69
70#[derive(Debug, Default, Clone)]
71pub struct TraceIngestOutcome {
72 pub write_cost: usize,
73 pub accepted_spans: usize,
74 pub rejected_spans: usize,
75 pub error_message: Option<String>,
76}
77
78#[derive(Debug, Default, Clone)]
80pub struct MetricsIngestOutcome {
81 pub write_cost: usize,
82 pub accepted_data_points: i64,
83 pub rejected_data_points: i64,
84 pub error_message: Option<String>,
85}
86
87#[async_trait]
88pub trait InfluxdbLineProtocolHandler {
89 async fn exec(&self, request: InfluxdbRequest, ctx: QueryContextRef) -> Result<Output>;
90}
91
92#[async_trait]
93pub trait OpentsdbProtocolHandler {
94 async fn preflight(&self, data_points: &[DataPoint], ctx: QueryContextRef) -> Result<()>;
96
97 async fn exec(&self, data_points: Vec<DataPoint>, ctx: QueryContextRef) -> Result<usize>;
98
99 async fn exec_batch(&self, data_points: Vec<DataPoint>, ctx: QueryContextRef) -> Result<usize> {
103 self.exec(data_points, ctx).await
104 }
105}
106
107pub struct PromStoreResponse {
108 pub content_type: HeaderValue,
109 pub content_encoding: HeaderValue,
110 pub resp_metrics: HashMap<String, Value>,
111 pub body: Vec<u8>,
112}
113
114#[async_trait]
115pub trait PromStoreProtocolHandler {
116 async fn prepare_metric_write(
119 &self,
120 _request: RowInsertRequests,
121 _ctx: QueryContextRef,
122 ) -> Result<PreparedMetricInserts> {
123 crate::error::NotSupportedSnafu {
124 feat: "metric write preparation",
125 }
126 .fail()
127 }
128
129 async fn write_prepared_metric(
131 &self,
132 _request: PreparedMetricInserts,
133 _ctx: QueryContextRef,
134 ) -> Result<Output> {
135 crate::error::NotSupportedSnafu {
136 feat: "prepared metric ingestion",
137 }
138 .fail()
139 }
140
141 async fn write_exemplars_with_sources(
143 &self,
144 request: RowInsertRequests,
145 ctx: QueryContextRef,
146 _sources: &[TableInfoRef],
147 ) -> Result<Output> {
148 self.write_exemplars_prepared(request, ctx).await
149 }
150
151 async fn check_exemplar_sources(
153 &self,
154 _sources: &std::collections::BTreeSet<String>,
155 _ctx: QueryContextRef,
156 ) -> Result<()> {
157 crate::error::NotSupportedSnafu {
158 feat: "exemplar source authorization",
159 }
160 .fail()
161 }
162
163 async fn write_exemplars_prepared(
164 &self,
165 _request: RowInsertRequests,
166 _ctx: QueryContextRef,
167 ) -> Result<Output> {
168 crate::error::NotSupportedSnafu {
169 feat: "exemplar ingestion",
170 }
171 .fail()
172 }
173
174 async fn pre_write(&self, request: &RowInsertRequests, ctx: QueryContextRef) -> Result<()>;
176
177 async fn write_prepared(
179 &self,
180 request: RowInsertRequests,
181 ctx: QueryContextRef,
182 with_metric_engine: bool,
183 ) -> Result<Output>;
184
185 async fn write(
187 &self,
188 request: RowInsertRequests,
189 ctx: QueryContextRef,
190 with_metric_engine: bool,
191 ) -> Result<Output>;
192
193 async fn write_all(
195 &self,
196 requests: Vec<(QueryContextRef, RowInsertRequests)>,
197 with_metric_engine: bool,
198 ) -> Result<Vec<Result<Output>>>;
199
200 async fn read(&self, request: ReadRequest, ctx: QueryContextRef) -> Result<PromStoreResponse>;
202}
203
204#[async_trait]
205pub trait OpenTelemetryProtocolHandler: PipelineHandler {
206 async fn metrics(
209 &self,
210 request: ExportMetricsServiceRequest,
211 ctx: QueryContextRef,
212 memory_limiter: Option<(ServerMemoryLimiter, u64)>,
213 ) -> Result<MetricsIngestOutcome>;
214
215 async fn traces(
217 &self,
218 pipeline_handler: PipelineHandlerRef,
219 request: ExportTraceServiceRequest,
220 pipeline: PipelineWay,
221 pipeline_params: GreptimePipelineParams,
222 table_name: String,
223 ctx: QueryContextRef,
224 ) -> Result<TraceIngestOutcome>;
225
226 async fn logs(
227 &self,
228 pipeline_handler: PipelineHandlerRef,
229 request: ExportLogsServiceRequest,
230 pipeline: PipelineWay,
231 pipeline_params: GreptimePipelineParams,
232 table_name: String,
233 ctx: QueryContextRef,
234 ) -> Result<Vec<Output>>;
235}
236
237#[async_trait]
245pub trait PipelineHandler {
246 async fn insert(&self, input: RowInsertRequests, ctx: QueryContextRef) -> Result<Output>;
247
248 async fn insert_all(
250 &self,
251 inputs: Vec<(QueryContextRef, RowInsertRequests)>,
252 ) -> Result<Vec<Result<Output>>>;
253
254 fn check_pipeline_query_permission(&self, query_ctx: &QueryContextRef) -> Result<()>;
255
256 async fn get_pipeline(
263 &self,
264 name: &str,
265 version: PipelineVersion,
266 query_ctx: QueryContextRef,
267 ) -> Result<Arc<Pipeline>>;
268
269 async fn insert_pipeline(
270 &self,
271 name: &str,
272 content_type: &str,
273 pipeline: &str,
274 query_ctx: QueryContextRef,
275 ) -> Result<PipelineInfo>;
276
277 async fn delete_pipeline(
278 &self,
279 name: &str,
280 version: PipelineVersion,
281 query_ctx: QueryContextRef,
282 ) -> Result<Option<()>>;
283
284 async fn get_table(
285 &self,
286 table: &str,
287 query_ctx: &QueryContext,
288 ) -> std::result::Result<Option<Arc<table::Table>>, catalog::error::Error>;
289
290 fn pipeline_options(&self) -> &PipelineOptions;
292
293 fn build_pipeline(&self, pipeline: &str) -> Result<Pipeline>;
295
296 async fn get_pipeline_str(
298 &self,
299 name: &str,
300 version: PipelineVersion,
301 query_ctx: QueryContextRef,
302 ) -> Result<(String, TimestampNanosecond)>;
303}
304
305pub type DashboardHandlerRef = Arc<dyn DashboardHandler + Send + Sync>;
307
308#[async_trait]
309pub trait DashboardHandler {
310 async fn save(&self, name: &str, definition: &str, ctx: QueryContextRef) -> Result<()>;
311
312 async fn list(&self, ctx: QueryContextRef) -> Result<Vec<DashboardDefinition>>;
313
314 async fn delete(&self, name: &str, ctx: QueryContextRef) -> Result<()>;
315}
316
317#[async_trait]
319pub trait LogQueryHandler {
320 async fn query(&self, query: LogQuery, ctx: QueryContextRef) -> Result<Output>;
322
323 fn catalog_manager(&self, ctx: &QueryContext) -> Result<&dyn CatalogManager>;
325}
326
327#[async_trait]
329pub trait JaegerQueryHandler {
330 async fn get_services(&self, ctx: QueryContextRef) -> Result<Output>;
332
333 async fn get_operations(
335 &self,
336 ctx: QueryContextRef,
337 service_name: &str,
338 span_kind: Option<&str>,
339 ) -> Result<Output>;
340
341 async fn get_trace(
346 &self,
347 ctx: QueryContextRef,
348 trace_id: &str,
349 start_time: Option<i64>,
350 end_time: Option<i64>,
351 limit: Option<usize>,
352 ) -> Result<Output>;
353
354 async fn find_traces(
356 &self,
357 ctx: QueryContextRef,
358 query_params: QueryTraceParams,
359 ) -> Result<Output>;
360}
361
362#[async_trait]
368pub trait CopyInHandler: Send + Sync {
369 async fn copy_in_table(
374 &self,
375 catalog: &str,
376 schema: &str,
377 table: &str,
378 query_ctx: QueryContextRef,
379 ) -> Result<Option<TableInfoRef>>;
380
381 async fn copy_in_insert(
384 &self,
385 requests: RowInsertRequests,
386 query_ctx: QueryContextRef,
387 ) -> Result<Output>;
388}
389
390pub type CopyInHandlerRef = Arc<dyn CopyInHandler + Send + Sync>;