Skip to content

Commit b3d239f

Browse files
committed
Enabled
1 parent 887e891 commit b3d239f

File tree

5 files changed

+60
-25
lines changed

5 files changed

+60
-25
lines changed

bin/router/src/lib.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,14 @@ pub async fn configure_app_from_config(
110110
true => Some(JwtAuthRuntime::init(bg_tasks_manager, &router_config.jwt).await?),
111111
false => None,
112112
};
113-
let usage_agent = router_config
114-
.usage_reporting
115-
.as_ref()
116-
.map(|usage_config| Arc::new(create_hive_user_agent(usage_config)));
113+
114+
let usage_agent = if router_config.usage_reporting.enabled {
115+
Some(Arc::new(create_hive_user_agent(
116+
&router_config.usage_reporting,
117+
)))
118+
} else {
119+
None
120+
};
117121

118122
if let Some(usage_agent) = &usage_agent {
119123
bg_tasks_manager.register_task(usage_agent.clone());

bin/router/src/pipeline/mod.rs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -192,26 +192,19 @@ pub async fn execute_pipeline(
192192
)
193193
.await?;
194194

195-
shared_state
196-
.hive_usage_agent
197-
.as_ref()
198-
.and_then(|usage_agent| {
199-
shared_state
200-
.router_config
201-
.usage_reporting
202-
.as_ref()
203-
.map(|usage_config| {
204-
usage_reporting::collect_usage_report(
205-
supergraph.supergraph_schema.clone(),
206-
start.elapsed(),
207-
req,
208-
&client_request_details,
209-
usage_agent,
210-
usage_config,
211-
&execution_result,
212-
)
213-
})
214-
});
195+
if shared_state.router_config.usage_reporting.enabled {
196+
if let Some(usage_agent) = &shared_state.hive_usage_agent {
197+
usage_reporting::collect_usage_report(
198+
supergraph.supergraph_schema.clone(),
199+
start.elapsed(),
200+
req,
201+
&client_request_details,
202+
usage_agent,
203+
&shared_state.router_config.usage_reporting,
204+
&execution_result,
205+
);
206+
}
207+
}
215208

216209
Ok(execution_result)
217210
}

lib/router-config/src/env_overrides.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ pub struct EnvVarOverrides {
3333
pub hive_console_cdn_key: Option<String>,
3434
#[envconfig(from = "HIVE_CDN_POLL_INTERVAL")]
3535
pub hive_console_cdn_poll_interval: Option<String>,
36+
#[envconfig(from = "HIVE_ACCESS_TOKEN")]
37+
pub hive_access_token: Option<String>,
38+
#[envconfig(from = "HIVE_TARGET")]
39+
pub hive_target: Option<String>,
3640
}
3741

3842
#[derive(Debug, thiserror::Error)]
@@ -99,6 +103,14 @@ impl EnvVarOverrides {
99103
}
100104
}
101105

106+
if let Some(hive_access_token) = self.hive_access_token.take() {
107+
config = config.set_override("usage_reporting.access_token", hive_access_token)?;
108+
if let Some(hive_target) = self.hive_target.take() {
109+
config = config.set_override("usage_reporting.target_id", hive_target)?;
110+
}
111+
config = config.set_override("usage_reporting.enabled", true)?;
112+
}
113+
102114
// GraphiQL overrides
103115
if let Some(graphiql_enabled) = self.graphiql_enabled.take() {
104116
config = config.set_override("graphiql.enabled", graphiql_enabled)?;

lib/router-config/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ pub struct HiveRouterConfig {
9696

9797
/// Configuration for usage reporting to GraphQL Hive.
9898
#[serde(default)]
99-
pub usage_reporting: Option<usage_reporting::UsageReportingConfig>,
99+
pub usage_reporting: usage_reporting::UsageReportingConfig,
100100
}
101101

102102
#[derive(Debug, thiserror::Error)]

lib/router-config/src/usage_reporting.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use serde::{Deserialize, Serialize};
66
#[derive(Debug, Deserialize, Serialize, JsonSchema, Clone)]
77
#[serde(deny_unknown_fields)]
88
pub struct UsageReportingConfig {
9+
#[serde(default = "default_enabled")]
10+
pub enabled: bool,
911
/// Your [Registry Access Token](https://the-guild.dev/graphql/hive/docs/management/targets#registry-access-tokens) with write permission.
1012
pub access_token: String,
1113
/// A target ID, this can either be a slug following the format “$organizationSlug/$projectSlug/$targetSlug” (e.g “the-guild/graphql-hive/staging”) or an UUID (e.g. “a0f4c605-6541-4350-8cfe-b31f21a4bf80”). To be used when the token is configured with an organization access token.
@@ -64,6 +66,30 @@ pub struct UsageReportingConfig {
6466
pub flush_interval: Duration,
6567
}
6668

69+
impl Default for UsageReportingConfig {
70+
fn default() -> Self {
71+
Self {
72+
enabled: default_enabled(),
73+
access_token: String::new(),
74+
target_id: None,
75+
endpoint: default_endpoint(),
76+
sample_rate: default_sample_rate(),
77+
exclude: Vec::new(),
78+
client_name_header: default_client_name_header(),
79+
client_version_header: default_client_version_header(),
80+
buffer_size: default_buffer_size(),
81+
accept_invalid_certs: default_accept_invalid_certs(),
82+
connect_timeout: default_connect_timeout(),
83+
request_timeout: default_request_timeout(),
84+
flush_interval: default_flush_interval(),
85+
}
86+
}
87+
}
88+
89+
fn default_enabled() -> bool {
90+
false
91+
}
92+
6793
fn default_endpoint() -> String {
6894
"https://app.graphql-hive.com/usage".to_string()
6995
}

0 commit comments

Comments
 (0)