1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use opcua_types::DateTime;
use crate::{
comms::transport::{Transport, TransportState},
config,
diagnostics::ServerDiagnostics,
server,
state::ServerState,
subscriptions::subscriptions::{self},
};
#[derive(Serialize)]
pub struct ServerMetrics {
pub server: Server,
pub diagnostics: ServerDiagnostics,
pub config: Option<config::ServerConfig>,
pub connections: Vec<Connection>,
pub runtime_components: Vec<String>,
}
#[derive(Serialize)]
pub struct Server {
pub start_time: String,
pub uptime_ms: i64,
}
#[derive(Serialize)]
pub struct Connection {
pub id: String,
pub client_address: String,
pub transport_state: String,
pub session_activated: bool,
pub session_terminated: bool,
pub session_terminated_at: String,
pub subscriptions: subscriptions::Metrics,
}
impl ServerMetrics {
pub fn new() -> ServerMetrics {
ServerMetrics {
server: Server {
start_time: String::new(),
uptime_ms: 0,
},
diagnostics: ServerDiagnostics::default(),
config: None,
connections: Vec::new(),
runtime_components: Vec::new(),
}
}
pub fn set_server_info(&mut self, server: &server::Server) {
let server_state = server.server_state();
let config = {
let server_state = trace_read_lock_unwrap!(server_state);
server_state.config.clone()
};
let mut config = {
let config = trace_read_lock_unwrap!(config);
config.clone()
};
config.user_tokens.clear();
config.user_tokens.insert(String::new(), config::ServerUserToken {
user: String::from("User identity tokens have been removed"),
pass: None,
x509: None,
thumbprint: None,
});
self.config = Some(config.clone());
}
pub fn update_from_server_state(&mut self, server_state: &ServerState) {
let start_time = &server_state.start_time;
let now = DateTime::now();
self.server.start_time = start_time.as_chrono().to_rfc3339();
{
let diagnostics = trace_read_lock_unwrap!(server_state.diagnostics);
self.diagnostics = diagnostics.clone();
}
let elapsed = now.as_chrono().signed_duration_since(start_time.as_chrono());
self.server.uptime_ms = elapsed.num_milliseconds();
}
pub fn update_from_connections(&mut self, connections: server::Connections) {
self.runtime_components = runtime_components!();
self.connections = connections.iter().map(|c| {
let (client_address, transport_state, session) = {
let connection = trace_read_lock_unwrap!(c);
let client_address = if let Some(ref client_address) = connection.client_address() {
format!("{:?}", client_address)
} else {
String::new()
};
let transport_state = match connection.state() {
TransportState::New => "New".to_string(),
TransportState::WaitingHello => "WaitingHello".to_string(),
TransportState::ProcessMessages => "ProcessMessages".to_string(),
TransportState::Finished(status_code) => format!("Finished({})", status_code)
};
(client_address, transport_state, connection.session())
};
let (id, session_activated, session_terminated, session_terminated_at, subscriptions) = {
let session = trace_read_lock_unwrap!(session);
let id = session.session_id().to_string();
let session_activated = session.is_activated();
let session_terminated = session.is_terminated();
let session_terminated_at = if session.is_terminated() {
session.terminated_at().to_rfc3339()
} else {
String::new()
};
let subscriptions = session.subscriptions().metrics();
(id, session_activated, session_terminated, session_terminated_at, subscriptions)
};
Connection {
id,
client_address,
transport_state,
session_activated,
session_terminated,
session_terminated_at,
subscriptions,
}
}).collect();
}
}