Skip to content

Commit 07db74b

Browse files
Applying env-filter to OpenTelemetry span exporter (in Rust microservices) | Adding error handling for Elasticsearch response parsing
1 parent d770ef0 commit 07db74b

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ futures = "0.3.30"
4545
jsonwebtoken = "9.2.0"
4646
kafka = { version = "0.10.0", default-features = false }
4747
lazy_static = "1.4.0"
48-
opentelemetry = "0.21.0"
48+
opentelemetry = { version = "0.21.0", default-features = false, features = ["trace"] }
4949
opentelemetry-otlp = { version = "0.14.0", default-features = false, features = ["gzip-tonic", "trace", "grpc-tonic"] }
5050
opentelemetry_sdk = { version = "0.21.2", features = ["rt-tokio"] }
5151
postgres-types = { version = "0.2.6", features = ["with-time-0_3"] }

backend/lib.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,13 @@ pub mod utils {
8787
};
8888
use tokio::spawn;
8989
use tracing::{
90-
debug, debug_span, level_filters::LevelFilter, subscriber::set_global_default, warn, Span,
90+
debug, info_span, level_filters::LevelFilter, subscriber::set_global_default, warn, Span,
9191
};
9292
use tracing_opentelemetry::{OpenTelemetryLayer, OpenTelemetrySpanExt};
93-
use tracing_subscriber::fmt;
94-
use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Layer, Registry};
93+
use tracing_subscriber::{
94+
layer::{Layered, SubscriberExt},
95+
EnvFilter, Registry,
96+
};
9597
use warp::Filter;
9698

9799
pub fn setupObservability(serviceName: &'static str) {
@@ -102,8 +104,9 @@ pub mod utils {
102104

103105
set_global_default(
104106
Registry::default()
107+
.with(envFilter)
105108
.with(startTracer(serviceName))
106-
.with(fmt::layer().with_filter(envFilter)),
109+
.with(tracing_subscriber::fmt::layer()),
107110
)
108111
.expect("ERROR : Setting up global default tracing registry");
109112

@@ -127,7 +130,9 @@ pub mod utils {
127130
}
128131

129132
// startTracer creates an OpenTelemetry tracing pipeline and sets the global tracer.
130-
pub fn startTracer(serviceName: &'static str) -> OpenTelemetryLayer<Registry, Tracer> {
133+
pub fn startTracer(
134+
serviceName: &'static str,
135+
) -> OpenTelemetryLayer<Layered<EnvFilter, Registry>, Tracer> {
131136
opentelemetry::global::set_text_map_propagator(TraceContextPropagator::new());
132137

133138
let tracer = new_pipeline()
@@ -155,7 +160,7 @@ pub mod utils {
155160

156161
pub fn makeSpan(request: &Request<Body>) -> Span {
157162
let headers = request.headers();
158-
debug_span!(
163+
info_span!(
159164
"Incoming Request",
160165
?headers,
161166
trace_id = tracing::field::Empty

backend/microservices/profiles/adapters/elasticsearch.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,17 @@ impl ElasticsearchAdapter {
108108
// deserializeQueryProfilesResponse takes in the response returned from Elasticsearch for the query
109109
// done in ElasticsearchAdapter.searchProfiles and tries to deserialize it to Vec<ProfilePreview>.
110110
async fn deserializeQueryProfilesResponse(response: Response) -> Result<Vec<ProfilePreview>> {
111-
#[derive(Deserialize)]
111+
#[derive(Debug, Deserialize)]
112112
struct Response {
113113
hits: Hits,
114114
}
115115

116-
#[derive(Deserialize)]
116+
#[derive(Debug, Deserialize)]
117117
struct Hits {
118118
hits: Vec<Hit>,
119119
}
120120

121-
#[derive(Deserialize)]
121+
#[derive(Debug, Deserialize)]
122122
struct Hit {
123123
_id: String,
124124
_source: Source,
@@ -133,7 +133,7 @@ async fn deserializeQueryProfilesResponse(response: Response) -> Result<Vec<Prof
133133
let response = response.bytes().await?.to_vec();
134134
let response = from_utf8(&response)?;
135135

136-
let response: Response = serde_json::from_str(response).unwrap();
136+
let response: Response = serde_json::from_str(response)?;
137137

138138
let profiles = response
139139
.hits

0 commit comments

Comments
 (0)