Skip to content

Commit 91db103

Browse files
authored
feat(cubestore): Metrics - track command for data/cache/queue (#7430)
1 parent f7859ca commit 91db103

File tree

4 files changed

+102
-5
lines changed

4 files changed

+102
-5
lines changed

rust/cubestore/cubestore/src/sql/cachestore.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::sql::parser::{
99
use crate::sql::{QueryPlans, SqlQueryContext, SqlService};
1010
use crate::store::DataFrame;
1111
use crate::table::{Row, TableValue};
12+
use crate::util::metrics;
1213
use crate::{app_metrics, CubeError};
1314
use async_trait::async_trait;
1415
use datafusion::sql::parser::Statement as DFStatement;
@@ -174,7 +175,13 @@ impl CacheStoreSqlService {
174175
_context: SqlQueryContext,
175176
command: CacheCommand,
176177
) -> Result<Arc<DataFrame>, CubeError> {
177-
app_metrics::CACHE_QUERIES.increment();
178+
app_metrics::CACHE_QUERIES.add_with_tags(
179+
1,
180+
Some(&vec![metrics::format_tag(
181+
"command",
182+
command.as_tag_command(),
183+
)]),
184+
);
178185
let execution_time = SystemTime::now();
179186

180187
let (result, track_time) = match command {
@@ -268,7 +275,13 @@ impl CacheStoreSqlService {
268275
_context: SqlQueryContext,
269276
command: QueueCommand,
270277
) -> Result<Arc<DataFrame>, CubeError> {
271-
app_metrics::QUEUE_QUERIES.increment();
278+
app_metrics::QUEUE_QUERIES.add_with_tags(
279+
1,
280+
Some(&vec![metrics::format_tag(
281+
"command",
282+
command.as_tag_command(),
283+
)]),
284+
);
272285
let execution_time = SystemTime::now();
273286

274287
let (result, track_time) = match command {

rust/cubestore/cubestore/src/sql/mod.rs

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ pub mod cachestore;
8181
pub mod parser;
8282

8383
use crate::sql::cachestore::CacheStoreSqlService;
84+
use crate::util::metrics;
8485
use mockall::automock;
8586

8687
#[automock]
@@ -944,6 +945,11 @@ impl SqlService for SqlServiceImpl {
944945
schema_name,
945946
if_not_exists,
946947
} => {
948+
app_metrics::DATA_QUERIES.add_with_tags(
949+
1,
950+
Some(&vec![metrics::format_tag("command", "create_schema")]),
951+
);
952+
947953
let name = schema_name.to_string();
948954
let res = self.create_schema(name, if_not_exists).await?;
949955
Ok(Arc::new(DataFrame::from(vec![res])))
@@ -963,6 +969,11 @@ impl SqlService for SqlServiceImpl {
963969
unique_key,
964970
partitioned_index,
965971
} => {
972+
app_metrics::DATA_QUERIES.add_with_tags(
973+
1,
974+
Some(&vec![metrics::format_tag("command", "create_table")]),
975+
);
976+
966977
let nv = &name.0;
967978
if nv.len() != 2 {
968979
return Err(CubeError::user(format!(
@@ -1127,6 +1138,11 @@ impl SqlService for SqlServiceImpl {
11271138
columns,
11281139
..
11291140
}) => {
1141+
app_metrics::DATA_QUERIES.add_with_tags(
1142+
1,
1143+
Some(&vec![metrics::format_tag("command", "create_index")]),
1144+
);
1145+
11301146
if table_name.0.len() != 2 {
11311147
return Err(CubeError::user(format!(
11321148
"Schema's name should be present in table name but found: {}",
@@ -1163,6 +1179,11 @@ impl SqlService for SqlServiceImpl {
11631179
credentials,
11641180
or_update,
11651181
} => {
1182+
app_metrics::DATA_QUERIES.add_with_tags(
1183+
1,
1184+
Some(&vec![metrics::format_tag("command", "create_source")]),
1185+
);
1186+
11661187
if or_update {
11671188
let creds = match source_type.as_str() {
11681189
"ksql" => {
@@ -1209,6 +1230,14 @@ impl SqlService for SqlServiceImpl {
12091230
columns,
12101231
if_not_exists,
12111232
}) => {
1233+
app_metrics::DATA_QUERIES.add_with_tags(
1234+
1,
1235+
Some(&vec![metrics::format_tag(
1236+
"command",
1237+
"create_partitioned_index",
1238+
)]),
1239+
);
1240+
12121241
if name.0.len() != 2 {
12131242
return Err(CubeError::user(format!(
12141243
"Expected name for PARTITIONED INDEX in the form '<SCHEMA>.<INDEX>', found: {}",
@@ -1230,24 +1259,31 @@ impl SqlService for SqlServiceImpl {
12301259
CubeStoreStatement::Statement(Statement::Drop {
12311260
object_type, names, ..
12321261
}) => {
1233-
match object_type {
1262+
let command = match object_type {
12341263
ObjectType::Schema => {
12351264
self.db.delete_schema(names[0].to_string()).await?;
1265+
&"drop_schema"
12361266
}
12371267
ObjectType::Table => {
12381268
let table = self
12391269
.db
12401270
.get_table(names[0].0[0].to_string(), names[0].0[1].to_string())
12411271
.await?;
12421272
self.db.drop_table(table.get_id()).await?;
1273+
&"drop_table"
12431274
}
12441275
ObjectType::PartitionedIndex => {
12451276
let schema = names[0].0[0].value.clone();
12461277
let name = names[0].0[1].value.clone();
12471278
self.db.drop_partitioned_index(schema, name).await?;
1279+
&"drop_partitioned_index"
12481280
}
12491281
_ => return Err(CubeError::user("Unsupported drop operation".to_string())),
1250-
}
1282+
};
1283+
1284+
app_metrics::DATA_QUERIES
1285+
.add_with_tags(1, Some(&vec![metrics::format_tag("command", command)]));
1286+
12511287
Ok(Arc::new(DataFrame::new(vec![], vec![])))
12521288
}
12531289
CubeStoreStatement::Statement(Statement::Insert {
@@ -1256,6 +1292,9 @@ impl SqlService for SqlServiceImpl {
12561292
source,
12571293
..
12581294
}) => {
1295+
app_metrics::DATA_QUERIES
1296+
.add_with_tags(1, Some(&vec![metrics::format_tag("command", "insert")]));
1297+
12591298
let data = if let SetExpr::Values(Values(data_series)) = &source.body {
12601299
data_series
12611300
} else {
@@ -1295,14 +1334,19 @@ impl SqlService for SqlServiceImpl {
12951334
context.trace_obj.clone(),
12961335
)
12971336
.await?;
1337+
12981338
// TODO distribute and combine
12991339
let res = match logical_plan {
13001340
QueryPlan::Meta(logical_plan) => {
13011341
app_metrics::META_QUERIES.increment();
13021342
Arc::new(self.query_planner.execute_meta_plan(logical_plan).await?)
13031343
}
13041344
QueryPlan::Select(serialized, workers) => {
1305-
app_metrics::DATA_QUERIES.increment();
1345+
app_metrics::DATA_QUERIES.add_with_tags(
1346+
1,
1347+
Some(&vec![metrics::format_tag("command", "select")]),
1348+
);
1349+
13061350
let cluster = self.cluster.clone();
13071351
let executor = self.query_executor.clone();
13081352
timeout(

rust/cubestore/cubestore/src/sql/parser.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,19 @@ pub enum CacheCommand {
9191
},
9292
}
9393

94+
impl CacheCommand {
95+
pub fn as_tag_command(&self) -> &'static str {
96+
match self {
97+
CacheCommand::Set { .. } => "set",
98+
CacheCommand::Get { .. } => "get",
99+
CacheCommand::Keys { .. } => "keys",
100+
CacheCommand::Remove { .. } => "remove",
101+
CacheCommand::Truncate { .. } => "truncate",
102+
CacheCommand::Incr { .. } => "incr",
103+
}
104+
}
105+
}
106+
94107
#[derive(Debug, Clone, PartialEq)]
95108
pub enum QueueCommand {
96109
Add {
@@ -142,6 +155,29 @@ pub enum QueueCommand {
142155
Truncate {},
143156
}
144157

158+
impl QueueCommand {
159+
pub fn as_tag_command(&self) -> &'static str {
160+
match self {
161+
QueueCommand::Add { .. } => "add",
162+
QueueCommand::Get { .. } => "get",
163+
QueueCommand::ToCancel { .. } => "to_cancel",
164+
QueueCommand::List { status_filter, .. } => match status_filter {
165+
Some(QueueItemStatus::Active) => "active",
166+
Some(QueueItemStatus::Pending) => "pending",
167+
_ => "list",
168+
},
169+
QueueCommand::Cancel { .. } => "cancel",
170+
QueueCommand::Heartbeat { .. } => "heartbeat",
171+
QueueCommand::Ack { .. } => "ack",
172+
QueueCommand::MergeExtra { .. } => "merge_extra",
173+
QueueCommand::Retrieve { .. } => "retrieve",
174+
QueueCommand::Result { .. } => "result",
175+
QueueCommand::ResultBlocking { .. } => "result_blocking",
176+
QueueCommand::Truncate { .. } => "truncate",
177+
}
178+
}
179+
}
180+
145181
#[derive(Debug, Clone, PartialEq)]
146182
pub enum SystemCommand {
147183
KillAllJobs,

rust/cubestore/cubestore/src/util/metrics.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ pub const fn distribution(name: &'static str) -> IntMetric {
6161
}
6262
}
6363

64+
pub fn format_tag(name: &'static str, value: &str) -> String {
65+
format!("{}:{}", name, value)
66+
}
67+
6468
pub struct Counter {
6569
metric: Metric,
6670
}

0 commit comments

Comments
 (0)