Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: project telemetry config models #1972

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions common/src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ pub mod log;
pub mod project;
pub mod resource;
pub mod team;
pub mod telemetry;
pub mod user;
77 changes: 77 additions & 0 deletions common/src/models/telemetry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use serde::{Deserialize, Serialize};

/// Status of a telemetry export configuration for an external sink
#[derive(Eq, Clone, Debug, PartialEq, Serialize, Deserialize)]
#[typeshare::typeshare]
pub struct TelemetrySinkStatus {
/// Indicates that the associated project is configured to export telemetry data to this sink
enabled: bool,
}

/// A safe-for-display representation of the current telemetry export configuration for a given project
#[derive(Eq, Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[typeshare::typeshare]
pub struct ProjectTelemetryConfigResponse {
betterstack: Option<TelemetrySinkStatus>,
datadog: Option<TelemetrySinkStatus>,
grafana_cloud: Option<TelemetrySinkStatus>,
}

impl From<Vec<ProjectTelemetrySinkConfig>> for ProjectTelemetryConfigResponse {
fn from(value: Vec<ProjectTelemetrySinkConfig>) -> Self {
let mut instance = Self::default();

for sink in value {
match sink {
ProjectTelemetrySinkConfig::Betterstack(_) => {
instance.betterstack = Some(TelemetrySinkStatus { enabled: true })
}
ProjectTelemetrySinkConfig::Datadog(_) => {
instance.datadog = Some(TelemetrySinkStatus { enabled: true })
}
ProjectTelemetrySinkConfig::GrafanaCloud(_) => {
instance.grafana_cloud = Some(TelemetrySinkStatus { enabled: true })
}
}
}

instance
}
}
jonaro00 marked this conversation as resolved.
Show resolved Hide resolved

/// The user-supplied config required to export telemetry to a given external sink
#[derive(Eq, Clone, PartialEq, Serialize, Deserialize, strum::AsRefStr)]
#[serde(tag = "type", content = "content")]
#[typeshare::typeshare]
pub enum ProjectTelemetrySinkConfig {
/// [Betterstack](https://betterstack.com/docs/logs/open-telemetry/)
Betterstack(BetterstackConfig),
/// [Datadog](https://docs.datadoghq.com/opentelemetry/collector_exporter/otel_collector_datadog_exporter)
Datadog(DatadogConfig),
/// [Grafana Cloud](https://grafana.com/docs/grafana-cloud/send-data/otlp/)
GrafanaCloud(GrafanaCloudConfig),
}

impl ProjectTelemetrySinkConfig {
pub fn as_db_type(&self) -> String {
format!("project::telemetry::{}::config", self.as_ref())
}
Comment on lines +62 to +64
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: as_db_type could panic if as_ref() returns an unexpected value. Consider using a match statement for exhaustive pattern matching.

Suggested change
pub fn as_db_type(&self) -> String {
format!("project::telemetry::{}::config", self.as_ref())
}
pub fn as_db_type(&self) -> String {
let sink_type = match self {
Self::Betterstack(_) => "betterstack",
Self::Datadog(_) => "datadog",
Self::GrafanaCloud(_) => "grafana_cloud",
};
format!("project::telemetry::{}::config", sink_type)
}

}

#[derive(Eq, Clone, PartialEq, Serialize, Deserialize)]
#[typeshare::typeshare]
pub struct BetterstackConfig {
pub source_token: String,
}
Comment on lines +67 to +71
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider adding validation for the source_token format and non-emptiness

#[derive(Eq, Clone, PartialEq, Serialize, Deserialize)]
#[typeshare::typeshare]
pub struct DatadogConfig {
pub api_key: String,
}
#[derive(Eq, Clone, PartialEq, Serialize, Deserialize)]
#[typeshare::typeshare]
pub struct GrafanaCloudConfig {
pub token: String,
pub endpoint: String,
pub instance_id: String,
Comment on lines +79 to +82
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider adding validation for the endpoint URL and instance_id format.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Validation can happen in backend handler if needed

}
36 changes: 36 additions & 0 deletions common/types.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.