1
1
use crate :: cli_state:: journeys:: attributes:: make_host;
2
2
use crate :: cli_state:: journeys:: {
3
3
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 ,
5
6
} ;
7
+ use crate :: cli_state:: NodeInfo ;
8
+ use crate :: orchestrator:: project:: Project ;
6
9
use crate :: CliState ;
7
10
use futures:: future:: BoxFuture ;
8
11
use futures:: FutureExt ;
@@ -51,6 +54,7 @@ pub struct OckamSpanExporter<S: SpanExporter + 'static> {
51
54
exporter : Arc < Mutex < S > > ,
52
55
is_ockam_developer : bool ,
53
56
span_export_cutoff : Option < Duration > ,
57
+ span_attributes : Arc < Mutex < Option < SpanAttributes > > > ,
54
58
}
55
59
56
60
#[ async_trait]
@@ -60,12 +64,27 @@ impl<S: SpanExporter + 'static> SpanExporter for OckamSpanExporter<S> {
60
64
let is_ockam_developer = self . is_ockam_developer ;
61
65
let span_export_cutoff = self . span_export_cutoff ;
62
66
let exporter = self . exporter . clone ( ) ;
67
+ let span_attributes = self . span_attributes . clone ( ) ;
63
68
64
69
let f = async move {
65
70
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
+
66
84
exporter
67
85
. 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 ,
69
88
)
70
89
. await
71
90
}
@@ -84,7 +103,7 @@ impl<S: SpanExporter + 'static> SpanExporter for OckamSpanExporter<S> {
84
103
85
104
fn shutdown ( & mut self ) {
86
105
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 ( ) ;
88
107
exporter. shutdown ( ) ;
89
108
}
90
109
@@ -111,29 +130,29 @@ impl<S: SpanExporter> OckamSpanExporter<S> {
111
130
exporter : Arc :: new ( Mutex :: new ( exporter) ) ,
112
131
is_ockam_developer,
113
132
span_export_cutoff,
133
+ span_attributes : Arc :: new ( Mutex :: new ( None ) ) ,
114
134
}
115
135
}
116
136
117
137
async fn add_attributes (
118
- cli_state : Arc < CliState > ,
138
+ span_attributes : & SpanAttributes ,
119
139
batch : Vec < SpanData > ,
120
140
is_ockam_developer : bool ,
121
141
) -> Vec < SpanData > {
122
142
let mut result = vec ! [ ] ;
123
143
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 )
127
146
}
128
147
result
129
148
}
130
149
131
150
async fn add_attributes_to_span (
132
- cli_state : Arc < CliState > ,
151
+ span_attributes : & SpanAttributes ,
133
152
mut span : SpanData ,
134
153
is_ockam_developer : bool ,
135
154
) -> SpanData {
136
- if let Ok ( node_info) = cli_state . get_default_node ( ) . await {
155
+ if let Some ( node_info) = & span_attributes . node_info {
137
156
span. attributes . push ( KeyValue :: new (
138
157
APPLICATION_EVENT_NODE_NAME . clone ( ) ,
139
158
node_info. name ( ) ,
@@ -144,6 +163,17 @@ impl<S: SpanExporter> OckamSpanExporter<S> {
144
163
) ) ;
145
164
} ;
146
165
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
+
147
177
span. attributes . push ( KeyValue :: new (
148
178
APPLICATION_EVENT_OCKAM_DEVELOPER . clone ( ) ,
149
179
is_ockam_developer,
@@ -166,3 +196,9 @@ impl<S: SpanExporter> OckamSpanExporter<S> {
166
196
Some ( span)
167
197
}
168
198
}
199
+
200
+ #[ derive( Debug , Clone , Eq , PartialEq , Default ) ]
201
+ struct SpanAttributes {
202
+ node_info : Option < NodeInfo > ,
203
+ project : Option < Project > ,
204
+ }
0 commit comments