Skip to content

Commit d9af753

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 856f162 commit d9af753

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
@@ -16,7 +16,7 @@
1616
|[**query\_planner**](#query_planner)|`object`|Query planning configuration.<br/>Default: `{"allow_expose":false,"timeout":"10s"}`<br/>||
1717
|[**supergraph**](#supergraph)|`object`|Configuration for the Federation supergraph source. By default, the router will use a local file-based supergraph source (`./supergraph.graphql`).<br/>||
1818
|[**traffic\_shaping**](#traffic_shaping)|`object`|Configuration for the traffic-shaper executor. Use these configurations to control how requests are being executed to subgraphs.<br/>Default: `{"dedupe_enabled":true,"max_connections_per_host":100,"pool_idle_timeout_seconds":50}`<br/>||
19-
|[**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|
19+
|[**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/>||
2020

2121
**Additional Properties:** not allowed
2222
**Example**
@@ -113,7 +113,7 @@ traffic_shaping:
113113
pool_idle_timeout_seconds: 50
114114
usage_reporting:
115115
accept_invalid_certs: false
116-
access_token: ''
116+
access_token: null
117117
buffer_size: 1000
118118
client_name_header: graphql-client-name
119119
client_version_header: graphql-client-version
@@ -1878,26 +1878,26 @@ Configuration for usage reporting to GraphQL Hive.
18781878

18791879
|Name|Type|Description|Required|
18801880
|----|----|-----------|--------|
1881-
|**accept\_invalid\_certs**|`boolean`|Accepts invalid SSL certificates<br/>Default: false<br/>Default: `false`<br/>|no|
1882-
|**access\_token**|`string`|Your [Registry Access Token](https://the-guild.dev/graphql/hive/docs/management/targets#registry-access-tokens) with write permission.<br/>|yes|
1883-
|**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|
1884-
|**client\_name\_header**|`string`|Default: `"graphql-client-name"`<br/>|no|
1885-
|**client\_version\_header**|`string`|Default: `"graphql-client-version"`<br/>|no|
1886-
|**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|
1887-
|**enabled**|`boolean`|Default: `false`<br/>|no|
1888-
|**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|
1889-
|[**exclude**](#usage_reportingexclude)|`string[]`|A list of operations (by name) to be ignored by Hive.<br/>Default: <br/>|no|
1890-
|**flush\_interval**|`string`|Frequency of flushing the buffer to the server<br/>Default: 5 seconds<br/>Default: `"5s"`<br/>|no|
1891-
|**request\_timeout**|`string`|A timeout for the entire request to Hive Console<br/>Default: 15 seconds<br/>Default: `"15s"`<br/>|no|
1892-
|**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|
1893-
|**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|
1881+
|**accept\_invalid\_certs**|`boolean`|Accepts invalid SSL certificates<br/>Default: false<br/>Default: `false`<br/>||
1882+
|**access\_token**|`string`, `null`|Your [Registry Access Token](https://the-guild.dev/graphql/hive/docs/management/targets#registry-access-tokens) with write permission.<br/>||
1883+
|**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/>||
1884+
|**client\_name\_header**|`string`|Default: `"graphql-client-name"`<br/>||
1885+
|**client\_version\_header**|`string`|Default: `"graphql-client-version"`<br/>||
1886+
|**connect\_timeout**|`string`|A timeout for only the connect phase of a request to Hive Console<br/>Default: 5 seconds<br/>Default: `"5s"`<br/>||
1887+
|**enabled**|`boolean`|Default: `false`<br/>||
1888+
|**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/>||
1889+
|[**exclude**](#usage_reportingexclude)|`string[]`|A list of operations (by name) to be ignored by Hive.<br/>Default: <br/>||
1890+
|**flush\_interval**|`string`|Frequency of flushing the buffer to the server<br/>Default: 5 seconds<br/>Default: `"5s"`<br/>||
1891+
|**request\_timeout**|`string`|A timeout for the entire request to Hive Console<br/>Default: 15 seconds<br/>Default: `"15s"`<br/>||
1892+
|**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/>||
1893+
|**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/>||
18941894

18951895
**Additional Properties:** not allowed
18961896
**Example**
18971897

18981898
```yaml
18991899
accept_invalid_certs: false
1900-
access_token: ''
1900+
access_token: null
19011901
buffer_size: 1000
19021902
client_name_header: graphql-client-name
19031903
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)