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

Merged
merged 9 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
jonaro00 marked this conversation as resolved.
Show resolved Hide resolved
- 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" }
jonaro00 marked this conversation as resolved.
Show resolved Hide resolved
command = "typeshare"
args = ["common", "-l", "typescript", "-c", "common/typeshare.toml", "-o", "common/types.ts"]

Expand Down
14 changes: 9 additions & 5 deletions admin/src/args.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use clap::{Parser, Subcommand};
use shuttle_common::{
constants::API_URL_DEFAULT_BETA,
models::{project::ComputeTier, user::UserId},
};
use shuttle_common::{constants::API_URL_DEFAULT_BETA, models::project::ComputeTier};

#[derive(Parser, Debug)]
pub struct Args {
Expand All @@ -22,7 +19,7 @@ pub struct Args {
pub enum Command {
ChangeProjectOwner {
project_name: String,
new_user_id: UserId,
new_user_id: String,
},

UpdateCompute {
Expand All @@ -37,6 +34,13 @@ pub enum Command {
/// Renew all custom domain certificates
RenewCerts,

SetBetaAccess {
user_id: String,
},
UnsetBetaAccess {
user_id: String,
},
jonaro00 marked this conversation as resolved.
Show resolved Hide resolved

/// Garbage collect free tier projects
Gc {
/// days since last deployment to filter by
Expand Down
19 changes: 19 additions & 0 deletions admin/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,25 @@ impl Client {
.await
}

pub async fn set_beta_access(&self, user_id: &str, access: bool) -> Result<()> {
let resp = if access {
self.inner
.put(format!("/admin/users/{user_id}/beta"), Option::<()>::None)
.await?
} else {
self.inner
.delete(format!("/admin/users/{user_id}/beta"), Option::<()>::None)
.await?
};

if !resp.status().is_success() {
dbg!(resp);
panic!("request failed");
}
jonaro00 marked this conversation as resolved.
Show resolved Hide resolved

Ok(())
}

pub async fn gc_free_tier(&self, days: u32) -> Result<Vec<String>> {
let path = format!("/admin/gc/free/{days}");
self.inner.get_json(&path).await
Expand Down
8 changes: 8 additions & 0 deletions admin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ pub async fn run(args: Args) {
.unwrap();
println!("{res:?}");
}
Command::SetBetaAccess { user_id } => {
client.set_beta_access(&user_id, true).await.unwrap();
println!("Set user {user_id} beta access");
}
Command::UnsetBetaAccess { user_id } => {
client.set_beta_access(&user_id, false).await.unwrap();
println!("Unset user {user_id} beta access");
jonaro00 marked this conversation as resolved.
Show resolved Hide resolved
}
jonaro00 marked this conversation as resolved.
Show resolved Hide resolved
Command::Gc {
days,
stop_deployments,
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;
4 changes: 1 addition & 3 deletions common/src/models/team.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use serde::{Deserialize, Serialize};
use strum::{Display, EnumString};

use super::user::UserId;

/// Minimal team information
#[derive(Debug, Default, Serialize, Deserialize, PartialEq)]
pub struct Response {
Expand All @@ -20,7 +18,7 @@ pub struct Response {
#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct MemberResponse {
/// User ID
pub id: UserId,
pub id: String,

/// Role of the user in the team
pub role: MemberRole,
Expand Down
129 changes: 129 additions & 0 deletions common/src/models/telemetry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
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 TelemetryConfigResponse {
betterstack: Option<TelemetrySinkStatus>,
datadog: Option<TelemetrySinkStatus>,
grafana_cloud: Option<TelemetrySinkStatus>,
}

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

for sink in value {
match sink {
TelemetrySinkConfig::Betterstack(_) => {
instance.betterstack = Some(TelemetrySinkStatus { enabled: true })
}
TelemetrySinkConfig::Datadog(_) => {
instance.datadog = Some(TelemetrySinkStatus { enabled: true })
}
TelemetrySinkConfig::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 TelemetrySinkConfig {
/// [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 TelemetrySinkConfig {
pub fn as_db_type(&self) -> String {
format!("project::telemetry::{}::config", self.as_ref())
}
}

impl TelemetrySinkConfigDiscriminants {
pub fn as_db_type(&self) -> String {
format!("project::telemetry::{}::config", self.as_ref())
}
jonaro00 marked this conversation as resolved.
Show resolved Hide resolved
}

#[derive(Eq, Clone, PartialEq, Serialize, Deserialize)]
#[typeshare::typeshare]
pub struct BetterstackConfig {
pub source_token: String,
}
jonaro00 marked this conversation as resolved.
Show resolved Hide resolved
#[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,
jonaro00 marked this conversation as resolved.
Show resolved Hide resolved
}

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

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

assert_eq!(
"betterstack",
TelemetrySinkConfigDiscriminants::Betterstack.as_ref()
);
assert_eq!(
"grafana_cloud",
TelemetrySinkConfigDiscriminants::GrafanaCloud.as_ref()
);
assert_eq!(
"\"betterstack\"",
serde_json::to_string(&TelemetrySinkConfigDiscriminants::Betterstack).unwrap()
);
assert_eq!(
"\"grafana_cloud\"",
serde_json::to_string(&TelemetrySinkConfigDiscriminants::GrafanaCloud).unwrap()
);
}
}
4 changes: 0 additions & 4 deletions common/src/models/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ use crossterm::style::Stylize;
#[cfg(feature = "display")]
use std::fmt::Write;

/// In normal cases, a string with the format `user_<ULID>`.
/// This is a soft rule and the string can be something different.
pub type UserId = String;

#[derive(Deserialize, Serialize, Debug)]
#[typeshare::typeshare]
pub struct UserResponse {
Expand Down
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.