mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-11-28 05:34:02 +00:00
feat(otlp): bind exemplars to source metric identities
Signed-off-by: shuiyisong <xixing.sys@gmail.com>
This commit is contained in:
@@ -20,6 +20,7 @@ use std::collections::HashSet;
|
||||
use std::sync::Arc;
|
||||
|
||||
use api::v1::RowInsertRequests;
|
||||
use api::v1::value::ValueData;
|
||||
use async_trait::async_trait;
|
||||
use auth::{
|
||||
OTLP_WRITE, PermissionChecker, PermissionCheckerRef, PermissionReq, PermissionTableTarget,
|
||||
@@ -28,7 +29,9 @@ use auth::{
|
||||
use client::Output;
|
||||
use common_catalog::consts::{trace_operations_table_name, trace_services_table_name};
|
||||
use common_error::ext::BoxedError;
|
||||
use common_query::prelude::{GREPTIME_PHYSICAL_TABLE, METRICS_EXEMPLARS_TABLE};
|
||||
use common_query::prelude::{
|
||||
EXEMPLAR_METRIC_COLUMN, GREPTIME_PHYSICAL_TABLE, METRICS_EXEMPLARS_TABLE,
|
||||
};
|
||||
use common_telemetry::{tracing, warn};
|
||||
use opentelemetry_proto::tonic::collector::logs::v1::ExportLogsServiceRequest;
|
||||
use opentelemetry_proto::tonic::collector::trace::v1::ExportTraceServiceRequest;
|
||||
@@ -204,21 +207,93 @@ impl OpenTelemetryProtocolHandler for Instance {
|
||||
|
||||
if exemplar_rows > 0 {
|
||||
let confirmed = Arc::new(ctx.with_confirmed_writes());
|
||||
let (metric_output, exemplar_output) = tokio::join!(
|
||||
self.handle_otlp_metric_row_inserts(requests, confirmed.clone(), &metric_ctx),
|
||||
self.inserter.handle_exemplar_inserts(
|
||||
exemplars,
|
||||
confirmed,
|
||||
&self.statement_executor
|
||||
),
|
||||
let source_names = exemplars
|
||||
.inserts
|
||||
.iter()
|
||||
.filter_map(|request| request.rows.as_ref())
|
||||
.flat_map(|rows| {
|
||||
let index = rows
|
||||
.schema
|
||||
.iter()
|
||||
.position(|c| c.column_name == EXEMPLAR_METRIC_COLUMN);
|
||||
rows.rows.iter().filter_map(move |row| {
|
||||
match &row.values.get(index?)?.value_data {
|
||||
Some(ValueData::StringValue(name)) => Some(name.as_str()),
|
||||
_ => None,
|
||||
}
|
||||
})
|
||||
})
|
||||
.collect::<HashSet<_>>();
|
||||
let (source_requests, other): (Vec<_>, Vec<_>) =
|
||||
requests.inserts.into_iter().partition(|request| {
|
||||
metric_ctx.with_metric_engine
|
||||
&& source_names.contains(request.table_name.as_str())
|
||||
});
|
||||
let prepared = if metric_ctx.with_metric_engine {
|
||||
self.inserter
|
||||
.prepare_metric_row_inserts(
|
||||
RowInsertRequests {
|
||||
inserts: source_requests,
|
||||
},
|
||||
confirmed.clone(),
|
||||
&self.statement_executor,
|
||||
ctx.extension(PHYSICAL_TABLE_PARAM)
|
||||
.unwrap_or(GREPTIME_PHYSICAL_TABLE)
|
||||
.to_string(),
|
||||
)
|
||||
.await
|
||||
.map_err(BoxedError::new)
|
||||
.context(error::ExecuteGrpcQuerySnafu)
|
||||
} else {
|
||||
error::NotSupportedSnafu {
|
||||
feat: "exemplars require metric-engine ingestion",
|
||||
}
|
||||
.fail()
|
||||
};
|
||||
let source_ready = prepared.is_ok();
|
||||
let sources = prepared
|
||||
.as_ref()
|
||||
.map(|request| request.source_tables().cloned().collect::<Vec<_>>())
|
||||
.unwrap_or_default();
|
||||
let (metric_output, other_output, exemplar_output) = tokio::join!(
|
||||
async {
|
||||
self.inserter
|
||||
.handle_prepared_metric_inserts(prepared?, confirmed.clone())
|
||||
.await
|
||||
.map_err(BoxedError::new)
|
||||
.context(error::ExecuteGrpcQuerySnafu)
|
||||
},
|
||||
async {
|
||||
if other.is_empty() {
|
||||
Ok(Output::new_with_affected_rows(0))
|
||||
} else {
|
||||
self.handle_otlp_metric_row_inserts(
|
||||
RowInsertRequests { inserts: other },
|
||||
confirmed.clone(),
|
||||
&metric_ctx,
|
||||
)
|
||||
.await
|
||||
}
|
||||
},
|
||||
async {
|
||||
// Failed source preparation must not bind exemplars to a later replacement table.
|
||||
if !source_ready {
|
||||
return Ok(Output::new_with_affected_rows(0));
|
||||
}
|
||||
self.inserter
|
||||
.handle_exemplar_inserts(
|
||||
exemplars,
|
||||
confirmed.clone(),
|
||||
&self.statement_executor,
|
||||
&sources,
|
||||
)
|
||||
.await
|
||||
.map_err(BoxedError::new)
|
||||
.context(error::ExecuteGrpcQuerySnafu)
|
||||
},
|
||||
);
|
||||
let mut progress = common_query::WriteProgress::default();
|
||||
for output in [
|
||||
metric_output,
|
||||
exemplar_output
|
||||
.map_err(BoxedError::new)
|
||||
.context(error::ExecuteGrpcQuerySnafu),
|
||||
] {
|
||||
for output in [metric_output, other_output, exemplar_output] {
|
||||
match output {
|
||||
Ok(output) => {
|
||||
outcome.write_cost += output.meta.cost;
|
||||
|
||||
@@ -1575,6 +1575,84 @@ WITH(
|
||||
assert_eq!(results[..3], results[3..]);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn test_exemplar_otlp_source_identity() {
|
||||
use common_error::ext::ErrorExt;
|
||||
use otel_arrow_rust::proto::opentelemetry::metrics::v1::{Exemplar, exemplar};
|
||||
|
||||
for (with_metric_engine, batching) in [(false, false), (true, false), (true, true)] {
|
||||
let standalone = GreptimeDbStandaloneBuilder::new("exemplar_otlp_identity")
|
||||
.with_logical_batcher(BatcherOptions {
|
||||
protocols: vec![BatchingProtocol::Otlp],
|
||||
pending_rows_flush_interval: Duration::from_millis(5),
|
||||
..Default::default()
|
||||
})
|
||||
.build()
|
||||
.await;
|
||||
let instance = standalone.fe_instance();
|
||||
let mut ctx = QueryContext::with(DEFAULT_CATALOG_NAME, "public");
|
||||
ctx.set_logical_batching_enabled(batching);
|
||||
ctx.set_protocol_ctx(ProtocolCtx::OtlpMetric(OtlpMetricCtx {
|
||||
with_metric_engine,
|
||||
promote_all_resource_attrs: true,
|
||||
..Default::default()
|
||||
}));
|
||||
let ctx = Arc::new(ctx);
|
||||
let mut request = build_histogram_request(
|
||||
"latency",
|
||||
vec![HistogramDataPoint {
|
||||
attributes: vec![keyvalue("job", "point"), keyvalue("zone", "")],
|
||||
time_unix_nano: 10_000_000_000,
|
||||
count: 3,
|
||||
sum: Some(5.0),
|
||||
explicit_bounds: vec![1.0, 2.0],
|
||||
bucket_counts: vec![1, 1, 1],
|
||||
exemplars: [1.5, 3.0]
|
||||
.into_iter()
|
||||
.map(|value| Exemplar {
|
||||
filtered_attributes: vec![keyvalue("trace_id", "observation")],
|
||||
time_unix_nano: 9_123_456_789,
|
||||
value: Some(exemplar::Value::AsDouble(value)),
|
||||
..Default::default()
|
||||
})
|
||||
.collect(),
|
||||
..Default::default()
|
||||
}],
|
||||
);
|
||||
request.resource_metrics[0].resource = Some(Resource {
|
||||
attributes: vec![keyvalue("job", "resource"), keyvalue("service.name", "api")],
|
||||
..Default::default()
|
||||
});
|
||||
let result = instance.metrics(request, ctx.clone()).await;
|
||||
if !with_metric_engine {
|
||||
assert!(
|
||||
result
|
||||
.unwrap_err()
|
||||
.output_msg()
|
||||
.contains("require metric-engine ingestion")
|
||||
);
|
||||
assert_trace_v2_query(
|
||||
instance,
|
||||
"SELECT COUNT(*)=3 FROM latency_bucket",
|
||||
ctx.clone(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_trace_v2_query(instance, "SELECT COUNT(*)=0 FROM information_schema.tables WHERE table_name='metrics_exemplars'", ctx).await.unwrap();
|
||||
continue;
|
||||
}
|
||||
assert_eq!(result.unwrap().accepted_data_points, 1);
|
||||
assert_trace_v2_query(
|
||||
instance,
|
||||
"SELECT COUNT(*)=2 FROM metrics_exemplars",
|
||||
ctx.clone(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_trace_v2_query(instance, "SELECT COUNT(*)=2 FROM metrics_exemplars e JOIN greptime_physical_table m ON e.__table_id=m.__table_id AND e.__tsid=m.__tsid AND e.le=m.le AND e.job=m.job AND e.zone=m.zone WHERE e.greptime_metric='latency_bucket'", ctx).await.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn test_exemplar_otlp_schema_failure_isolation() {
|
||||
use otel_arrow_rust::proto::opentelemetry::metrics::v1::{Exemplar, exemplar};
|
||||
@@ -1584,7 +1662,12 @@ WITH(
|
||||
.build()
|
||||
.await;
|
||||
let instance = standalone.fe_instance();
|
||||
let ctx = QueryContext::arc();
|
||||
let mut ctx = QueryContext::with(DEFAULT_CATALOG_NAME, "public");
|
||||
ctx.set_protocol_ctx(ProtocolCtx::OtlpMetric(OtlpMetricCtx {
|
||||
with_metric_engine: true,
|
||||
..Default::default()
|
||||
}));
|
||||
let ctx = Arc::new(ctx);
|
||||
let (failed, retained) = if metric_fails {
|
||||
("exemplar_partial_total", "metrics_exemplars")
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user