Skip to content

Commit 9b6293a

Browse files
committed
feat: more info
1 parent 2be6b68 commit 9b6293a

File tree

4 files changed

+87
-9
lines changed

4 files changed

+87
-9
lines changed

src/utils/calc.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::utils::from_env::{FromEnv, FromEnvErr, FromEnvVar};
22
use core::num;
33

4+
use super::from_env::EnvItemInfo;
5+
46
// Env vars
57
pub(crate) const START_TIMESTAMP: &str = "START_TIMESTAMP";
68
pub(crate) const SLOT_OFFSET: &str = "SLOT_OFFSET";
@@ -125,8 +127,24 @@ impl SlotCalculator {
125127
impl FromEnv for SlotCalculator {
126128
type Error = SlotCalcEnvError;
127129

128-
fn inventory() -> Vec<&'static str> {
129-
vec![START_TIMESTAMP, SLOT_OFFSET, SLOT_DURATION]
130+
fn inventory() -> Vec<&'static EnvItemInfo> {
131+
vec![
132+
&EnvItemInfo {
133+
var: START_TIMESTAMP,
134+
description: "The start timestamp of the chain",
135+
optional: false,
136+
},
137+
&EnvItemInfo {
138+
var: SLOT_OFFSET,
139+
description: "The slot offset of the chain",
140+
optional: false,
141+
},
142+
&EnvItemInfo {
143+
var: SLOT_DURATION,
144+
description: "The slot duration of the chain",
145+
optional: false,
146+
},
147+
]
130148
}
131149

132150
fn from_env() -> Result<Self, FromEnvErr<Self::Error>> {

src/utils/from_env.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
use std::{convert::Infallible, env::VarError, num::ParseIntError, str::FromStr};
22

3+
/// Details about an environment variable. This is used to generate
4+
/// documentation for the environment variables and by the [`FromEnv`] trait to
5+
/// check if necessary environment variables are present.
6+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7+
pub struct EnvItemInfo {
8+
/// The environment variable name.
9+
pub var: &'static str,
10+
/// A description of the environment variable function in the CFG.
11+
pub description: &'static str,
12+
/// Whether the environment variable is optional or not.
13+
pub optional: bool,
14+
}
15+
316
/// Error type for loading from the environment. See the [`FromEnv`] trait for
417
/// more information.
518
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
@@ -93,8 +106,28 @@ pub trait FromEnv: core::fmt::Debug + Sized + 'static {
93106
/// Error type produced when loading from the environment.
94107
type Error: core::error::Error;
95108

96-
/// Get the environment variable names for this type.
97-
fn inventory() -> Vec<&'static str>;
109+
/// Get the required environment variable names for this type.
110+
///
111+
/// ## Note
112+
///
113+
/// This MUST include the environment variable names for all fields in the
114+
/// struct, including optional vars.
115+
fn inventory() -> Vec<&'static EnvItemInfo>;
116+
117+
/// Get a list of missing environment variables.
118+
fn check_inventory() -> Result<(), Vec<&'static EnvItemInfo>> {
119+
let mut missing = Vec::new();
120+
for var in Self::inventory() {
121+
if std::env::var(var.var).is_err() && !var.optional {
122+
missing.push(var);
123+
}
124+
}
125+
if missing.is_empty() {
126+
Ok(())
127+
} else {
128+
Err(missing)
129+
}
130+
}
98131

99132
/// Load from the environment.
100133
fn from_env() -> Result<Self, FromEnvErr<Self::Error>>;

src/utils/metrics.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::utils::from_env::{FromEnv, FromEnvErr, FromEnvVar};
22
use metrics_exporter_prometheus::PrometheusBuilder;
33

4+
use super::from_env::EnvItemInfo;
5+
46
/// Metrics port env var
57
const METRICS_PORT: &str = "METRICS_PORT";
68

@@ -40,8 +42,12 @@ impl From<u16> for MetricsConfig {
4042
impl FromEnv for MetricsConfig {
4143
type Error = std::num::ParseIntError;
4244

43-
fn inventory() -> Vec<&'static str> {
44-
vec![METRICS_PORT]
45+
fn inventory() -> Vec<&'static EnvItemInfo> {
46+
vec![&EnvItemInfo {
47+
var: METRICS_PORT,
48+
description: "Port on which to serve metrics",
49+
optional: true,
50+
}]
4551
}
4652

4753
fn from_env() -> Result<Self, FromEnvErr<Self::Error>> {

src/utils/otlp.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::from_env::{FromEnv, FromEnvErr, FromEnvVar};
1+
use crate::utils::from_env::{EnvItemInfo, FromEnv, FromEnvErr, FromEnvVar};
22
use opentelemetry::{trace::TracerProvider, KeyValue};
33
use opentelemetry_sdk::trace::SdkTracerProvider;
44
use opentelemetry_sdk::Resource;
@@ -110,8 +110,29 @@ pub struct OtelConfig {
110110
impl FromEnv for OtelConfig {
111111
type Error = url::ParseError;
112112

113-
fn inventory() -> Vec<&'static str> {
114-
vec![OTEL_ENDPOINT, OTEL_LEVEL, OTEL_TIMEOUT, OTEL_ENVIRONMENT]
113+
fn inventory() -> Vec<&'static EnvItemInfo> {
114+
vec![
115+
&EnvItemInfo {
116+
var: OTEL_ENDPOINT,
117+
description: "OTLP endpoint to send traces to",
118+
optional: true,
119+
},
120+
&EnvItemInfo {
121+
var: OTEL_LEVEL,
122+
description: "OTLP level to export",
123+
optional: true,
124+
},
125+
&EnvItemInfo {
126+
var: OTEL_TIMEOUT,
127+
description: "OTLP timeout in milliseconds",
128+
optional: true,
129+
},
130+
&EnvItemInfo {
131+
var: OTEL_ENVIRONMENT,
132+
description: "OTLP environment name",
133+
optional: true,
134+
},
135+
]
115136
}
116137

117138
fn from_env() -> Result<Self, FromEnvErr<Self::Error>> {

0 commit comments

Comments
 (0)