#tracing #layer #slack #async #filter

tracing-layer-slack

Send filtered tracing events to Slack

8 unstable releases (3 breaking)

0.4.0 Oct 28, 2021
0.3.1 Aug 17, 2021
0.2.2 Aug 17, 2021
0.1.1 Aug 2, 2021

#151 in Asynchronous

Download history 24/week @ 2021-09-26 40/week @ 2021-10-03 14/week @ 2021-10-10 106/week @ 2021-10-17 56/week @ 2021-10-24 13/week @ 2021-10-31 17/week @ 2021-11-07 7/week @ 2021-11-14 57/week @ 2021-11-21 16/week @ 2021-11-28 35/week @ 2021-12-05 8/week @ 2021-12-12 30/week @ 2021-12-19 99/week @ 2021-12-26 58/week @ 2022-01-02 16/week @ 2022-01-09

206 downloads per month

Apache-2.0

25KB
406 lines

tracing-layer-slack

Docs Crates.io

tracing-layer-slack provides a Layer implementation for sending tracing events to Slack.

Synopsis

SlackLayer sends POST requests via tokio and reqwest to a Slack Webhook URL for each new tracing event. The format of the text field is statically defined.

This layer also looks for an optional JsonStorageLayer extension on the parent span of each event. This extension may contain additional contextual information for the parent span of an event, which is included into the Slack message.

Installation

Configure the dependencies and pull directly from GitHub:

[dependencies]
tokio = "1.0"
tracing = "0.1"
tracing-futures = "0.2"
tracing-layer-slack = "0.4"

Examples

See the full list of examples in examples/.

Simple

In this simple example, a layer is created using Slack configuration in the environment. An orphaned event (one with no parent span) and an event occurring within a span are created in three separate futures, and a number of messages are sent quickly to Slack.

Slack Messages

This screenshots shows the first three Slack messages sent while running this example. More messages are sent but were truncated from this image.

Screenshot demonstrating the current formatter implementation for events sent as Slack messages

Code example

Run this example locally using the following commands:

$ git clone https://github.com/seanpianka/tracing-layer-slack.git
$ cd tracing-layer-slack
$ cargo run --example simple

You must have Slack configuration exported in the environment.

Source
use regex::Regex;
use tracing::{info, warn, instrument};
use tracing_subscriber::{layer::SubscriberExt, Registry};

use tracing_layer_slack::{EventFilters, SlackLayer};

#[instrument]
pub async fn create_user(id: u64) -> u64 {
    network_io(id).await;
    info!(param = id, "A user was created");
    id
}

#[instrument]
pub async fn network_io(id: u64) {
    warn!(user_id = id, "some network io happened");
}

pub async fn controller() {
    info!("Orphan event without a parent span");
    let (id1, id2, id3) = tokio::join!(create_user(2), create_user(4), create_user(6));
}

#[tokio::main]
async fn main() {
    // Only show events from where this example code is the target.
    let target_to_filter: EventFilters = Regex::new("simple").unwrap().into();

    // Initialize the layer and an async background task for sending our Slack messages.
    let (slack_layer, background_worker) = SlackLayer::builder(target_to_filter).build();
    // Initialize the global default subscriber for tracing events.
    let subscriber = Registry::default().with(slack_layer);
    tracing::subscriber::set_global_default(subscriber).unwrap();

    // Perform our application code that needs tracing and Slack messages.
    controller().await;
    // Waits for all Slack messages to be sent before exiting.
    background_worker.shutdown().await;
}

Dependencies

~8–12MB
~260K SLoC