|
| 1 | +use serde::{Deserialize, Serialize}; |
| 2 | + |
| 3 | +/// Status of a telemetry export configuration for an external sink |
| 4 | +#[derive(Eq, Clone, Debug, PartialEq, Serialize, Deserialize)] |
| 5 | +#[typeshare::typeshare] |
| 6 | +pub struct TelemetrySinkStatus { |
| 7 | + /// Indicates that the associated project is configured to export telemetry data to this sink |
| 8 | + enabled: bool, |
| 9 | +} |
| 10 | + |
| 11 | +/// A safe-for-display representation of the current telemetry export configuration for a given project |
| 12 | +#[derive(Eq, Clone, Debug, Default, PartialEq, Serialize, Deserialize)] |
| 13 | +#[typeshare::typeshare] |
| 14 | +pub struct TelemetryConfigResponse { |
| 15 | + betterstack: Option<TelemetrySinkStatus>, |
| 16 | + datadog: Option<TelemetrySinkStatus>, |
| 17 | + grafana_cloud: Option<TelemetrySinkStatus>, |
| 18 | +} |
| 19 | + |
| 20 | +impl From<Vec<TelemetrySinkConfig>> for TelemetryConfigResponse { |
| 21 | + fn from(value: Vec<TelemetrySinkConfig>) -> Self { |
| 22 | + let mut instance = Self::default(); |
| 23 | + |
| 24 | + for sink in value { |
| 25 | + match sink { |
| 26 | + TelemetrySinkConfig::Betterstack(_) => { |
| 27 | + instance.betterstack = Some(TelemetrySinkStatus { enabled: true }) |
| 28 | + } |
| 29 | + TelemetrySinkConfig::Datadog(_) => { |
| 30 | + instance.datadog = Some(TelemetrySinkStatus { enabled: true }) |
| 31 | + } |
| 32 | + TelemetrySinkConfig::GrafanaCloud(_) => { |
| 33 | + instance.grafana_cloud = Some(TelemetrySinkStatus { enabled: true }) |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + instance |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +/// The user-supplied config required to export telemetry to a given external sink |
| 43 | +#[derive( |
| 44 | + Eq, Clone, PartialEq, Serialize, Deserialize, strum::AsRefStr, strum::EnumDiscriminants, |
| 45 | +)] |
| 46 | +#[serde(tag = "type", content = "content", rename_all = "snake_case")] |
| 47 | +#[strum(serialize_all = "snake_case")] |
| 48 | +#[typeshare::typeshare] |
| 49 | +#[strum_discriminants(derive(Serialize, Deserialize, strum::AsRefStr))] |
| 50 | +#[strum_discriminants(serde(rename_all = "snake_case"))] |
| 51 | +#[strum_discriminants(strum(serialize_all = "snake_case"))] |
| 52 | +pub enum TelemetrySinkConfig { |
| 53 | + /// [Betterstack](https://betterstack.com/docs/logs/open-telemetry/) |
| 54 | + Betterstack(BetterstackConfig), |
| 55 | + /// [Datadog](https://docs.datadoghq.com/opentelemetry/collector_exporter/otel_collector_datadog_exporter) |
| 56 | + Datadog(DatadogConfig), |
| 57 | + /// [Grafana Cloud](https://grafana.com/docs/grafana-cloud/send-data/otlp/) |
| 58 | + GrafanaCloud(GrafanaCloudConfig), |
| 59 | +} |
| 60 | + |
| 61 | +impl TelemetrySinkConfig { |
| 62 | + pub fn as_db_type(&self) -> String { |
| 63 | + format!("project::telemetry::{}::config", self.as_ref()) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +impl TelemetrySinkConfigDiscriminants { |
| 68 | + pub fn as_db_type(&self) -> String { |
| 69 | + format!("project::telemetry::{}::config", self.as_ref()) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +#[derive(Eq, Clone, PartialEq, Serialize, Deserialize)] |
| 74 | +#[typeshare::typeshare] |
| 75 | +pub struct BetterstackConfig { |
| 76 | + pub source_token: String, |
| 77 | +} |
| 78 | +#[derive(Eq, Clone, PartialEq, Serialize, Deserialize)] |
| 79 | +#[typeshare::typeshare] |
| 80 | +pub struct DatadogConfig { |
| 81 | + pub api_key: String, |
| 82 | +} |
| 83 | +#[derive(Eq, Clone, PartialEq, Serialize, Deserialize)] |
| 84 | +#[typeshare::typeshare] |
| 85 | +pub struct GrafanaCloudConfig { |
| 86 | + pub token: String, |
| 87 | + pub endpoint: String, |
| 88 | + pub instance_id: String, |
| 89 | +} |
| 90 | + |
| 91 | +#[cfg(test)] |
| 92 | +mod tests { |
| 93 | + use super::*; |
| 94 | + |
| 95 | + #[test] |
| 96 | + fn sink_config_enum() { |
| 97 | + assert_eq!( |
| 98 | + "betterstack", |
| 99 | + TelemetrySinkConfig::Betterstack(BetterstackConfig { |
| 100 | + source_token: "".into() |
| 101 | + }) |
| 102 | + .as_ref() |
| 103 | + ); |
| 104 | + assert_eq!( |
| 105 | + "project::telemetry::betterstack::config", |
| 106 | + TelemetrySinkConfig::Betterstack(BetterstackConfig { |
| 107 | + source_token: "".into() |
| 108 | + }) |
| 109 | + .as_db_type() |
| 110 | + ); |
| 111 | + |
| 112 | + assert_eq!( |
| 113 | + "betterstack", |
| 114 | + TelemetrySinkConfigDiscriminants::Betterstack.as_ref() |
| 115 | + ); |
| 116 | + assert_eq!( |
| 117 | + "grafana_cloud", |
| 118 | + TelemetrySinkConfigDiscriminants::GrafanaCloud.as_ref() |
| 119 | + ); |
| 120 | + assert_eq!( |
| 121 | + "\"betterstack\"", |
| 122 | + serde_json::to_string(&TelemetrySinkConfigDiscriminants::Betterstack).unwrap() |
| 123 | + ); |
| 124 | + assert_eq!( |
| 125 | + "\"grafana_cloud\"", |
| 126 | + serde_json::to_string(&TelemetrySinkConfigDiscriminants::GrafanaCloud).unwrap() |
| 127 | + ); |
| 128 | + } |
| 129 | +} |
0 commit comments