Skip to content

Commit

Permalink
Use opentelemetry-appender-tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
julianocosta89 committed Mar 14, 2024
1 parent 2adbfd5 commit dc6493e
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 52 deletions.
21 changes: 8 additions & 13 deletions src/shippingservice/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/shippingservice/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ prost = "0.12"
prost-types = "0.12"
tokio = { version = "1.36", features = ["macros", "rt-multi-thread"] }
opentelemetry = { version = "0.21", features = ["trace", "metrics", "logs"] }
opentelemetry-appender-log = "0.2.0"
opentelemetry-appender-tracing = "0.2.0"
opentelemetry_sdk = { version = "0.21", features = ["rt-tokio", "metrics", "logs_level_enabled"]}
opentelemetry-semantic-conventions = "0.13.0"
opentelemetry-http = "0.10.0"
Expand Down
13 changes: 8 additions & 5 deletions src/shippingservice/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,25 @@ use shipping_service::shop::shipping_service_server::ShippingServiceServer;
use shipping_service::ShippingServer;

mod telemetry;
use telemetry::configure_global_logger;
use telemetry::init_logger;
use telemetry::init_reqwest_tracing;
use telemetry::init_tracer;
use telemetry::init_metrics;

use telemetry::init_tracer;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let (mut health_reporter, health_service) = tonic_health::server::health_reporter();
health_reporter
.set_serving::<ShippingServiceServer<ShippingServer>>()
.await;

init_logger();
init_reqwest_tracing(init_tracer()?)?;
let tracer = init_tracer()?;
let _ = init_metrics()?;


let _ = init_logger();
configure_global_logger(tracer);

info!("OTel pipeline created");
let port = env::var("SHIPPING_SERVICE_PORT").expect("$SHIPPING_SERVICE_PORT is not set");
let addr = format!("0.0.0.0:{}", port).parse()?;
Expand Down
4 changes: 1 addition & 3 deletions src/shippingservice/src/shipping_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,7 @@ impl ShippingService for ShippingServer {
#[cfg(test)]
mod tests {
use super::{
shop::shipping_service_server::ShippingService,
shop::ShipOrderRequest,
ShippingServer,
shop::shipping_service_server::ShippingService, shop::ShipOrderRequest, ShippingServer,
};
use tonic::Request;
use uuid::Uuid;
Expand Down
4 changes: 2 additions & 2 deletions src/shippingservice/src/shipping_service/quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use core::fmt;
use std::{collections::HashMap, env};

use log::debug;
use log::info;
use opentelemetry::{trace::get_active_span, KeyValue};
use reqwest_middleware::ClientBuilder;
use reqwest_tracing::{SpanBackendWithUrl, TracingMiddleware};
Expand Down Expand Up @@ -62,7 +62,7 @@ async fn request_quote(count: u32) -> Result<f64, Box<dyn std::error::Error>> {
.text_with_charset("utf-8")
.await?;

debug!("{:?}", resp);
info!("Received quote: {:?}", resp);

match resp.parse::<f64>() {
Ok(f) => Ok(f),
Expand Down
2 changes: 1 addition & 1 deletion src/shippingservice/src/telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ mod resources_conf;
pub use resources_conf::get_resource_attr;

mod traces_conf;
pub use traces_conf::init_reqwest_tracing;
pub use traces_conf::init_tracer;

mod logs_conf;
pub use logs_conf::configure_global_logger;
pub use logs_conf::init_logger;

mod metrics_conf;
Expand Down
32 changes: 16 additions & 16 deletions src/shippingservice/src/telemetry/logs_conf.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

use log::Level;
use opentelemetry::global::logger_provider;
use opentelemetry_appender_log::OpenTelemetryLogBridge;

use opentelemetry_otlp;
use opentelemetry_appender_tracing::layer::OpenTelemetryTracingBridge;
use opentelemetry_sdk::{logs::Config, runtime};

use tracing_subscriber::prelude::*;

use opentelemetry_sdk::logs as sdklogs;

use super::get_resource_attr;

pub fn init_logger() {
let _ = opentelemetry_otlp::new_pipeline()
pub fn init_logger() -> Result<sdklogs::Logger, opentelemetry::logs::LogError> {
opentelemetry_otlp::new_pipeline()
.logging()
.with_log_config(Config::default().with_resource(get_resource_attr()))
.with_exporter(opentelemetry_otlp::new_exporter().tonic())
.install_batch(runtime::Tokio);

set_log_bridge();
.install_batch(runtime::Tokio)
}

fn set_log_bridge() {
let logger_provider = logger_provider();

let otel_log_appender = OpenTelemetryLogBridge::new(&logger_provider);
log::set_boxed_logger(Box::new(otel_log_appender)).unwrap();
log::set_max_level(Level::Info.to_level_filter());
pub fn configure_global_logger(tracer: opentelemetry_sdk::trace::Tracer) {
let telemetry = tracing_opentelemetry::layer().with_tracer(tracer);
let logger_provider = opentelemetry::global::logger_provider();
let layer = OpenTelemetryTracingBridge::new(&logger_provider);
let _ = tracing_subscriber::registry()
.with(layer)
.with(telemetry)
.init();
}
11 changes: 0 additions & 11 deletions src/shippingservice/src/telemetry/traces_conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ use opentelemetry::{global, trace::TraceError};
use opentelemetry_otlp;
use opentelemetry_sdk::{propagation::TraceContextPropagator, runtime, trace as sdktrace};

use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::Registry;

use super::get_resource_attr;

pub fn init_tracer() -> Result<sdktrace::Tracer, TraceError> {
Expand All @@ -19,11 +16,3 @@ pub fn init_tracer() -> Result<sdktrace::Tracer, TraceError> {
.with_trace_config(sdktrace::config().with_resource(get_resource_attr()))
.install_batch(runtime::Tokio)
}

pub fn init_reqwest_tracing(
tracer: sdktrace::Tracer,
) -> Result<(), tracing::subscriber::SetGlobalDefaultError> {
let telemetry = tracing_opentelemetry::layer().with_tracer(tracer);
let subscriber = Registry::default().with(telemetry);
tracing::subscriber::set_global_default(subscriber)
}

0 comments on commit dc6493e

Please sign in to comment.