Skip to content

Commit

Permalink
feat: introduce synthetics in backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Subhra264 committed May 8, 2024
1 parent c3ec7eb commit 3a7a000
Show file tree
Hide file tree
Showing 13 changed files with 934 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/common/infra/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use crate::{
organization::OrganizationSetting,
pipelines::PipeLine,
prom::ClusterLeader,
synthetics,
syslog::SyslogRoute,
user::User,
},
Expand Down Expand Up @@ -70,6 +71,8 @@ pub static ALERTS_DESTINATIONS: Lazy<RwHashMap<String, alerts::destinations::Des
Lazy::new(Default::default);
pub static DASHBOARD_REPORTS: Lazy<RwHashMap<String, reports::Report>> =
Lazy::new(Default::default);
pub static SYNTHETICS: Lazy<RwHashMap<String, synthetics::Synthetics>> =
Lazy::new(Default::default);
pub static SYSLOG_ROUTES: Lazy<RwHashMap<String, SyslogRoute>> = Lazy::new(Default::default);
pub static SYSLOG_ENABLED: Lazy<Arc<RwLock<bool>>> = Lazy::new(|| Arc::new(RwLock::new(false)));
pub static ENRICHMENT_TABLES: Lazy<RwHashMap<String, StreamTable>> = Lazy::new(Default::default);
Expand Down
1 change: 1 addition & 0 deletions src/common/meta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub mod proxy;
pub mod saved_view;
pub mod service;
pub mod stream;
pub mod synthetics;
pub mod syslog;
pub mod telemetry;
pub mod traces;
Expand Down
215 changes: 215 additions & 0 deletions src/common/meta/synthetics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
// Copyright 2023 Zinc Labs Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use chrono::{DateTime, FixedOffset};
use hashbrown::HashMap;
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;

use super::{
alerts::{destinations::HTTPType, Operator},
dashboards::datetime_now,
};

#[derive(Serialize, Debug, Default, Deserialize, PartialEq, Clone, ToSchema)]
pub enum SyntheticsAlertType {
#[serde(rename = "email")]
#[default]
Email,
}

#[derive(Serialize, Debug, Default, Deserialize, PartialEq, Clone, ToSchema)]
pub enum SyntheticsFrequencyType {
#[serde(rename = "once")]
Once,
#[serde(rename = "seconds")]
Seconds,
#[serde(rename = "minutes")]
Minutes,
#[serde(rename = "hours")]
#[default]
Hours,
#[serde(rename = "days")]
Days,
#[serde(rename = "cron")]
Cron,
}

#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
pub struct SyntheticsFrequency {
/// Frequency interval in the `frequency_type` unit
#[serde(default)]
pub interval: i64,
/// Cron expression
#[serde(default)]
pub cron: String,
#[serde(rename = "type")]
#[serde(default)]
pub frequency_type: SyntheticsFrequencyType,
/// Start time of report generation in UNIX microseconds.
pub start: i64,
#[serde(default)]
#[serde(rename = "timezoneOffset")]
pub timezone_offset: i64,
}

impl Default for SyntheticsFrequency {
fn default() -> Self {
Self {
interval: 1,
cron: "".to_string(),
frequency_type: Default::default(),
start: 0,
timezone_offset: 0,
}
}
}

#[derive(Clone, Debug, Serialize, Default, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub enum SyntheticsType {
#[default]
Http,
}

#[derive(Clone, Debug, Serialize, Default, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub enum HttpSyntheticsBodyType {
#[default]
Raw,
}

#[derive(Clone, Debug, Serialize, Default, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub enum HttpSyntheticsAuthType {
#[default]
Basic,
Bearer,
}

#[derive(Clone, Debug, Serialize, Default, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct HttpSyntheticsAuth {
#[serde(rename = "type")]
pub auth_type: HttpSyntheticsAuthType,
pub basic: String,
pub bearer: String,
}

#[derive(Clone, Debug, Serialize, Default, Deserialize, ToSchema)]
pub struct HttpSynthetics {
pub url: String,
pub method: HTTPType,
#[serde(skip_serializing_if = "Option::is_none")]
pub query_params: Option<HashMap<String, String>>,
pub body_type: HttpSyntheticsBodyType,
pub body: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub headers: Option<HashMap<String, String>>,
pub cookies: String,
pub auth: HttpSyntheticsAuth,
}

#[derive(Clone, Debug, Serialize, Default, Deserialize, ToSchema)]
pub struct SyntheticsRetry {
pub count: u32,
pub delay: u32,
}

#[derive(Clone, Debug, Serialize, Default, Deserialize, ToSchema)]
pub struct SyntheticsAlert {
#[serde(rename = "type")]
pub alert_type: SyntheticsAlertType,
pub emails: Vec<String>,
pub message: String,
pub title: String,
}

#[derive(Clone, Debug, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct Synthetics {
pub name: String,
#[serde(default)]
pub org_id: String,
#[serde(default)]
pub description: String,
#[serde(rename = "type")]
pub synthetics_type: SyntheticsType,
#[serde(skip_serializing_if = "Option::is_none")]
pub request: Option<HttpSynthetics>,
#[serde(default)]
pub retry: SyntheticsRetry,
/// Frequency of synthetics test. E.g. - Hourly.
#[serde(default)]
pub schedule: SyntheticsFrequency,
#[serde(default)]
pub assertions: Vec<Assertion>,
#[serde(default)]
pub alert: SyntheticsAlert,
#[serde(default)]
pub enabled: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub last_triggered_at: Option<i64>,
#[serde(default = "datetime_now")]
#[schema(value_type = String, format = DateTime)]
pub created_at: DateTime<FixedOffset>,
#[serde(skip_serializing_if = "Option::is_none")]
#[schema(value_type = String, format = DateTime)]
pub updated_at: Option<DateTime<FixedOffset>>,
pub owner: String,
pub last_edited_by: String,
}

impl Default for Synthetics {
fn default() -> Self {
Self {
name: "".to_string(),
org_id: "".to_string(),
description: "".to_string(),
synthetics_type: SyntheticsType::Http,
request: Some(Default::default()),
retry: Default::default(),
schedule: SyntheticsFrequency::default(),
assertions: Default::default(),
alert: SyntheticsAlert::default(),
enabled: false,
last_triggered_at: None,
created_at: datetime_now(),
updated_at: None,
owner: "".to_string(),
last_edited_by: "".to_string(),
}
}
}

#[derive(Clone, Debug, Serialize, Default, Deserialize, ToSchema)]
pub enum AssertionType {
#[default]
Body,
Headers,
ResponseTime,
StatusCode,
BodyHash,
}

#[derive(Clone, Debug, Serialize, Default, Deserialize, ToSchema)]
pub struct Assertion {
#[serde(rename = "type")]
pub assert_type: AssertionType,
pub operator: Operator,
pub key: String,
pub timing_scope: bool,
pub value: config::utils::json::Value,
}
1 change: 1 addition & 0 deletions src/handler/http/request/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub mod rum;
pub mod search;
pub mod status;
pub mod stream;
pub mod synthetics;
pub mod syslog;
pub mod traces;
pub mod users;
Expand Down

0 comments on commit 3a7a000

Please sign in to comment.