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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//! This is my awesome crate Enabling system metrics from process to be observed using opentelemetry.
//! Current metrics observed are:
//! - CPU
//! - Memory
//! - Disk
//! - Network
//!
//!
//! # Getting started
//!
//! ```
//! use opentelemetry::global;
//! use opentelemetry_system_metrics::init_process_observer;
//!
//! let meter = global::meter("process-meter");
//! init_process_observer(meter);
//! ```
//!
use opentelemetry::metrics::Unit;
use std::sync::Arc;
use std::sync::Mutex;
use sysinfo::PidExt;

use sysinfo::ProcessExt;
use sysinfo::SystemExt;
use sysinfo::{get_current_pid, System};

use opentelemetry::metrics::{BatchObserverResult, Meter};
use opentelemetry::Key;

const PROCESS_PID: Key = Key::from_static_str("process.pid");
const PROCESS_EXECUTABLE_NAME: Key = Key::from_static_str("process.executable.name");
const PROCESS_EXECUTABLE_PATH: Key = Key::from_static_str("process.executable.path");
const PROCESS_COMMAND: Key = Key::from_static_str("process.command");

// Not implemented yet!
//
// const PROCESS_COMMAND_LINE: Key = Key::from_static_str("process.command_line");
// const PROCESS_COMMAND_ARGS: Key = Key::from_static_str("process.command_args");
// const PROCESS_OWNER: Key = Key::from_static_str("process.owner");

const PROCESS_CPU_USAGE: &str = "process.cpu.usage";
const PROCESS_CPU_UTILIZATION: &str = "process.cpu.utilization";
const PROCESS_MEMORY_USAGE: &str = "process.memory.usage";
const PROCESS_MEMORY_VIRTUAL: &str = "process.memory.virtual";
const PROCESS_DISK_IO: &str = "process.disk.io";
// const PROCESS_NETWORK_IO: &str = "process.network.io";
const DIRECTION: Key = Key::from_static_str("direction");

// Record asynchronnously information about the current process.
// # Example
//
// ```
// use opentelemetry::global;
// use opentelemetry_system_metrics::init_process_observer;
//
// let meter = global::meter("process-meter");
// init_process_observer(meter);
// ```
//
pub fn init_process_observer(meter: Meter) {
    let sys = Arc::new(Mutex::new(System::new_all()));
    let mut sys_lock = sys.lock().unwrap();
    sys_lock.refresh_all();

    let pid = get_current_pid().unwrap();
    let core_count = sys_lock.physical_core_count().unwrap();

    meter
        .build_batch_observer(|batch| {
            let process_cpu_utilization = batch
                .f64_value_observer(PROCESS_CPU_USAGE)
                .with_description("The percentage of CPU in use.")
                .init();
            let process_cpu_usage = batch.f64_value_observer(PROCESS_CPU_UTILIZATION).init();
            let process_memory_usage = batch
                .i64_value_observer(PROCESS_MEMORY_USAGE)
                .with_description("The amount of physical memory in use.")
                .with_unit(Unit::new("byte"))
                .init();
            let process_memory_virtual = batch
                .i64_value_observer(PROCESS_MEMORY_VIRTUAL)
                .with_description("The amount of committed virtual memory.")
                .with_unit(Unit::new("byte"))
                .init();
            let process_disk_io = batch
                .i64_value_observer(PROCESS_DISK_IO)
                .with_description("Disk bytes transferred.")
                .with_unit(Unit::new("byte"))
                .init();
            // let process_network_io = batch
            //     .i64_value_observer(PROCESS_NETWORK_IO)
            //     .with_description("All network bytes transferred.")
            //     .with_unit(Unit::new("byte"))
            //     .init();

            let sys = sys.clone();

            let common_attributes = if let Some(process) = sys_lock.process(pid) {
                [
                    PROCESS_PID.i64(pid.as_u32().into()),
                    PROCESS_EXECUTABLE_NAME.string(process.name().to_string()),
                    PROCESS_EXECUTABLE_PATH.string(process.exe().to_str().unwrap().to_string()),
                    PROCESS_COMMAND.string(process.cmd().join(" ").to_string()),
                ]
            } else {
                unimplemented!()
            };

            Ok(move |result: BatchObserverResult| {
                let mut sys_lock = sys.lock().unwrap();

                sys_lock.refresh_process(pid);

                if let Some(process) = sys_lock.process(pid) {
                    let cpu_usage = process.cpu_usage() / 100.;
                    let disk_io = process.disk_usage();
                    // let network_io = process.network_usage();
                    result.observe(&[], &[process_cpu_usage.observation(cpu_usage.into())]);
                    result.observe(
                        &common_attributes,
                        &[process_cpu_utilization
                            .observation((cpu_usage / core_count as f32).into())],
                    );
                    result.observe(
                        &common_attributes,
                        &[process_memory_usage
                            .observation((process.memory() * 1_000).try_into().unwrap())],
                    );
                    result.observe(
                        &common_attributes,
                        &[process_memory_virtual
                            .observation((process.virtual_memory() * 1_000).try_into().unwrap())],
                    );
                    result.observe(
                        &[common_attributes.as_slice(), &[DIRECTION.string("read")]].concat(),
                        &[process_disk_io.observation(disk_io.read_bytes.try_into().unwrap())],
                    );
                    result.observe(
                        &[common_attributes.as_slice(), &[DIRECTION.string("write")]].concat(),
                        &[process_disk_io.observation(disk_io.written_bytes.try_into().unwrap())],
                    );
                    // result.observe(
                    //     &[common_attributes.as_slice(), &[DIRECTION.string("receive")]].concat(),
                    //     &[process_network_io
                    //         .observation(network_io.received_bytes.try_into().unwrap())],
                    // );
                    // result.observe(
                    //     &[
                    //         common_attributes.as_slice(),
                    //         &[DIRECTION.string("transmit")],
                    //     ]
                    //     .concat(),
                    //     &[process_network_io
                    //         .observation(network_io.transmitted_bytes.try_into().unwrap())],
                    // );
                }
            })
        })
        .unwrap();
}