scuffle_bootstrap_telemetry/
lib.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
use anyhow::Context;
use bytes::Bytes;
#[cfg(feature = "opentelemetry-logs")]
pub use opentelemetry_appender_tracing;
#[cfg(feature = "opentelemetry")]
pub use opentelemetry_sdk;
#[cfg(feature = "prometheus")]
pub use prometheus_client;
use scuffle_bootstrap::global::Global;
use scuffle_bootstrap::service::Service;
use scuffle_context::ContextFutExt;
use scuffle_http::backend::HttpServer;
use scuffle_http::body::IncomingBody;
#[cfg(feature = "opentelemetry-traces")]
pub use tracing_opentelemetry;

pub struct TelemetrySvc;

pub trait TelemetryConfig: Global {
	/// Return true if the service is enabled.
	fn enabled(&self) -> bool {
		true
	}

	/// Return the bind address to listen on.
	fn bind_address(&self) -> Option<std::net::SocketAddr> {
		None
	}

	/// Return the http server name.
	fn http_server_name(&self) -> &str {
		"scuffle-bootstrap-telemetry"
	}

	/// Return a health check to determine if the service is healthy.
	fn health_check(&self) -> impl std::future::Future<Output = Result<(), anyhow::Error>> + Send {
		std::future::ready(Ok(()))
	}

	/// Return a Prometheus metrics registry to scrape metrics from.
	#[cfg(feature = "prometheus")]
	fn prometheus_metrics_registry(&self) -> Option<&prometheus_client::registry::Registry> {
		None
	}

	/// Pass an OpenTelemetry instance to the service.
	/// If provided the service will flush and shutdown the OpenTelemetry
	/// instance when the service shuts down.
	#[cfg(feature = "opentelemetry")]
	fn opentelemetry(&self) -> Option<&opentelemetry::OpenTelemetry> {
		None
	}
}

impl<Global: TelemetryConfig> Service<Global> for TelemetrySvc {
	async fn enabled(&self, global: &std::sync::Arc<Global>) -> anyhow::Result<bool> {
		Ok(global.enabled())
	}

	async fn run(self, global: std::sync::Arc<Global>, ctx: scuffle_context::Context) -> anyhow::Result<()> {
		let server = global.bind_address().map(|addr| {
			scuffle_http::backend::tcp::TcpServerConfig::builder()
				.with_bind(addr)
				.with_server_name(global.http_server_name())
				.build()
				.into_server()
		});

		let global2 = global.clone();

		if let Some(server) = server {
			server
				.start(
					scuffle_http::svc::function_service(move |req| {
						let global = global2.clone();
						async move {
							match req.uri().path() {
								"/health" => health_check(&global, req).await,
								#[cfg(feature = "prometheus")]
								"/metrics" => metrics(&global, req).await,
								#[cfg(feature = "pprof")]
								"/pprof/cpu" => pprof(&global, req).await,
								#[cfg(feature = "opentelemetry")]
								"/opentelemetry/flush" => opentelemetry_flush(&global).await,
								_ => Ok(http::Response::builder()
									.status(http::StatusCode::NOT_FOUND)
									.body(http_body_util::Full::new(Bytes::from_static(b"not found")))?),
							}
						}
					}),
					1,
				)
				.await
				.context("server start")?;

			server.wait().with_context(&ctx).await.transpose().context("server wait")?;

			server.shutdown().await.context("server shutdown")?;
		} else {
			ctx.done().await;
		}

		#[cfg(feature = "opentelemetry")]
		if let Some(opentelemetry) = global.opentelemetry().cloned() {
			if opentelemetry.is_enabled() {
				tokio::task::spawn_blocking(move || opentelemetry.shutdown())
					.await
					.context("opentelemetry shutdown spawn")?
					.context("opentelemetry shutdown")?;
			}
		}

		Ok(())
	}
}

async fn health_check<G: TelemetryConfig>(
	global: &std::sync::Arc<G>,
	_: http::Request<IncomingBody>,
) -> Result<http::Response<http_body_util::Full<Bytes>>, scuffle_http::Error> {
	if let Err(err) = global.health_check().await {
		tracing::error!("health check failed: {err}");
		Ok(http::Response::builder()
			.status(http::StatusCode::INTERNAL_SERVER_ERROR)
			.body(http_body_util::Full::new(format!("{err:#}").into()))?)
	} else {
		Ok(http::Response::builder()
			.status(http::StatusCode::OK)
			.body(http_body_util::Full::new(Bytes::from_static(b"ok")))?)
	}
}

#[cfg(feature = "prometheus")]
async fn metrics<G: TelemetryConfig>(
	global: &std::sync::Arc<G>,
	_: http::Request<IncomingBody>,
) -> Result<http::Response<http_body_util::Full<Bytes>>, scuffle_http::Error> {
	if let Some(metrics) = global.prometheus_metrics_registry() {
		let mut buf = String::new();
		if prometheus_client::encoding::text::encode(&mut buf, metrics).is_err() {
			tracing::error!("metrics encode failed");
			return Ok(http::Response::builder()
				.status(http::StatusCode::INTERNAL_SERVER_ERROR)
				.body(http_body_util::Full::new("metrics encode failed".to_string().into()))?);
		}

		Ok(http::Response::builder()
			.status(http::StatusCode::OK)
			.body(http_body_util::Full::new(Bytes::from(buf)))?)
	} else {
		Ok(http::Response::builder()
			.status(http::StatusCode::NOT_FOUND)
			.body(http_body_util::Full::new(Bytes::from_static(b"not found")))?)
	}
}

#[cfg(feature = "pprof")]
async fn pprof<G: TelemetryConfig>(
	_: &std::sync::Arc<G>,
	req: http::Request<IncomingBody>,
) -> Result<http::Response<http_body_util::Full<Bytes>>, scuffle_http::Error> {
	let query = req.uri().query();
	let query = query.map(querystring::querify).into_iter().flatten();

	let mut freq = 100;
	let mut duration = std::time::Duration::from_secs(5);
	let mut ignore_list = Vec::new();

	for (key, value) in query {
		if key == "freq" {
			freq = match value.parse() {
				Ok(v) => v,
				Err(err) => {
					return Ok(http::Response::builder()
						.status(http::StatusCode::BAD_REQUEST)
						.body(http_body_util::Full::new(format!("invalid freq: {err:#}").into()))?);
				}
			};
		} else if key == "duration" {
			duration = match value.parse() {
				Ok(v) => std::time::Duration::from_secs(v),
				Err(err) => {
					return Ok(http::Response::builder()
						.status(http::StatusCode::BAD_REQUEST)
						.body(http_body_util::Full::new(format!("invalid duration: {err:#}").into()))?);
				}
			};
		} else if key == "ignore" {
			ignore_list.push(value);
		}
	}

	let cpu = scuffle_pprof::Cpu::new(freq, &ignore_list);

	match tokio::task::spawn_blocking(move || cpu.capture(duration)).await {
		Ok(Ok(data)) => Ok(http::Response::builder()
			.status(http::StatusCode::OK)
			.body(http_body_util::Full::new(Bytes::from(data)))?),
		Ok(Err(err)) => {
			tracing::error!("cpu capture failed: {err:#}");
			Ok(http::Response::builder()
				.status(http::StatusCode::INTERNAL_SERVER_ERROR)
				.body(http_body_util::Full::new(format!("{err:#}").into()))?)
		}
		Err(err) => {
			tracing::error!("cpu capture failed: {err:#}");
			Ok(http::Response::builder()
				.status(http::StatusCode::INTERNAL_SERVER_ERROR)
				.body(http_body_util::Full::new(format!("{err:#}").into()))?)
		}
	}
}

#[cfg(feature = "opentelemetry")]
async fn opentelemetry_flush<G: TelemetryConfig>(
	global: &std::sync::Arc<G>,
) -> Result<http::Response<http_body_util::Full<Bytes>>, scuffle_http::Error> {
	if let Some(opentelemetry) = global.opentelemetry().cloned() {
		if opentelemetry.is_enabled() {
			match tokio::task::spawn_blocking(move || opentelemetry.flush()).await {
				Ok(Ok(())) => Ok(http::Response::builder()
					.status(http::StatusCode::OK)
					.body(http_body_util::Full::new(Bytes::from_static(b"ok")))?),
				Ok(Err(err)) => {
					tracing::error!("opentelemetry flush failed: {err:#}");
					Ok(http::Response::builder()
						.status(http::StatusCode::INTERNAL_SERVER_ERROR)
						.body(http_body_util::Full::new(format!("{err:#}").into()))?)
				}
				Err(err) => {
					tracing::error!("opentelemetry flush spawn failed: {err:#}");
					Ok(http::Response::builder()
						.status(http::StatusCode::INTERNAL_SERVER_ERROR)
						.body(http_body_util::Full::new(format!("{err:#}").into()))?)
				}
			}
		} else {
			Ok(http::Response::builder()
				.status(http::StatusCode::OK)
				.body(http_body_util::Full::new(Bytes::from_static(b"ok")))?)
		}
	} else {
		Ok(http::Response::builder()
			.status(http::StatusCode::NOT_FOUND)
			.body(http_body_util::Full::new(Bytes::from_static(b"not found")))?)
	}
}

#[cfg(feature = "opentelemetry")]
pub mod opentelemetry {
	pub use ::opentelemetry::*;

	#[derive(Debug, thiserror::Error)]
	pub enum OpenTelemetryError {
		#[error("metrics: {0}")]
		Metrics(#[from] opentelemetry_sdk::metrics::MetricError),
		#[error("traces: {0}")]
		Traces(#[from] opentelemetry::trace::TraceError),
		#[error("logs: {0}")]
		Logs(#[from] opentelemetry_sdk::logs::LogError),
	}

	#[derive(Debug, Default, Clone)]
	pub struct OpenTelemetry {
		#[cfg(feature = "opentelemetry-metrics")]
		metrics: Option<opentelemetry_sdk::metrics::SdkMeterProvider>,
		#[cfg(feature = "opentelemetry-traces")]
		traces: Option<opentelemetry_sdk::trace::TracerProvider>,
		#[cfg(feature = "opentelemetry-logs")]
		logs: Option<opentelemetry_sdk::logs::LoggerProvider>,
	}

	impl OpenTelemetry {
		pub fn new() -> Self {
			Self::default()
		}

		pub fn is_enabled(&self) -> bool {
			#[cfg_attr(
				not(any(
					feature = "opentelemetry-metrics",
					feature = "opentelemetry-traces",
					feature = "opentelemetry-logs"
				)),
				allow(unused_mut)
			)]
			let mut enabled = false;
			#[cfg(feature = "opentelemetry-metrics")]
			{
				enabled |= self.metrics.is_some();
			}
			#[cfg(feature = "opentelemetry-traces")]
			{
				enabled |= self.traces.is_some();
			}
			#[cfg(feature = "opentelemetry-logs")]
			{
				enabled |= self.logs.is_some();
			}
			enabled
		}

		#[cfg(feature = "opentelemetry-metrics")]
		pub fn with_metrics(self, metrics: impl Into<Option<opentelemetry_sdk::metrics::SdkMeterProvider>>) -> Self {
			Self {
				metrics: metrics.into(),
				#[cfg(feature = "opentelemetry-traces")]
				traces: self.traces,
				#[cfg(feature = "opentelemetry-logs")]
				logs: self.logs,
			}
		}

		#[cfg(feature = "opentelemetry-traces")]
		pub fn with_traces(self, traces: impl Into<Option<opentelemetry_sdk::trace::TracerProvider>>) -> Self {
			Self {
				traces: traces.into(),
				#[cfg(feature = "opentelemetry-metrics")]
				metrics: self.metrics,
				#[cfg(feature = "opentelemetry-logs")]
				logs: self.logs,
			}
		}

		#[cfg(feature = "opentelemetry-logs")]
		pub fn with_logs(self, logs: impl Into<Option<opentelemetry_sdk::logs::LoggerProvider>>) -> Self {
			Self {
				logs: logs.into(),
				#[cfg(feature = "opentelemetry-traces")]
				traces: self.traces,
				#[cfg(feature = "opentelemetry-metrics")]
				metrics: self.metrics,
			}
		}

		/// Flushes all metrics, traces, and logs, warning; this blocks the
		/// current thread.
		pub fn flush(&self) -> Result<(), OpenTelemetryError> {
			#[cfg(feature = "opentelemetry-metrics")]
			if let Some(metrics) = &self.metrics {
				metrics.force_flush()?;
			}

			#[cfg(feature = "opentelemetry-traces")]
			if let Some(traces) = &self.traces {
				for r in traces.force_flush() {
					r?;
				}
			}

			#[cfg(feature = "opentelemetry-logs")]
			if let Some(logs) = &self.logs {
				for r in logs.force_flush() {
					r?;
				}
			}

			Ok(())
		}

		/// Shuts down all metrics, traces, and logs.
		pub fn shutdown(&self) -> Result<(), OpenTelemetryError> {
			#[cfg(feature = "opentelemetry-metrics")]
			if let Some(metrics) = &self.metrics {
				metrics.shutdown()?;
			}

			#[cfg(feature = "opentelemetry-traces")]
			if let Some(traces) = &self.traces {
				traces.shutdown()?;
			}

			#[cfg(feature = "opentelemetry-logs")]
			if let Some(logs) = &self.logs {
				logs.shutdown()?;
			}

			Ok(())
		}
	}
}