-
Notifications
You must be signed in to change notification settings - Fork 3
feat(observability): pluggable span exporter #64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| pub mod future; | ||
| pub mod jsonschema; | ||
| pub mod metrics; | ||
| pub mod observability; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| mod trace; | ||
|
|
||
| use std::borrow::Cow; | ||
|
|
||
| use anyhow::{Context, Result}; | ||
| use fastrace::collector::Config as FastraceConfig; | ||
| use fastrace_opentelemetry::OpenTelemetryReporter; | ||
| use log::error; | ||
| use logforth::{ | ||
| append::{FastraceEvent, Stdout}, | ||
| filter::env_filter::EnvFilterBuilder, | ||
| layout::TextLayout, | ||
| }; | ||
| use metrics_exporter_otel::OpenTelemetryRecorder; | ||
| use opentelemetry::{InstrumentationScope, metrics::MeterProvider}; | ||
| use opentelemetry_otlp::SpanExporter; | ||
| use opentelemetry_sdk::{ | ||
| Resource, | ||
| metrics::{PeriodicReader, SdkMeterProvider}, | ||
| }; | ||
| use tokio::{sync::oneshot, task::JoinHandle}; | ||
| pub use trace::*; | ||
|
|
||
| use crate::utils; | ||
|
|
||
| pub const INSTRUMENTATION_NAME: &str = "aisix"; | ||
|
|
||
| pub fn shutdown_handler<F, Fut>(f: F) -> (oneshot::Sender<()>, tokio::task::JoinHandle<()>) | ||
| where | ||
| F: FnOnce() -> Fut + Send + 'static, | ||
| Fut: Future<Output = ()> + Send + 'static, | ||
| { | ||
|
bzp2010 marked this conversation as resolved.
|
||
| let (tx, rx) = oneshot::channel::<()>(); | ||
| let shutdown_handle = tokio::spawn(async move { | ||
| let _ = rx.await; | ||
| f().await; | ||
| }); | ||
| (tx, shutdown_handle) | ||
| } | ||
|
bzp2010 marked this conversation as resolved.
|
||
|
|
||
| /// Initialize observability logging. | ||
| pub fn init_observability_log() -> Result<(oneshot::Sender<()>, JoinHandle<()>)> { | ||
| logforth::starter_log::builder() | ||
| .dispatch(|d| { | ||
| d.filter(EnvFilterBuilder::from_default_env_or("info,opentelemetry_sdk=off").build()) | ||
| .append(Stdout::default().with_layout(TextLayout::default())) | ||
| }) | ||
| .dispatch(|d| { | ||
| d.filter(EnvFilterBuilder::from_default_env_or("info").build()) | ||
| .append(FastraceEvent::default()) | ||
| }) | ||
| .apply(); | ||
|
|
||
| Ok(shutdown_handler(|| async move { | ||
| logforth::core::default_logger().flush(); | ||
| logforth::core::default_logger().exit(); | ||
| })) | ||
| } | ||
|
|
||
| /// Initialize observability tracing. | ||
| pub fn init_observability_trace( | ||
| span_exporter: Option<BoxedSpanExporter>, | ||
| config: Option<FastraceConfig>, | ||
| ) -> Result<(oneshot::Sender<()>, JoinHandle<()>)> { | ||
| let reporter = OpenTelemetryReporter::new( | ||
| match span_exporter { | ||
| Some(exporter) => exporter, | ||
| None => BoxedSpanExporter::new( | ||
| SpanExporter::builder() | ||
| .build() | ||
| .context("failed to initialize otlp exporter")?, | ||
| ), | ||
| }, | ||
| Cow::Owned(Resource::builder().build()), | ||
| InstrumentationScope::builder(INSTRUMENTATION_NAME) | ||
| .with_version(env!("CARGO_PKG_VERSION")) | ||
| .build(), | ||
| ); | ||
| fastrace::set_reporter(reporter, config.unwrap_or_default()); | ||
|
|
||
| Ok(shutdown_handler(|| async move { fastrace::flush() })) | ||
| } | ||
|
|
||
| /// Initialize observability metrics. | ||
| pub fn init_observability_metric() -> Result<(oneshot::Sender<()>, JoinHandle<()>)> { | ||
| let exporter = opentelemetry_otlp::MetricExporter::builder().build()?; | ||
|
|
||
| let reader = PeriodicReader::builder(exporter).build(); | ||
|
|
||
| let meter_provider = SdkMeterProvider::builder().with_reader(reader).build(); | ||
| let meter = meter_provider.meter(INSTRUMENTATION_NAME); | ||
|
|
||
| metrics::set_global_recorder(OpenTelemetryRecorder::new(meter)) | ||
| .context("failed to initialize metrics recorder")?; | ||
| utils::metrics::describe_metrics(); | ||
|
|
||
| // shutting down signal handler | ||
| Ok(shutdown_handler(|| async move { | ||
| if let Err(e) = meter_provider.shutdown() { | ||
| error!("Error shutting down meter provider: {}", e); | ||
| } | ||
| })) | ||
| } | ||
|
|
||
| /// Initialize observability (logging, tracing, metrics). | ||
| /// | ||
| /// Returns `(shutdown_sender, shutdown_task_handle)`. | ||
| /// Call `shutdown_sender.send(())` to flush and shut down observability. | ||
| pub fn init_observability() -> Result<(oneshot::Sender<()>, tokio::task::JoinHandle<()>)> { | ||
| let (log_tx, log_shutdown_handle) = init_observability_log()?; | ||
| let (trace_tx, trace_shutdown_handle) = init_observability_trace(None, None)?; | ||
| let (metric_tx, metric_shutdown_handle) = init_observability_metric()?; | ||
|
|
||
| Ok(shutdown_handler(|| async move { | ||
| let _ = trace_tx.send(()); | ||
| let _ = trace_shutdown_handle.await; | ||
| let _ = metric_tx.send(()); | ||
| let _ = metric_shutdown_handle.await; | ||
| let _ = log_tx.send(()); | ||
| let _ = log_shutdown_handle.await; | ||
| })) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| use std::{fmt::Debug, time::Duration}; | ||
|
|
||
| use async_trait::async_trait; | ||
| use opentelemetry_sdk::{ | ||
| Resource, | ||
| error::OTelSdkResult, | ||
| trace::{SpanData, SpanExporter}, | ||
| }; | ||
|
|
||
| /// Type-erased span exporter trait object. | ||
| #[async_trait] | ||
| pub trait DynSpanExporter: Send + Sync + Debug { | ||
| async fn export(&self, batch: Vec<SpanData>) -> OTelSdkResult; | ||
|
|
||
| fn shutdown_with_timeout(&self, timeout: Duration) -> OTelSdkResult; | ||
|
|
||
| fn force_flush(&self) -> OTelSdkResult; | ||
|
|
||
| fn set_resource(&mut self, resource: &Resource); | ||
| } | ||
|
|
||
| #[async_trait] | ||
| impl<T: SpanExporter> DynSpanExporter for T { | ||
| async fn export(&self, batch: Vec<SpanData>) -> OTelSdkResult { | ||
| SpanExporter::export(self, batch).await | ||
| } | ||
|
|
||
| fn shutdown_with_timeout(&self, timeout: Duration) -> OTelSdkResult { | ||
| SpanExporter::shutdown_with_timeout(self, timeout) | ||
| } | ||
|
|
||
| fn force_flush(&self) -> OTelSdkResult { | ||
| SpanExporter::force_flush(self) | ||
| } | ||
|
|
||
| fn set_resource(&mut self, resource: &Resource) { | ||
| SpanExporter::set_resource(self, resource) | ||
| } | ||
| } | ||
|
|
||
| /// Type-erased span exporter adapter. | ||
| #[derive(Debug)] | ||
| pub struct BoxedSpanExporter(Box<dyn DynSpanExporter>); | ||
|
|
||
| impl BoxedSpanExporter { | ||
| pub fn new<T: SpanExporter + 'static>(span_exporter: T) -> Self { | ||
| Self(Box::new(span_exporter)) | ||
| } | ||
| } | ||
|
|
||
| impl SpanExporter for BoxedSpanExporter { | ||
| async fn export(&self, batch: Vec<SpanData>) -> OTelSdkResult { | ||
| self.0.export(batch).await | ||
| } | ||
|
|
||
| fn shutdown_with_timeout(&self, timeout: Duration) -> OTelSdkResult { | ||
| self.0.shutdown_with_timeout(timeout) | ||
| } | ||
|
|
||
| fn force_flush(&self) -> OTelSdkResult { | ||
| self.0.force_flush() | ||
| } | ||
|
|
||
| fn set_resource(&mut self, resource: &Resource) { | ||
| self.0.set_resource(resource) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.