diff --git a/packages/documentation/content/docs/router/configuration/environment-variables.mdx b/packages/documentation/content/docs/router/configuration/environment-variables.mdx index 455ce170..ef36c7f8 100644 --- a/packages/documentation/content/docs/router/configuration/environment-variables.mdx +++ b/packages/documentation/content/docs/router/configuration/environment-variables.mdx @@ -2,7 +2,9 @@ title: "Environment Variables" --- -## Available Environment Variables +To configure Hive Router using environment variables, you use the pre-defined environment variables listed below, or hook any configuration field to an environment variable. + +## Pre-defined Environment Variables The following environment variables are accepted by the router, and they override other configurations that can be set in the router's configuration file (`router.config.yaml`): @@ -31,6 +33,34 @@ configurations that can be set in the router's configuration file (`router.confi > In cases where a configuration file (`router.config.yaml`) is used with environment variables at > the same time, environment variables will only override the specific configuration values. +> In cases where the pre-defined environment variables are used, along with `from_env`, the pre-defined environment variable will be used +> as they are considered preferred over the configuration file values. + +## Setting Any Config Value from an Environment Variable + +Beyond the fixed overrides above, any primitive field anywhere in the configuration file — strings, +numbers, booleans — can be set from an environment variable using `from_env`, instead of a literal value: + +```yaml title="router.config.yaml" +http: + port: + from_env: PORT +``` + +If the referenced environment variable is not set, the field falls back to its own default (or fails +validation as usual if the field is required). You can also supply an inline fallback with `default`, +which is used instead of the field's own default when the environment variable is unset: + +```yaml title="router.config.yaml" +http: + port: + from_env: PORT + default: 4000 +``` + +If you are using the plugin system, `from_env` also works inside a plugin's own `config` block, since it's resolved before the +configuration is split up and handed to each plugin. + ## Accessing Environment Variables in Expressions Beyond using environment variables to override configuration values at startup, you can also access diff --git a/packages/documentation/content/docs/router/configuration/storages.mdx b/packages/documentation/content/docs/router/configuration/storages.mdx index a405f16a..64d19af4 100644 --- a/packages/documentation/content/docs/router/configuration/storages.mdx +++ b/packages/documentation/content/docs/router/configuration/storages.mdx @@ -204,6 +204,15 @@ endpoint: are not dynamic in the way that header or routing expressions are. +For simple cases (no fallback logic beyond a single default), the more general +[`from_env`](./environment-variables#setting-any-config-value-from-an-environment-variable) shorthand +works here too, and on any other primitive field in the config: + +```yaml +bucket: + from_env: S3_BUCKET +``` + ## Provider examples ### Amazon S3 (production, EKS via IRSA) diff --git a/packages/documentation/content/docs/router/customizations/plugin-system/hooks.mdx b/packages/documentation/content/docs/router/customizations/plugin-system/hooks.mdx index 6d92ed2d..7e73fae8 100644 --- a/packages/documentation/content/docs/router/customizations/plugin-system/hooks.mdx +++ b/packages/documentation/content/docs/router/customizations/plugin-system/hooks.mdx @@ -952,6 +952,42 @@ async fn on_execute<'exec>( } ``` +**Report per-subgraph call durations** + +The request summary tracks `subgraph_calls_duration`, a map of subgraph name to the duration of +every call made to it. Read it via +[`get_current_summary()`](/docs/router/observability/logging#customizing-logs-from-plugins) in +`on_end` and expose it however you like, e.g. as a response extension. + +```rust +use hive_router::get_current_summary; +use std::collections::BTreeMap; + +async fn on_execute<'exec>( + &'exec self, + payload: OnExecuteStartHookPayload<'exec>, +) -> OnExecuteStartHookResult<'exec> { + payload.on_end(|mut end_payload: OnExecuteEndHookPayload<'exec>| { + let subgraph_calls: BTreeMap = get_current_summary() + .and_then(|summary| { + summary.subgraph_calls_duration.lock().ok().map(|durations| { + durations + .iter() + .map(|(name, calls)| { + let total_ms = calls.iter().map(|d| d.as_millis() as u64).sum(); + (name.clone(), total_ms) + }) + .collect() + }) + }) + .unwrap_or_default(); + + end_payload.add_extension("subgraph_calls", subgraph_calls); + end_payload.proceed() + }) +} +``` + #### Payload API Reference - [`OnExecuteStartHookPayload`](https://github.com/graphql-hive/router/blob/main/lib/executor/src/plugins/hooks/on_execute.rs#:~:text=struct%20OnExecuteStartHookPayload) diff --git a/packages/documentation/content/docs/router/observability/logging.mdx b/packages/documentation/content/docs/router/observability/logging.mdx index a1b0b3b4..88778aab 100644 --- a/packages/documentation/content/docs/router/observability/logging.mdx +++ b/packages/documentation/content/docs/router/observability/logging.mdx @@ -212,6 +212,7 @@ which makes filtering and routing precise. Common targets include: - `router::graphql_parsing` / `router::graphql_validation` / `router::graphql_normalization` - `router::coprocessor` / `router::plugin_system` — customizations - `router::jwt` / `router::authorization` — security +- `router::config` — rotuer configuration loading ## Request correlation @@ -256,8 +257,8 @@ Plugins can enrich the current request's logs through a few functions, callable - **`set_summary_message(message)`** — overrides the summary line's `message` field. The first call wins; later calls for the same request are no-ops. - **`get_current_summary()`** — reads the current request's in-progress summary (`operation_name`, - `status_code`, and so on), so a plugin can build a message from data only known once the request - is finishing. + `status_code`, `subgraph_calls_duration`, and so on), so a plugin can build a message from data + only known once the request is finishing. ```rust use hive_router::{get_current_summary, set_log_correlation, set_summary_message};