Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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`):
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,15 @@ endpoint:
are not dynamic in the way that header or routing expressions are.
</Callout>

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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, u64> = 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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};
Expand Down
Loading