Skip to main content

frontend/instance/
opentsdb.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::sync::Arc;
16
17use async_trait::async_trait;
18use auth::{PermissionChecker, PermissionCheckerRef, PermissionReq};
19use common_error::ext::BoxedError;
20use common_telemetry::tracing;
21use servers::error::{self as server_error, AuthSnafu, ExecuteGrpcQuerySnafu};
22use servers::opentsdb::codec::DataPoint;
23use servers::opentsdb::data_point_to_grpc_row_insert_requests;
24use servers::query_handler::OpentsdbProtocolHandler;
25use session::context::QueryContextRef;
26use snafu::prelude::*;
27use table::requests::{SEMANTIC_SIGNAL_TYPE, SEMANTIC_SOURCE, SIGNAL_TYPE_METRIC, SOURCE_OPENTSDB};
28
29use crate::instance::Instance;
30
31#[async_trait]
32impl OpentsdbProtocolHandler for Instance {
33    #[tracing::instrument(skip_all, fields(protocol = "opentsdb"))]
34    async fn exec(
35        &self,
36        data_points: Vec<DataPoint>,
37        ctx: QueryContextRef,
38    ) -> server_error::Result<usize> {
39        self.plugins
40            .get::<PermissionCheckerRef>()
41            .as_ref()
42            .check_permission(ctx.current_user(), PermissionReq::Opentsdb)
43            .context(AuthSnafu)?;
44
45        let (requests, _) = data_point_to_grpc_row_insert_requests(data_points)?;
46
47        let ctx = {
48            let mut c = (*ctx).clone();
49            c.set_extension(SEMANTIC_SIGNAL_TYPE, SIGNAL_TYPE_METRIC);
50            c.set_extension(SEMANTIC_SOURCE, SOURCE_OPENTSDB);
51            Arc::new(c)
52        };
53
54        // OpenTSDB is single value.
55        let output = self
56            .handle_row_inserts(requests, ctx, true, true)
57            .await
58            .map_err(BoxedError::new)
59            .context(ExecuteGrpcQuerySnafu)?;
60
61        Ok(match output.data {
62            common_query::OutputData::AffectedRows(rows) => rows,
63            _ => unreachable!(),
64        })
65    }
66}