Skip to content

Commit

Permalink
Configure metrics for Rust example (#682)
Browse files Browse the repository at this point in the history
  • Loading branch information
alanwest authored Sep 30, 2024
1 parent 65b286f commit 792e925
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
2 changes: 1 addition & 1 deletion getting-started-guides/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"

[dependencies]
actix-web = "4.8.0"
actix-web-opentelemetry = "0.19.0"
actix-web-opentelemetry = { version = "0.19.0", features = ["metrics"] }
opentelemetry = "0.24.0"
opentelemetry-otlp = { version = "0.17.0", features = ["tls-roots", "gzip-tonic"] }
opentelemetry_sdk = { version = "0.24.1", features = ["rt-tokio-current-thread"] }
Expand Down
40 changes: 31 additions & 9 deletions getting-started-guides/rust/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::time::Duration;

use actix_web::{web, App, HttpServer};
use actix_web_opentelemetry::RequestTracing;
use actix_web_opentelemetry::{RequestMetrics, RequestTracing};
use opentelemetry::{global, KeyValue};
use opentelemetry::trace::{Span, Status, Tracer};
use opentelemetry_sdk::runtime;
use opentelemetry_sdk::{runtime, Resource};
use opentelemetry_sdk::propagation::TraceContextPropagator;
use opentelemetry_sdk::resource::{EnvResourceDetector, ResourceDetector, SdkProvidedResourceDetector, TelemetryResourceDetector};
use opentelemetry_sdk::trace::Config;
Expand Down Expand Up @@ -57,7 +57,10 @@ fn compute_fibonacci(n: i64) -> Result<i64, Box<dyn std::error::Error>> {
if n < 1 || n > 90 {
let err_msg = "n must be between 1 and 90";
span.set_status(Status::error(err_msg));

// TODO: Figure out how to record the error
// span.record_error(err);

return Err(Box::from(err_msg));
}

Expand All @@ -78,37 +81,56 @@ fn compute_fibonacci(n: i64) -> Result<i64, Box<dyn std::error::Error>> {
Ok(result)
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
global::set_text_map_propagator(TraceContextPropagator::new());

fn create_resource() -> Resource {
let sdk_provided_resource = SdkProvidedResourceDetector.detect(Duration::from_secs(0));
let env_resource = EnvResourceDetector::new().detect(Duration::from_secs(0));
let telemetry_resource = TelemetryResourceDetector.detect(Duration::from_secs(0));
let resource = sdk_provided_resource
sdk_provided_resource
.merge(&env_resource)
.merge(&telemetry_resource);
.merge(&telemetry_resource)
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
global::set_text_map_propagator(TraceContextPropagator::new());



let tracer_provider = opentelemetry_otlp::new_pipeline()
.tracing()
.with_exporter(opentelemetry_otlp::new_exporter().tonic()
.with_tls_config(tonic::transport::ClientTlsConfig::new().with_native_roots()))
.with_trace_config(Config::default().with_resource(resource))
.with_trace_config(Config::default().with_resource(create_resource()))
.install_batch(runtime::TokioCurrentThread)
.expect("failed to initialize the trace pipeline");

global::set_tracer_provider(tracer_provider);

let meter_provider = opentelemetry_otlp::new_pipeline()
.metrics(runtime::TokioCurrentThread)
.with_exporter(opentelemetry_otlp::new_exporter().tonic()
.with_tls_config(tonic::transport::ClientTlsConfig::new().with_native_roots()))
.with_resource(create_resource())
.with_delta_temporality()
.build()
.expect("failed to initialize the metrics pipeline");

global::set_meter_provider(meter_provider);

HttpServer::new(|| {
App::new()
.wrap(RequestTracing::new())
.wrap(RequestMetrics::default())
.route("/fibonacci", web::get().to(fibonacci))
})
.bind(("0.0.0.0", 8080))?
.run()
.await?;

global::shutdown_tracer_provider();

// TODO: Figure out how to shutdown the meter provider cleanly
// meter_provider.shutdown()?;

Ok({})
}

0 comments on commit 792e925

Please sign in to comment.