Skip to content

Commit d574e9d

Browse files
committed
feat(rust): store attribute data in-memory
rather than getting from the database all the time
1 parent 0845231 commit d574e9d

File tree

4 files changed

+48
-10
lines changed

4 files changed

+48
-10
lines changed

implementations/rust/ockam/ockam_api/src/logs/span_exporters.rs

+45-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
use crate::cli_state::journeys::attributes::make_host;
22
use crate::cli_state::journeys::{
33
APPLICATION_EVENT_HOST, APPLICATION_EVENT_NODE_IDENTIFIER, APPLICATION_EVENT_NODE_NAME,
4-
APPLICATION_EVENT_OCKAM_DEVELOPER,
4+
APPLICATION_EVENT_OCKAM_DEVELOPER, APPLICATION_EVENT_PROJECT_ID,
5+
APPLICATION_EVENT_PROJECT_NAME,
56
};
7+
use crate::cli_state::NodeInfo;
8+
use crate::orchestrator::project::Project;
69
use crate::CliState;
710
use futures::future::BoxFuture;
811
use futures::FutureExt;
@@ -51,6 +54,7 @@ pub struct OckamSpanExporter<S: SpanExporter + 'static> {
5154
exporter: Arc<Mutex<S>>,
5255
is_ockam_developer: bool,
5356
span_export_cutoff: Option<Duration>,
57+
span_attributes: Arc<Mutex<Option<SpanAttributes>>>,
5458
}
5559

5660
#[async_trait]
@@ -60,12 +64,27 @@ impl<S: SpanExporter + 'static> SpanExporter for OckamSpanExporter<S> {
6064
let is_ockam_developer = self.is_ockam_developer;
6165
let span_export_cutoff = self.span_export_cutoff;
6266
let exporter = self.exporter.clone();
67+
let span_attributes = self.span_attributes.clone();
6368

6469
let f = async move {
6570
let mut exporter = exporter.lock().await;
71+
72+
// initialize span attributes from local data if they haven't been initialized yet.
73+
let mut span_attributes = span_attributes.lock().await;
74+
let attributes = if span_attributes.is_none() {
75+
SpanAttributes {
76+
node_info: cli_state.get_default_node().await.ok(),
77+
project: cli_state.projects().get_default_project().await.ok(),
78+
}
79+
} else {
80+
SpanAttributes::default()
81+
};
82+
*span_attributes = Some(attributes.clone());
83+
6684
exporter
6785
.export(
68-
Self::add_attributes(cli_state, Self::filter(batch), is_ockam_developer).await,
86+
Self::add_attributes(&attributes, Self::filter(batch), is_ockam_developer)
87+
.await,
6988
)
7089
.await
7190
}
@@ -84,7 +103,7 @@ impl<S: SpanExporter + 'static> SpanExporter for OckamSpanExporter<S> {
84103

85104
fn shutdown(&mut self) {
86105
debug!("shutting down the span exporter");
87-
let mut exporter = self.exporter.blocking_lock(); // Use blocking_lock() to acquire a lock synchronously
106+
let mut exporter = self.exporter.blocking_lock();
88107
exporter.shutdown();
89108
}
90109

@@ -111,29 +130,29 @@ impl<S: SpanExporter> OckamSpanExporter<S> {
111130
exporter: Arc::new(Mutex::new(exporter)),
112131
is_ockam_developer,
113132
span_export_cutoff,
133+
span_attributes: Arc::new(Mutex::new(None)),
114134
}
115135
}
116136

117137
async fn add_attributes(
118-
cli_state: Arc<CliState>,
138+
span_attributes: &SpanAttributes,
119139
batch: Vec<SpanData>,
120140
is_ockam_developer: bool,
121141
) -> Vec<SpanData> {
122142
let mut result = vec![];
123143
for span in batch.into_iter() {
124-
result.push(
125-
Self::add_attributes_to_span(cli_state.clone(), span, is_ockam_developer).await,
126-
)
144+
result
145+
.push(Self::add_attributes_to_span(span_attributes, span, is_ockam_developer).await)
127146
}
128147
result
129148
}
130149

131150
async fn add_attributes_to_span(
132-
cli_state: Arc<CliState>,
151+
span_attributes: &SpanAttributes,
133152
mut span: SpanData,
134153
is_ockam_developer: bool,
135154
) -> SpanData {
136-
if let Ok(node_info) = cli_state.get_default_node().await {
155+
if let Some(node_info) = &span_attributes.node_info {
137156
span.attributes.push(KeyValue::new(
138157
APPLICATION_EVENT_NODE_NAME.clone(),
139158
node_info.name(),
@@ -144,6 +163,17 @@ impl<S: SpanExporter> OckamSpanExporter<S> {
144163
));
145164
};
146165

166+
if let Some(project) = &span_attributes.project {
167+
span.attributes.push(KeyValue::new(
168+
APPLICATION_EVENT_PROJECT_ID.clone(),
169+
project.project_id().to_string(),
170+
));
171+
span.attributes.push(KeyValue::new(
172+
APPLICATION_EVENT_PROJECT_NAME.clone(),
173+
project.name().to_string(),
174+
));
175+
};
176+
147177
span.attributes.push(KeyValue::new(
148178
APPLICATION_EVENT_OCKAM_DEVELOPER.clone(),
149179
is_ockam_developer,
@@ -166,3 +196,9 @@ impl<S: SpanExporter> OckamSpanExporter<S> {
166196
Some(span)
167197
}
168198
}
199+
200+
#[derive(Debug, Clone, Eq, PartialEq, Default)]
201+
struct SpanAttributes {
202+
node_info: Option<NodeInfo>,
203+
project: Option<Project>,
204+
}

implementations/rust/ockam/ockam_api/src/orchestrator/project/project.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use ockam_core::{Error, Result};
1818
use ockam_multiaddr::MultiAddr;
1919
use ockam_node::tokio;
2020

21-
#[derive(Debug, Clone, Serialize)]
21+
#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
2222
pub struct Project {
2323
#[serde(flatten)]
2424
model: ProjectModel,

implementations/rust/ockam/ockam_node/src/context/context.rs

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use core::fmt::{Debug, Formatter};
1818
use ockam_core::compat::sync::Weak;
1919
use ockam_core::errcode::{Kind, Origin};
2020
use ockam_transport_core::Transport;
21+
#[cfg(feature = "std")]
2122
use opentelemetry::trace::{Span, TraceContextExt};
2223

2324
/// A default timeout in seconds

implementations/rust/ockam/ockam_transport_tcp/src/privileged_portal/privileged_portals.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use std::net::IpAddr;
1515
use tokio::net::TcpListener;
1616
use tokio::sync::mpsc::channel;
1717
use tracing::instrument;
18+
use tracing::Level;
1819

1920
impl TcpTransport {
2021
/// Check if privileged portals can be run with current permissions

0 commit comments

Comments
 (0)