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};
46
47#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
48pub struct DashboardDefinition {
49 pub name: String,
50 pub definition: String,
51}
52
53use crate::error::Result;
54use crate::http::jaeger::QueryTraceParams;
55use crate::influxdb::InfluxdbRequest;
56use crate::opentsdb::codec::DataPoint;
57pub type OpentsdbProtocolHandlerRef = Arc<dyn OpentsdbProtocolHandler + Send + Sync>;
58pub type InfluxdbLineProtocolHandlerRef = Arc<dyn InfluxdbLineProtocolHandler + Send + Sync>;
59pub type PromStoreProtocolHandlerRef = Arc<dyn PromStoreProtocolHandler + Send + Sync>;
60pub type OpenTelemetryProtocolHandlerRef = Arc<dyn OpenTelemetryProtocolHandler + Send + Sync>;
61pub type PipelineHandlerRef = Arc<dyn PipelineHandler + Send + Sync>;
62pub type LogQueryHandlerRef = Arc<dyn LogQueryHandler + Send + Sync>;
63pub type JaegerQueryHandlerRef = Arc<dyn JaegerQueryHandler + Send + Sync>;
64
65#[derive(Debug, Default, Clone)]
66pub struct TraceIngestOutcome {
67 pub write_cost: usize,
68 pub accepted_spans: usize,
69 pub rejected_spans: usize,
70 pub error_message: Option<String>,
71}
72
73#[derive(Debug, Default, Clone)]
75pub struct MetricsIngestOutcome {
76 pub write_cost: usize,
77 pub accepted_data_points: i64,
78 pub rejected_data_points: i64,
79 pub error_message: Option<String>,
80}
81
82#[async_trait]
83pub trait InfluxdbLineProtocolHandler {
84 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>;
97
98 async fn exec_batch(&self, data_points: Vec<DataPoint>, ctx: QueryContextRef) -> Result<usize> {
102 self.exec(data_points, ctx).await
103 }
104}
105
106pub struct PromStoreResponse {
107 pub content_type: HeaderValue,
108 pub content_encoding: HeaderValue,
109 pub resp_metrics: HashMap<String, Value>,
110 pub body: Vec<u8>,
111}
112
113#[async_trait]
114pub trait PromStoreProtocolHandler {
115 async fn pre_write(&self, request: &RowInsertRequests, ctx: QueryContextRef) -> Result<()>;
117
118 async fn write_prepared(
120 &self,
121 request: RowInsertRequests,
122 ctx: QueryContextRef,
123 with_metric_engine: bool,
124 ) -> Result<Output>;
125
126 async fn write(
128 &self,
129 request: RowInsertRequests,
130 ctx: QueryContextRef,
131 with_metric_engine: bool,
132 ) -> Result<Output>;
133
134 async fn write_all(
136 &self,
137 requests: Vec<(QueryContextRef, RowInsertRequests)>,
138 with_metric_engine: bool,
139 ) -> Result<Vec<Result<Output>>>;
140
141 async fn read(&self, request: ReadRequest, ctx: QueryContextRef) -> Result<PromStoreResponse>;
143}
144
145#[async_trait]
146pub trait OpenTelemetryProtocolHandler: PipelineHandler {
147 async fn metrics(
149 &self,
150 request: ExportMetricsServiceRequest,
151 ctx: QueryContextRef,
152 ) -> Result<MetricsIngestOutcome>;
153
154 async fn traces(
156 &self,
157 pipeline_handler: PipelineHandlerRef,
158 request: ExportTraceServiceRequest,
159 pipeline: PipelineWay,
160 pipeline_params: GreptimePipelineParams,
161 table_name: String,
162 ctx: QueryContextRef,
163 ) -> Result<TraceIngestOutcome>;
164
165 async fn logs(
166 &self,
167 pipeline_handler: PipelineHandlerRef,
168 request: ExportLogsServiceRequest,
169 pipeline: PipelineWay,
170 pipeline_params: GreptimePipelineParams,
171 table_name: String,
172 ctx: QueryContextRef,
173 ) -> Result<Vec<Output>>;
174}
175
176#[async_trait]
184pub trait PipelineHandler {
185 async fn insert(&self, input: RowInsertRequests, ctx: QueryContextRef) -> Result<Output>;
186
187 async fn insert_all(
189 &self,
190 inputs: Vec<(QueryContextRef, RowInsertRequests)>,
191 ) -> Result<Vec<Result<Output>>>;
192
193 fn check_pipeline_query_permission(&self, query_ctx: &QueryContextRef) -> Result<()>;
194
195 async fn get_pipeline(
202 &self,
203 name: &str,
204 version: PipelineVersion,
205 query_ctx: QueryContextRef,
206 ) -> Result<Arc<Pipeline>>;
207
208 async fn insert_pipeline(
209 &self,
210 name: &str,
211 content_type: &str,
212 pipeline: &str,
213 query_ctx: QueryContextRef,
214 ) -> Result<PipelineInfo>;
215
216 async fn delete_pipeline(
217 &self,
218 name: &str,
219 version: PipelineVersion,
220 query_ctx: QueryContextRef,
221 ) -> Result<Option<()>>;
222
223 async fn get_table(
224 &self,
225 table: &str,
226 query_ctx: &QueryContext,
227 ) -> std::result::Result<Option<Arc<table::Table>>, catalog::error::Error>;
228
229 fn build_pipeline(&self, pipeline: &str) -> Result<Pipeline>;
231
232 async fn get_pipeline_str(
234 &self,
235 name: &str,
236 version: PipelineVersion,
237 query_ctx: QueryContextRef,
238 ) -> Result<(String, TimestampNanosecond)>;
239}
240
241pub type DashboardHandlerRef = Arc<dyn DashboardHandler + Send + Sync>;
243
244#[async_trait]
245pub trait DashboardHandler {
246 async fn save(&self, name: &str, definition: &str, ctx: QueryContextRef) -> Result<()>;
247
248 async fn list(&self, ctx: QueryContextRef) -> Result<Vec<DashboardDefinition>>;
249
250 async fn delete(&self, name: &str, ctx: QueryContextRef) -> Result<()>;
251}
252
253#[async_trait]
255pub trait LogQueryHandler {
256 async fn query(&self, query: LogQuery, ctx: QueryContextRef) -> Result<Output>;
258
259 fn catalog_manager(&self, ctx: &QueryContext) -> Result<&dyn CatalogManager>;
261}
262
263#[async_trait]
265pub trait JaegerQueryHandler {
266 async fn get_services(&self, ctx: QueryContextRef) -> Result<Output>;
268
269 async fn get_operations(
271 &self,
272 ctx: QueryContextRef,
273 service_name: &str,
274 span_kind: Option<&str>,
275 ) -> Result<Output>;
276
277 async fn get_trace(
282 &self,
283 ctx: QueryContextRef,
284 trace_id: &str,
285 start_time: Option<i64>,
286 end_time: Option<i64>,
287 limit: Option<usize>,
288 ) -> Result<Output>;
289
290 async fn find_traces(
292 &self,
293 ctx: QueryContextRef,
294 query_params: QueryTraceParams,
295 ) -> Result<Output>;
296}