Skip to content

feat: project telemetry config models #1972

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

Merged
merged 9 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ jobs:
- restore-cargo-and-sccache
- install-cargo-make
- run: cargo make ci-workspace
- run: curl --proto '=https' --tlsv1.2 -LsSf https://github.com/1Password/typeshare/releases/download/v1.11.0/typeshare-cli-v1.11.0-installer.sh | sh
- run: curl --proto '=https' --tlsv1.2 -LsSf https://github.com/1Password/typeshare/releases/download/v1.13.0/typeshare-cli-v1.13.0-installer.sh | sh
- run: cargo make types
- run: cargo make check-types
- save-sccache
Expand Down
2 changes: 1 addition & 1 deletion Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ command = "git-cliff"
args = ["-o", "CHANGELOG.md", "-t", "${@}"]

[tasks.types]
install_crate = { crate_name = "typeshare-cli", binary = "typeshare", test_arg = ["-V"], version = "1.11.0" }
install_crate = { crate_name = "typeshare-cli", binary = "typeshare", test_arg = ["-V"], version = "1.13.0" }
command = "typeshare"
args = ["common", "-l", "typescript", "-c", "common/typeshare.toml", "-o", "common/types.ts"]

Expand Down
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;
123 changes: 123 additions & 0 deletions common/src/models/telemetry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
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
}
}

/// The user-supplied config required to export telemetry to a given external sink
#[derive(
Eq, Clone, PartialEq, Serialize, Deserialize, strum::AsRefStr, strum::EnumDiscriminants,
)]
#[serde(tag = "type", content = "content", rename_all = "snake_case")]
#[strum(serialize_all = "snake_case")]
#[typeshare::typeshare]
#[strum_discriminants(derive(Serialize, Deserialize, strum::AsRefStr))]
#[strum_discriminants(serde(rename_all = "snake_case"))]
#[strum_discriminants(strum(serialize_all = "snake_case"))]
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())
}
}

#[derive(Eq, Clone, PartialEq, Serialize, Deserialize)]
#[typeshare::typeshare]
pub struct BetterstackConfig {
pub source_token: String,
}
#[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,
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn sink_config_enum() {
assert_eq!(
"betterstack",
ProjectTelemetrySinkConfig::Betterstack(BetterstackConfig {
source_token: "".into()
})
.as_ref()
);
assert_eq!(
"project::telemetry::betterstack::config",
ProjectTelemetrySinkConfig::Betterstack(BetterstackConfig {
source_token: "".into()
})
.as_db_type()
);

assert_eq!(
"betterstack",
ProjectTelemetrySinkConfigDiscriminants::Betterstack.as_ref()
);
assert_eq!(
"grafana_cloud",
ProjectTelemetrySinkConfigDiscriminants::GrafanaCloud.as_ref()
);
assert_eq!(
"\"betterstack\"",
serde_json::to_string(&ProjectTelemetrySinkConfigDiscriminants::Betterstack).unwrap()
);
assert_eq!(
"\"grafana_cloud\"",
serde_json::to_string(&ProjectTelemetrySinkConfigDiscriminants::GrafanaCloud).unwrap()
);
}
}
38 changes: 37 additions & 1 deletion common/types.ts

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