Skip to content

Commit

Permalink
feat(new relic): new crate to send logs to New Relic (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertohuertasm authored Oct 12, 2024
1 parent fa6583b commit 39c3b0c
Show file tree
Hide file tree
Showing 10 changed files with 585 additions and 5 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/release_dd_tracing_layer.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Release DD Tracing Layer

on:
push:
tags:
- 'v*.*.*'
workflow_dispatch:

jobs:
crates:
runs-on: ubuntu-latest
steps:
- name: Setup Rust
uses: hecrj/setup-rust-action@master
with:
rust-version: stable
- name: Checkout
uses: actions/checkout@v2
- name: Publish to crates.io
run: |
cargo login ${{secrets.crates_key}}
cargo publish -p dd-tracing-layer
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
name: Release
name: Release Log Tracing Layer

on:
push:
tags:
- "v*.*.*"
- 'v*.*.*-log-tracing-layer'
workflow_dispatch:

jobs:
crates:
runs-on: ubuntu-latest
Expand All @@ -20,4 +20,3 @@ jobs:
run: |
cargo login ${{secrets.crates_key}}
cargo publish -p log-tracing-layer
cargo publish -p dd-tracing-layer
22 changes: 22 additions & 0 deletions .github/workflows/release_nr_tracing_layer.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Release NR Tracing Layer

on:
push:
tags:
- 'v*.*.*-nr-tracing-layer'
workflow_dispatch:

jobs:
crates:
runs-on: ubuntu-latest
steps:
- name: Setup Rust
uses: hecrj/setup-rust-action@master
with:
rust-version: stable
- name: Checkout
uses: actions/checkout@v2
- name: Publish to crates.io
run: |
cargo login ${{secrets.crates_key}}
cargo publish -p nr-tracing-layer
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[workspace]
resolver = "2"
members = ["log-tracing-layer", "dd-tracing-layer"]
members = ["log-tracing-layer", "dd-tracing-layer", "nr-tracing-layer"]
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ It's a base library to easily build [tracing layers](https://docs.rs/tracing-sub
[![Crates.io](https://img.shields.io/crates/v/dd-tracing-layer?label=dd-tracing-layer&style=flat-square)](https://crates.io/crates/dd-tracing-layer)

Tracing layer that will send logs to the [Datadog Log API](https://docs.datadoghq.com/api/latest/logs/?code-lang=typescript#send-logs).

[![Crates.io](https://img.shields.io/crates/v/nr-tracing-layer?label=nr-tracing-layer&style=flat-square)](https://crates.io/crates/nr-tracing-layer)

Tracing layer that will send logs to the [New Relic Log API](https://docs.newrelic.com/docs/logs/get-started/get-started-log-management/).
32 changes: 32 additions & 0 deletions nr-tracing-layer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[package]
name = "nr-tracing-layer"
version = "0.4.0"
authors = ["Roberto Huertas <[email protected]>"]
description = "Send your logs to New Relic"
edition = "2021"
license = "MIT"
repository = "https://github.com/robertohuertasm/log-tracing-layer"
categories = ["development-tools", "asynchronous"]
keywords = ["tracing", "log", "new relic", "layer", "tracing-subscriber"]

[badges]
maintenance = { status = "actively-developed" }

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
log-tracing-layer = { path = "../log-tracing-layer", version = "0.4.0" }
tracing-subscriber = "0.3"
tracing = "0.1"
reqwest = { version = "0.12", features = ["gzip"] }
tokio = { version = "1", features = ["sync", "rt-multi-thread", "time"] }
serde_json = "1"
chrono = "0.4"
async-trait = "0.1"
async-recursion = "1.0"
libflate = "2.0"

[dev-dependencies]
dotenvy = "0.15.7"
tracing-subscriber = { version = "0.3", features = ["json", "registry"] }
httpmock = "0.7.0"
48 changes: 48 additions & 0 deletions nr-tracing-layer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# nr-tracing-layer

[![license](https://img.shields.io/crates/l/nr-tracing-layer?style=for-the-badge)](https://github.com/robertohuertasm/log-tracing-layer/blob/master/LICENSE)
[![crates.io](https://img.shields.io/crates/v/nr-tracing-layer?style=for-the-badge)](https://crates.io/crates/nr-tracing-layer)
[![docs.rs](https://img.shields.io/docsrs/nr-tracing-layer?style=for-the-badge)](https://docs.rs/nr-tracing-layer)

A [tracing layer](https://tokio.rs/tokio/topics/tracing) that sends logs to the [New Relic Log API](https://docs.newrelic.com/docs/logs/get-started/get-started-log-management/).


## Requirements

You'll need a [New Relic API Key](https://docs.newrelic.com/docs/apis/intro-apis/new-relic-api-keys/) for everything to work.

## Endpoint

By default, will try to send the logs to the `US1` region.

You can easily change the region or provide a custom URL if needed.

## Example

Here's a simple example of how to set it up and use it:

```rust
use nr_tracing_layer::NewRelicOptions;
use tracing_subscriber::prelude::*;
use tracing::{instrument, subscriber}

#[instrument]
fn log(msg: &'static str) {
tracing::info!("your message: {}", msg);
}

fn main() {
let options = NewRelicOptions::new("my-service", "my-new-relic-api-key")
.with_tags("env:dev");
let dd = nr_tracing_layer::create(options);
let subscriber = tracing_subscriber::registry()
.with(tracing_subscriber::fmt::Layer::new().json())
.with(dd);
let _s = subscriber::set_default(subscriber);
log("hello world!");
}
```

## Caveats

The layer will send the logs either 5 seconds after the last log is received or when the buffer arrives to 1000 logs. This is basically due to a limitation in the Datadog API.
36 changes: 36 additions & 0 deletions nr-tracing-layer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//! A tracing layer that sends logs to [New Relic](https://docs.newrelic.com/docs/logs/get-started/get-started-log-management/).
//!
//! ## Example
//!
//! ```rust
//! use nr_tracing_layer::NewRelicOptions;
//! use tracing_subscriber::prelude::*;
//! use tracing::{instrument, subscriber};
//!
//! #[instrument]
//! fn log(msg: &'static str) {
//! tracing::info!("your message: {}", msg);
//! }
//!
//! fn main() {
//! let options = NewRelicOptions::new("my-service", "my-new-relic-api-key")
//! .with_tags("env:dev");
//! let dd = nr_tracing_layer::create(options);
//! let subscriber = tracing_subscriber::registry()
//! .with(tracing_subscriber::fmt::Layer::new().json())
//! .with(dd);
//! let _s = subscriber::set_default(subscriber);
//! log("hello world!");
//! }
//!```
mod new_relic_ingestor;

pub use log_tracing_layer::LogLayer;
pub use new_relic_ingestor::{NewRelicOptions, Region};

/// Creates a log layer that will send logs to New Relic.
#[must_use]
pub fn create(options: NewRelicOptions) -> LogLayer {
let ingestor = new_relic_ingestor::NewRelicLogIngestor::new(options);
LogLayer::new(ingestor)
}
Loading

0 comments on commit 39c3b0c

Please sign in to comment.