servers/http/
logs.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;
16use std::time::Instant;
17
18use axum::extract::State;
19use axum::response::{IntoResponse, Response};
20use axum::{Extension, Json};
21use common_telemetry::tracing;
22use log_query::LogQuery;
23use session::context::{Channel, QueryContext};
24
25use crate::http::result::greptime_result_v1::GreptimedbV1Response;
26use crate::query_handler::LogQueryHandlerRef;
27
28#[axum_macros::debug_handler]
29#[tracing::instrument(skip_all, fields(protocol = "http", request_type = "logs"))]
30pub async fn logs(
31    State(handler): State<LogQueryHandlerRef>,
32    Extension(mut query_ctx): Extension<QueryContext>,
33    Json(params): Json<LogQuery>,
34) -> Response {
35    let exec_start = Instant::now();
36    let db = query_ctx.get_db_string();
37
38    query_ctx.set_channel(Channel::Http);
39    let query_ctx = Arc::new(query_ctx);
40
41    let _timer = crate::metrics::METRIC_HTTP_LOGS_ELAPSED
42        .with_label_values(&[db.as_str()])
43        .start_timer();
44
45    let output = handler.query(params, query_ctx).await;
46    let resp = GreptimedbV1Response::from_output(vec![output]).await;
47
48    resp.with_execution_time(exec_start.elapsed().as_millis() as u64)
49        .into_response()
50}