Skip to content

Commit 99c23de

Browse files
committed
Fix: Usage reporting access token handling
- Changed the `access_token` field in the `UsageReportingConfig` to `Option<String>`. - Updated the documentation to reflect the change. - Added error handling for missing access tokens.
1 parent 70dd3e1 commit 99c23de

File tree

3 files changed

+33
-20
lines changed

3 files changed

+33
-20
lines changed

bin/router/src/pipeline/usage_reporting.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,26 @@ use crate::{
1919
consts::ROUTER_VERSION,
2020
};
2121

22+
#[derive(Debug, thiserror::Error)]
23+
pub enum UsageReportingError {
24+
#[error("Usage Reporting - Access token is missing. Please provide it via 'HIVE_ACCESS_TOKEN' environment variable or under 'usage_reporting.access_token' in the configuration.")]
25+
MissingAccessToken,
26+
#[error("Usage Reporting - Failed to initialize usage agent: {0}")]
27+
AgentCreationError(#[from] AgentError),
28+
}
29+
2230
pub fn init_hive_user_agent(
2331
bg_tasks_manager: &mut BackgroundTasksManager,
2432
usage_config: &UsageReportingConfig,
25-
) -> Result<Arc<UsageAgent>, AgentError> {
33+
) -> Result<Arc<UsageAgent>, UsageReportingError> {
2634
let user_agent = format!("hive-router/{}", ROUTER_VERSION);
35+
let access_token = usage_config
36+
.access_token
37+
.as_deref()
38+
.ok_or(UsageReportingError::MissingAccessToken)?;
39+
2740
let hive_user_agent = UsageAgent::try_new(
28-
&usage_config.access_token,
41+
access_token,
2942
usage_config.endpoint.clone(),
3043
usage_config.target_id.clone(),
3144
usage_config.buffer_size,

docs/README.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
|[**query\_planner**](#query_planner)|`object`|Query planning configuration.<br/>Default: `{"allow_expose":false,"timeout":"10s"}`<br/>||
1818
|[**supergraph**](#supergraph)|`object`|Configuration for the Federation supergraph source. By default, the router will use a local file-based supergraph source (`./supergraph.graphql`).<br/>||
1919
|[**traffic\_shaping**](#traffic_shaping)|`object`|Configuration for the traffic-shaping of the executor. Use these configurations to control how requests are being executed to subgraphs.<br/>Default: `{"all":{"dedupe_enabled":true,"pool_idle_timeout":"50s","request_timeout":"30s"},"max_connections_per_host":100}`<br/>||
20-
|[**usage\_reporting**](#usage_reporting)|`object`|Configuration for usage reporting to GraphQL Hive.<br/>Default: `{"accept_invalid_certs":false,"access_token":"","buffer_size":1000,"client_name_header":"graphql-client-name","client_version_header":"graphql-client-version","connect_timeout":"5s","enabled":false,"endpoint":"https://app.graphql-hive.com/usage","exclude":[],"flush_interval":"5s","request_timeout":"15s","sample_rate":"100%","target_id":null}`<br/>|yes|
20+
|[**usage\_reporting**](#usage_reporting)|`object`|Configuration for usage reporting to GraphQL Hive.<br/>Default: `{"accept_invalid_certs":false,"access_token":null,"buffer_size":1000,"client_name_header":"graphql-client-name","client_version_header":"graphql-client-version","connect_timeout":"5s","enabled":false,"endpoint":"https://app.graphql-hive.com/usage","exclude":[],"flush_interval":"5s","request_timeout":"15s","sample_rate":"100%","target_id":null}`<br/>||
2121

2222
**Additional Properties:** not allowed
2323
**Example**
@@ -121,7 +121,7 @@ traffic_shaping:
121121
max_connections_per_host: 100
122122
usage_reporting:
123123
accept_invalid_certs: false
124-
access_token: ''
124+
access_token: null
125125
buffer_size: 1000
126126
client_name_header: graphql-client-name
127127
client_version_header: graphql-client-version
@@ -1910,26 +1910,26 @@ Configuration for usage reporting to GraphQL Hive.
19101910

19111911
|Name|Type|Description|Required|
19121912
|----|----|-----------|--------|
1913-
|**accept\_invalid\_certs**|`boolean`|Accepts invalid SSL certificates<br/>Default: false<br/>Default: `false`<br/>|no|
1914-
|**access\_token**|`string`|Your [Registry Access Token](https://the-guild.dev/graphql/hive/docs/management/targets#registry-access-tokens) with write permission.<br/>|yes|
1915-
|**buffer\_size**|`integer`|A maximum number of operations to hold in a buffer before sending to Hive Console<br/>Default: 1000<br/>Default: `1000`<br/>Format: `"uint"`<br/>Minimum: `0`<br/>|no|
1916-
|**client\_name\_header**|`string`|Default: `"graphql-client-name"`<br/>|no|
1917-
|**client\_version\_header**|`string`|Default: `"graphql-client-version"`<br/>|no|
1918-
|**connect\_timeout**|`string`|A timeout for only the connect phase of a request to Hive Console<br/>Default: 5 seconds<br/>Default: `"5s"`<br/>|no|
1919-
|**enabled**|`boolean`|Default: `false`<br/>|no|
1920-
|**endpoint**|`string`|For self-hosting, you can override `/usage` endpoint (defaults to `https://app.graphql-hive.com/usage`).<br/>Default: `"https://app.graphql-hive.com/usage"`<br/>|no|
1921-
|[**exclude**](#usage_reportingexclude)|`string[]`|A list of operations (by name) to be ignored by Hive.<br/>Default: <br/>|no|
1922-
|**flush\_interval**|`string`|Frequency of flushing the buffer to the server<br/>Default: 5 seconds<br/>Default: `"5s"`<br/>|no|
1923-
|**request\_timeout**|`string`|A timeout for the entire request to Hive Console<br/>Default: 15 seconds<br/>Default: `"15s"`<br/>|no|
1924-
|**sample\_rate**|`string`|Sample rate to determine sampling.<br/>0% = never being sent<br/>50% = half of the requests being sent<br/>100% = always being sent<br/>Default: 100%<br/>Default: `"100%"`<br/>|no|
1925-
|**target\_id**|`string`, `null`|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.<br/>|no|
1913+
|**accept\_invalid\_certs**|`boolean`|Accepts invalid SSL certificates<br/>Default: false<br/>Default: `false`<br/>||
1914+
|**access\_token**|`string`, `null`|Your [Registry Access Token](https://the-guild.dev/graphql/hive/docs/management/targets#registry-access-tokens) with write permission.<br/>||
1915+
|**buffer\_size**|`integer`|A maximum number of operations to hold in a buffer before sending to Hive Console<br/>Default: 1000<br/>Default: `1000`<br/>Format: `"uint"`<br/>Minimum: `0`<br/>||
1916+
|**client\_name\_header**|`string`|Default: `"graphql-client-name"`<br/>||
1917+
|**client\_version\_header**|`string`|Default: `"graphql-client-version"`<br/>||
1918+
|**connect\_timeout**|`string`|A timeout for only the connect phase of a request to Hive Console<br/>Default: 5 seconds<br/>Default: `"5s"`<br/>||
1919+
|**enabled**|`boolean`|Default: `false`<br/>||
1920+
|**endpoint**|`string`|For self-hosting, you can override `/usage` endpoint (defaults to `https://app.graphql-hive.com/usage`).<br/>Default: `"https://app.graphql-hive.com/usage"`<br/>||
1921+
|[**exclude**](#usage_reportingexclude)|`string[]`|A list of operations (by name) to be ignored by Hive.<br/>Default: <br/>||
1922+
|**flush\_interval**|`string`|Frequency of flushing the buffer to the server<br/>Default: 5 seconds<br/>Default: `"5s"`<br/>||
1923+
|**request\_timeout**|`string`|A timeout for the entire request to Hive Console<br/>Default: 15 seconds<br/>Default: `"15s"`<br/>||
1924+
|**sample\_rate**|`string`|Sample rate to determine sampling.<br/>0% = never being sent<br/>50% = half of the requests being sent<br/>100% = always being sent<br/>Default: 100%<br/>Default: `"100%"`<br/>||
1925+
|**target\_id**|`string`, `null`|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.<br/>||
19261926

19271927
**Additional Properties:** not allowed
19281928
**Example**
19291929

19301930
```yaml
19311931
accept_invalid_certs: false
1932-
access_token: ''
1932+
access_token: null
19331933
buffer_size: 1000
19341934
client_name_header: graphql-client-name
19351935
client_version_header: graphql-client-version

lib/router-config/src/usage_reporting.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub struct UsageReportingConfig {
1010
pub enabled: bool,
1111

1212
/// Your [Registry Access Token](https://the-guild.dev/graphql/hive/docs/management/targets#registry-access-tokens) with write permission.
13-
pub access_token: String,
13+
pub access_token: Option<String>,
1414

1515
/// 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.
1616
#[serde(deserialize_with = "deserialize_target_id")]
@@ -83,7 +83,7 @@ impl Default for UsageReportingConfig {
8383
fn default() -> Self {
8484
Self {
8585
enabled: default_enabled(),
86-
access_token: String::new(),
86+
access_token: None,
8787
target_id: None,
8888
endpoint: default_endpoint(),
8989
sample_rate: default_sample_rate(),

0 commit comments

Comments
 (0)