1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-02-23 02:43:16 +01:00
Luca Palmieri 7da6ea91ac
Adopt OpenTelemetry's semantic convention (#15)
* Add http.route.

* Align all fields with OpenTelemetry's semantic conventions.

* Add span kind.

* Emit event for errors.
Add OTEL status code.

* Create otel.status_code field as empty.

* Fix errors.

* Add (feature-gated) support for OpenTelemetry span propagation.

* Capture the trace id as an attribute on the span.

* Change message.

* Log the newly-generated trace id if there is no parent context.

* Define a root_span macro as a stepping stone to allow crate users to add their own fields to the root span.

* Add comments.

* mut is no longer necessary.

* Allow users to customise generation of the root span. Split recording fields on span end from emission of log record. Make log record on error optional via feature flag.

* Provide constructor + default implementation.

* Explode into multiple modules.
Fix various paths/private imports in root_span.

* Rename module to root_span_macro.

* Add a new extractor to retrieve the root span.

* Document crate.

* Docs!

* Add section on OTEL.

* Mention actix-web-opentelemetry.

* Add OpenTelemetry example.

* Improve readme.

* Add custom root span example.

Co-authored-by: LukeMathWalker <contact@palmieri.com>
2021-04-25 12:19:27 +01:00
2020-09-27 16:27:51 +01:00
2020-09-27 16:27:51 +01:00
2020-09-27 16:27:51 +01:00
2020-09-27 16:27:51 +01:00

tracing-actix-web

Structured logging for actix-web applications.

tracing-actix-web provides TracingLogger, a middleware to log request and response info when using the actix-web framework.

TracingLogger is designed as a drop-in replacement of actix-web's Logger.

Logger is built on top of the log crate: you need to use regular expressions to parse the request information out of the logged message.

TracingLogger relies on tracing, a modern instrumentation framework for structured logging: all request information is captured as a machine-parsable set of key-value pairs.
It also enables propagation of context information to children spans.

How to install

Add tracing-actix-web to your dependencies:

[dependencies]
# ...
tracing-actix-web = "0.2"

If you are using cargo-edit, run

cargo add tracing-actix-web

tracing-actix-web 0.2.x depends on actix-web 3.x.x.
If you are using actix-web 2.x.x use tracing-actix-web 0.1.x.

Usage example

Register TracingLogger as a middleware for your application using .wrap on App.
Add a Subscriber implementation to output logs to the console.

use actix_web::middleware::Logger;
use actix_web::App;
use tracing::{Subscriber, subscriber::set_global_default};
use tracing_log::LogTracer;
use tracing_actix_web::TracingLogger;
use tracing_bunyan_formatter::{BunyanFormattingLayer, JsonStorageLayer};
use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry};

/// Compose multiple layers into a `tracing`'s subscriber.
pub fn get_subscriber(
    name: String,
    env_filter: String
) -> impl Subscriber + Send + Sync {
    let env_filter = EnvFilter::try_from_default_env()
        .unwrap_or(EnvFilter::new(env_filter));
    let formatting_layer = BunyanFormattingLayer::new(
        name.into(),
        std::io::stdout
    );
    Registry::default()
        .with(env_filter)
        .with(JsonStorageLayer)
        .with(formatting_layer)
}

/// Register a subscriber as global default to process span data.
///
/// It should only be called once!
pub fn init_subscriber(subscriber: impl Subscriber + Send + Sync) {
    LogTracer::init().expect("Failed to set logger");
    set_global_default(subscriber).expect("Failed to set subscriber");
}

fn main() {
    let subscriber = get_subscriber("app".into(), "info".into());
    init_subscriber(subscriber);

    let app = App::new().wrap(TracingLogger);
}
Description
No description provided
Readme 30 MiB
Languages
Rust 98.6%
Just 0.9%
HTML 0.5%