-
Notifications
You must be signed in to change notification settings - Fork 187
/
Copy pathmod.rs
187 lines (171 loc) · 5.16 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use serde::Deserialize;
use std::borrow::Cow;
use std::collections::HashMap;
use std::io::{Error, ErrorKind};
use std::net::SocketAddr;
use std::path::PathBuf;
use svix_bridge_plugin_queue::config::{
into_receiver_output, QueueConsumerConfig, ReceiverOutputOpts as QueueOutOpts,
};
use svix_bridge_types::{
ReceiverInputOpts, ReceiverOutput, SenderInput, SenderOutputOpts, TransformationConfig,
};
use tracing::Level;
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Config {
#[serde(default)]
pub senders: Vec<SenderConfig>,
#[serde(default)]
pub receivers: Vec<ReceiverConfig>,
/// The log level to run the service with. Supported: info, debug, trace
#[serde(default)]
pub log_level: LogLevel,
/// The log format that all output will follow. Supported: default, json
#[serde(default)]
pub log_format: LogFormat,
/// OpenTelemetry exporter settings
#[serde(default)]
pub opentelemetry: Option<OtelExporterConfig>,
#[serde(default = "default_http_listen_address")]
pub http_listen_address: SocketAddr,
}
impl Config {
/// Build a Config from yaml source.
/// Optionally accepts a map to perform variable substitution with.
pub fn from_src(
raw_src: &str,
vars: Option<&HashMap<String, String>>,
) -> std::io::Result<Self> {
let src = if let Some(vars) = vars {
Cow::Owned(envsubst::substitute(raw_src, vars).map_err(|e| {
Error::new(
ErrorKind::Other,
format!("Variable substitution failed: {e}"),
)
})?)
} else {
Cow::Borrowed(raw_src)
};
serde_yaml::from_str(&src)
.map_err(|e| Error::new(ErrorKind::Other, format!("Failed to parse config: {}", e)))
}
}
fn default_http_listen_address() -> SocketAddr {
"0.0.0.0:5000".parse().expect("default http listen address")
}
#[derive(Deserialize)]
pub struct OtelExporterConfig {
/// The OpenTelemetry service name to use
pub service_name: Option<String>,
/// The OpenTelemetry address to send events to if given.
pub address: String,
/// The ratio at which to sample spans when sending to OpenTelemetry. When not given it defaults
/// to always sending. If the OpenTelemetry address is not set, this will do nothing.
pub sample_ratio: Option<f64>,
}
#[derive(Clone, Debug, Default, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum LogLevel {
#[default]
Info,
Debug,
Trace,
}
impl ToString for LogLevel {
fn to_string(&self) -> String {
match self {
Self::Info => Level::INFO,
Self::Debug => Level::DEBUG,
Self::Trace => Level::TRACE,
}
.to_string()
}
}
#[derive(Clone, Debug, Default, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum LogFormat {
#[default]
Default,
Json,
}
#[derive(Deserialize)]
#[serde(untagged)]
pub enum SenderConfig {
#[cfg(any(
feature = "gcp-pubsub",
feature = "rabbitmq",
feature = "redis",
feature = "sqs"
))]
QueueConsumer(QueueConsumerConfig),
JsModule(JsModuleSenderConfig),
}
impl TryFrom<SenderConfig> for Box<dyn SenderInput> {
type Error = &'static str;
fn try_from(value: SenderConfig) -> Result<Self, Self::Error> {
match value {
#[cfg(any(
feature = "gcp-pubsub",
feature = "rabbitmq",
feature = "redis",
feature = "sqs"
))]
SenderConfig::QueueConsumer(backend) => backend.into_sender_input(),
SenderConfig::JsModule(inner) => inner.into_sender_input(),
}
}
}
#[derive(Deserialize)]
pub struct ReceiverConfig {
pub name: String,
pub input: ReceiverInputOpts,
#[serde(default)]
pub transformation: Option<TransformationConfig>,
pub output: ReceiverOut,
}
#[derive(Deserialize)]
#[serde(untagged)]
pub enum ReceiverOut {
#[cfg(any(
feature = "gcp-pubsub",
feature = "rabbitmq",
feature = "redis",
feature = "sqs"
))]
QueueProducer(QueueOutOpts),
}
impl ReceiverConfig {
pub async fn into_receiver_output(self) -> std::io::Result<Box<dyn ReceiverOutput>> {
match self.output {
ReceiverOut::QueueProducer(x) => {
into_receiver_output(self.name.clone(), x, &self.transformation)
.await
.map_err(Into::into)
}
}
}
}
#[derive(Deserialize)]
pub struct JsModuleSenderConfig {
pub name: String,
pub input: JsModuleSenderInputOpts,
#[serde(default)]
pub transformation: Option<TransformationConfig>,
pub output: SenderOutputOpts,
}
impl JsModuleSenderConfig {
fn into_sender_input(self) -> Result<Box<dyn SenderInput>, &'static str> {
// FIXME: need to make it so we can use latest deno for transformations before we can
// connect the new module code.
todo!()
}
}
#[derive(Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum JsModuleSenderInputOpts {
#[serde(rename = "js-module")]
JsModule { module_path: PathBuf },
}
#[cfg(test)]
mod tests;