Skip to content

Commit

Permalink
add OpenTelemetry example
Browse files Browse the repository at this point in the history
  • Loading branch information
timzaak committed Jul 12, 2024
1 parent e60e19f commit 725997c
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
8 changes: 8 additions & 0 deletions server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ flate2 = "1.0"
# tokio trace and log
tracing = "0.1.21"# sync with warp
tracing-subscriber = { version = "0.3", features = ["env-filter"] }

#tracing-opentelemetry = "0.24"
#opentelemetry = "0.23"
#opentelemetry_sdk = { version = "0.23", features = ["rt-tokio", "metrics"] }
#opentelemetry-otlp = {version = "0.16", features = ["default", "metrics"]}
#opentelemetry-resource-detectors = {version = "0.2"}
#opentelemetry-semantic-conventions = { version = "0.15"}

# tokio cron
delay_timer = "0.11.6" # dashmap is same

Expand Down
72 changes: 72 additions & 0 deletions server/examples/try_opentelemetry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
fn main() {

}
/*
use std::time::Duration;
use opentelemetry::KeyValue;
use opentelemetry_otlp::{ExportConfig, Protocol, TonicExporterBuilder, WithExportConfig};
use opentelemetry_resource_detectors::{OsResourceDetector, ProcessResourceDetector};
use opentelemetry_sdk::{Resource};
use opentelemetry_sdk::metrics::reader::DefaultAggregationSelector;
use tracing::{Level};
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
use opentelemetry_sdk::resource::{ResourceDetector, SdkProvidedResourceDetector, TelemetryResourceDetector};
use opentelemetry_sdk::trace::config;
use opentelemetry_semantic_conventions::resource;
use tracing_subscriber::EnvFilter;
fn resource() -> Resource{
let os_resource = OsResourceDetector.detect(Duration::from_secs(0));
let process_resource = ProcessResourceDetector.detect(Duration::from_secs(0));
let telemetry_resource = TelemetryResourceDetector.detect(Duration::from_secs(0));
let sdk_resource = SdkProvidedResourceDetector.detect(Duration::from_secs(0));
let provided = Resource::new(vec![
KeyValue::new(resource::SERVICE_NAME, "spa-server"),
KeyValue::new(resource::SERVICE_VERSION, env!("CARGO_PKG_VERSION")),
]);
sdk_resource
.merge(&provided)
.merge(&telemetry_resource)
.merge(&os_resource)
.merge(&process_resource)
}
fn otlp_exporter() -> TonicExporterBuilder {
let export_config = ExportConfig {
endpoint: "http://localhost:4317".to_string(),
timeout: Duration::from_secs(3),
protocol: Protocol::Grpc
};
opentelemetry_otlp::new_exporter().tonic()
.with_export_config(export_config)
}
#[tokio::main]
async fn main() -> anyhow::Result<()>{
let tracer = opentelemetry_otlp::new_pipeline()
.tracing()
.with_exporter(otlp_exporter())
.with_trace_config(config().with_resource(resource()))
.install_batch(opentelemetry_sdk::runtime::Tokio)?;
let metrics = opentelemetry_otlp::new_pipeline().metrics(opentelemetry_sdk::runtime::Tokio)
.with_exporter(otlp_exporter())
.with_resource(resource())
.with_aggregation_selector(DefaultAggregationSelector::new()).build()?;
let filter = EnvFilter::builder()
.with_default_directive(Level::INFO.into())
.from_env_lossy();
tracing_subscriber::registry()
.with(filter)
.with(tracing_subscriber::fmt::layer().compact())// stdout_layer
.with(tracing_opentelemetry::MetricsLayer::new(metrics))
.with(tracing_opentelemetry::layer().with_tracer(tracer)) // traces_layer
.init();
Ok(())
}
*/

0 comments on commit 725997c

Please sign in to comment.