Skip to main content

quickwit_telemetry/
payload.rs

1// Copyright (C) 2021 Quickwit, Inc.
2//
3// Quickwit is offered under the AGPL v3.0 and as commercial software.
4// For commercial licensing, contact us at hello@quickwit.io.
5//
6// AGPL:
7// This program is free software: you can redistribute it and/or modify
8// it under the terms of the GNU Affero General Public License as
9// published by the Free Software Foundation, either version 3 of the
10// License, or (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU Affero General Public License for more details.
16//
17// You should have received a copy of the GNU Affero General Public License
18// along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20use std::env;
21use std::time::UNIX_EPOCH;
22
23use serde::{Deserialize, Serialize};
24use uuid::Uuid;
25
26/// Represents the payload of the request sent with telemetry requests.
27#[derive(Debug, Serialize, Deserialize)]
28pub struct TelemetryPayload {
29    /// Client information. See details in `[ClientInformation]`.
30    pub client_information: ClientInformation,
31    pub events: Vec<EventWithTimestamp>,
32    /// Represents the number of events that where drops due to the
33    /// combination of the `TELEMETRY_PUSH_COOLDOWN` and `MAX_EVENT_IN_QUEUE`.
34    pub num_dropped_events: usize,
35}
36
37#[derive(Debug, Serialize, Deserialize)]
38pub struct EventWithTimestamp {
39    /// Unix time in seconds.
40    pub unixtime: u64,
41    /// Telemetry event.
42    pub event: TelemetryEvent,
43}
44
45/// Returns the number of seconds elapsed since UNIX_EPOCH.
46///
47/// If the system clock is set before 1970, returns 0.
48fn unixtime() -> u64 {
49    match UNIX_EPOCH.elapsed() {
50        Ok(duration) => duration.as_secs(),
51        Err(_) => 0u64,
52    }
53}
54
55impl From<TelemetryEvent> for EventWithTimestamp {
56    fn from(event: TelemetryEvent) -> Self {
57        EventWithTimestamp {
58            unixtime: unixtime(),
59            event,
60        }
61    }
62}
63
64/// Represents a Telemetry Event send to Quickwit's server for usage information.
65#[derive(Debug, Serialize, Deserialize)]
66pub enum TelemetryEvent {
67    /// Create command is called.
68    Create,
69    /// Ingest command is called.
70    Ingest,
71    /// Delete command
72    Delete,
73    /// Garbage Collect command
74    GarbageCollect,
75    /// Serve command is called.
76    RunService(String),
77    /// EndCommand (with the return code)
78    EndCommand { return_code: i32 },
79}
80
81#[derive(Clone, Debug, Serialize, Deserialize)]
82pub struct ClientInformation {
83    session_uuid: uuid::Uuid,
84    quickwit_version: String,
85    os: String,
86    arch: String,
87    hashed_host_username: String,
88    kubernetes: bool,
89}
90
91fn hashed_host_username() -> String {
92    let hostname = hostname::get()
93        .map(|hostname| hostname.to_string_lossy().to_string())
94        .unwrap_or_else(|_| "".to_string());
95    let username = username::get_user_name().unwrap_or_else(|_| "".to_string());
96    let hashed_value = format!("{}:{}", hostname, username);
97    let digest = md5::compute(hashed_value.as_bytes());
98    format!("{:x}", digest)
99}
100
101impl Default for ClientInformation {
102    fn default() -> ClientInformation {
103        ClientInformation {
104            session_uuid: Uuid::new_v4(),
105            quickwit_version: env!("CARGO_PKG_VERSION").to_string(),
106            os: env::consts::OS.to_string(),
107            arch: env::consts::ARCH.to_string(),
108            hashed_host_username: hashed_host_username(),
109            kubernetes: std::env::var_os("KUBERNETES_SERVICE_HOST").is_some(),
110        }
111    }
112}