@@ -50,11 +50,18 @@ pub struct UsagePlugin {
5050pub struct Config {
5151 /// Default: true
5252 enabled : Option < bool > ,
53- /// Hive token, can also be set using the HIVE_TOKEN environment variable
53+ /// Hive token, can also be set using the HIVE_TOKEN environment variable.
54+ /// The token can be a registry access token, or a organization access token.
5455 registry_token : Option < String > ,
5556 /// Hive registry token. Set to your `/usage` endpoint if you are self-hosting.
5657 /// Default: https://app.graphql-hive.com/usage
58+ /// When `target` is set and organization access token is in use, the target ID is appended to the endpoint,
59+ /// so usage endpoint becomes `https://app.graphql-hive.com/usage/<target_id>`
5760 registry_usage_endpoint : Option < String > ,
61+ /// The target to which the usage data should be reported to.
62+ /// This can either be a slug following the format "$organizationSlug/$projectSlug/$targetSlug" (e.g "the-guild/graphql-hive/staging")
63+ /// or an UUID (e.g. "a0f4c605-6541-4350-8cfe-b31f21a4bf80").
64+ target : Option < String > ,
5865 /// Sample rate to determine sampling.
5966 /// 0.0 = 0% chance of being sent
6067 /// 1.0 = 100% chance of being sent.
@@ -98,6 +105,7 @@ impl Default for Config {
98105 connect_timeout : Some ( 5 ) ,
99106 request_timeout : Some ( 15 ) ,
100107 flush_interval : Some ( 5 ) ,
108+ target : None ,
101109 }
102110 }
103111}
@@ -186,14 +194,25 @@ impl Plugin for UsagePlugin {
186194 return Err ( "Hive token is required" . into ( ) ) ;
187195 }
188196
189- let endpoint = init
197+ let mut endpoint = init
190198 . config
191199 . registry_usage_endpoint
192200 . clone ( )
193201 . unwrap_or_else ( || {
194202 env:: var ( "HIVE_ENDPOINT" ) . unwrap_or ( DEFAULT_HIVE_USAGE_ENDPOINT . to_string ( ) )
195203 } ) ;
196204
205+ let target_id = init
206+ . config
207+ . target
208+ . clone ( )
209+ . or_else ( || env:: var ( "HIVE_TARGET_ID" ) . ok ( ) ) ;
210+
211+ // In case target ID is specified in configuration, append it to the endpoint
212+ if let Some ( target_id) = & target_id {
213+ endpoint. push_str ( & format ! ( "/{}" , target_id) ) ;
214+ }
215+
197216 let default_config = Config :: default ( ) ;
198217 let user_config = init. config ;
199218 let enabled = user_config
0 commit comments