clickhouse_arrow/
telemetry.rs

1//! ## Tracing (telemetry) utilities and constants.
2//!
3//! `clickhouse-arrow` uses the `tracing` crate to emit spans and events for debugging and
4//! monitoring. To enable tracing, add a subscriber in your application:
5//!
6//! ```rust
7//! use tracing_subscriber;
8//!
9//! tracing_subscriber::fmt()
10//!     .with_env_filter("clickhouse_arrow=debug")
11//!     .init();
12//! // Use clickhouse_arrow
13//! ```
14use std::num::NonZeroU64;
15
16pub use opentelemetry_semantic_conventions::*;
17use tracing::Span;
18
19/// Commonly used attribute names
20pub const ATT_CID: &str = "clickhouse.client.id";
21pub const ATT_CON: &str = "clickhouse.connection.id";
22pub const ATT_CREQ: &str = "clickhouse.client.request";
23pub const ATT_QID: &str = "clickhouse.query.id";
24pub const ATT_PCOUNT: &str = "clickhouse.packet.count";
25pub const ATT_PID: &str = "clickhouse.packet.id";
26pub const ATT_MSGTYPE: &str = "clickhouse.message.type";
27pub const ATT_FIELD_NAME: &str = "clickhouse.field.name";
28pub const ATT_FIELD_TYPE: &str = "clickhouse.field.type";
29
30/// A helper to link spans to various actions, namely connection. Sometimes, clients are spawned on
31/// separate tasks. This provides a simple way to link traces if a link is preferred in some
32/// situations over other types of instrumentation.
33#[derive(Clone, Copy, Default, Debug, PartialEq)]
34pub struct TraceContext(Option<NonZeroU64>);
35
36impl TraceContext {
37    pub(super) fn link(&self, span: &Span) -> &Self {
38        let _ = span.follows_from(self.get_id());
39        self
40    }
41
42    pub(super) fn get_id(self) -> Option<tracing::span::Id> {
43        self.0.map(tracing::span::Id::from_non_zero_u64)
44    }
45}
46
47impl From<NonZeroU64> for TraceContext {
48    fn from(id: NonZeroU64) -> Self { Self(Some(id)) }
49}
50
51impl From<Option<NonZeroU64>> for TraceContext {
52    fn from(id: Option<NonZeroU64>) -> Self { Self(id) }
53}