Skip to content

Commit

Permalink
add logging-otlp example (#790)
Browse files Browse the repository at this point in the history
* Implement ToSchema for PhantomData<T>

* Add introduction of salvo-cli

* Optimization: take an uncommon name to avoid bugs with the same name as the user's

* Fix the code, pass the test

* Resolving Macro Expansion Errors

* add logging-otlp example
  • Loading branch information
fankaiLiu authored May 26, 2024
1 parent 0e89f11 commit 8767774
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
18 changes: 18 additions & 0 deletions examples/logging-otlp/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "example-logging-otlp"
version.workspace = true
edition.workspace = true
publish.workspace = true


[dependencies]
anyhow.workspace = true
salvo = { workspace = true, features = ["logging"] }
tokio = { workspace = true, features = ["macros"] }
tracing.workspace = true
tracing-subscriber ={ workspace = true, features = ["env-filter"] }
tracing-opentelemetry = "0.23.0"
opentelemetry = "0.22.0"
opentelemetry-otlp = { version = "0.15.0", features = ["tonic"] }
opentelemetry_sdk = { version = "0.22.1", features = ["rt-tokio"] }
tracing-appender = "0.2.3"
33 changes: 33 additions & 0 deletions examples/logging-otlp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Salvo with OpenTelemetry OTLP Example

## open-telemetry-otlp
[otlp](https://opentelemetry.io/) High-quality, ubiquitous, and portable telemetry to enable effective observability

This example demonstrates how to integrate the Salvo web framework with OpenTelemetry OTLP for tracing and logging, using Jaeger as the backend for visualization.

## Prerequisites

- Rust and Cargo installed on your system.
- Docker installed for running Jaeger.

## Running Jaeger with Docker

Before running the application, you need to start Jaeger using Docker. This will set up Jaeger's UI and OTLP collector to receive telemetry data.

```bash
docker run -d -p 16686:16686 -p 4317:4317 -e COLLECTOR_OTLP_ENABLED=true jaegertracing/all-in-one:latest
```


## Running the Application
Build and run the application:
Access the example endpoint:
```bash
curl http://localhost:5800/
You should see "Hello World" as the response. Traces for requests made to this endpoint will be sent to Jaeger.
```
## Viewing Traces
Open Jaeger UI in a web browser:
http://localhost:16686
Navigate to the Jaeger UI to view the traces and detailed telemetry data generated by interacting with the Salvo application.

76 changes: 76 additions & 0 deletions examples/logging-otlp/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use anyhow::Result;
use opentelemetry::KeyValue;
use opentelemetry_otlp::WithExportConfig;
use opentelemetry_sdk::{
runtime,
trace::{self, RandomIdGenerator, Tracer},
Resource,
};
use salvo::logging::Logger;
use salvo::prelude::*;
use tracing::{debug, info, instrument, level_filters::LevelFilter, warn};
use tracing_subscriber::{
fmt::{self, format::FmtSpan},
layer::SubscriberExt,
util::SubscriberInitExt,
Layer,
};

#[instrument(fields(http.uri = req.uri().path(), http.method = req.method().as_str()))]
#[handler]
async fn hello(req: &mut Request) -> &'static str {
"Hello World"
}

#[tokio::main]
async fn main() -> Result<()> {
// console layer for tracing-subscriber
let console = fmt::Layer::new()
.with_span_events(FmtSpan::CLOSE)
.pretty()
.with_filter(LevelFilter::DEBUG);

// file appender layer for tracing-subscriber
let file_appender = tracing_appender::rolling::daily("./logs", "salvo.log");
let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender);
let file = fmt::Layer::new()
.with_writer(non_blocking)
.pretty()
.with_filter(LevelFilter::INFO);

// opentelemetry tracing layer for tracing-subscriber
let tracer = init_tracer()?;
let opentelemetry = tracing_opentelemetry::layer().with_tracer(tracer);

tracing_subscriber::registry()
.with(console)
.with(file)
.with(opentelemetry)
.init();

let router = Router::new().get(hello);
let service = Service::new(router).hoop(Logger::new());

let acceptor = TcpListener::new("0.0.0.0:5800").bind().await;
Server::new(acceptor).serve(service).await;
Ok(())
}

fn init_tracer() -> anyhow::Result<Tracer> {
let tracer = opentelemetry_otlp::new_pipeline()
.tracing()
.with_exporter(
opentelemetry_otlp::new_exporter()
.tonic()
.with_endpoint("http://localhost:4317"),
)
.with_trace_config(
trace::config()
.with_id_generator(RandomIdGenerator::default())
.with_max_events_per_span(32)
.with_max_attributes_per_span(64)
.with_resource(Resource::new(vec![KeyValue::new("service.name", "salvo-tracing")])),
)
.install_batch(runtime::Tokio)?;
Ok(tracer)
}

0 comments on commit 8767774

Please sign in to comment.