Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prepare for release #69

Merged
merged 10 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .mise.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
rust = "1.74"
17 changes: 16 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

---

## [0.9.0] - 2024-11-25

### Added

- New `event_with_options!` macro, [see the original repo](https://github.com/mcasper/dogstatsd-rs) for more details

### Changed

- Bump `dogstatsd` to 0.12
- Bumped MSRV to 1.74

---

## [0.8.0] - 2024-06-21

## Changed
Expand Down Expand Up @@ -151,7 +164,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0



[Unreleased]: https://github.com/primait/prima_datadog.rs/compare/0.8.0...HEAD

[Unreleased]: https://github.com/primait/prima_datadog.rs/compare/0.9.0...HEAD
[0.9.0]: https://github.com/primait/prima_datadog.rs/compare/0.8.0...0.9.0
[0.8.0]: https://github.com/primait/prima_datadog.rs/compare/0.7.2...0.8.0
[0.7.2]: https://github.com/primait/prima_datadog.rs/compare/0.7.1...0.7.2
[0.7.1]: https://github.com/primait/prima_datadog.rs/compare/0.7.0...0.7.1
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description = "An opinionated library to share code and approach to Datadog logg
edition = "2018"
license = "MIT"
name = "prima_datadog"
version = "0.8.0"
version = "0.9.0"

[features]
default = []
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM rust:1.72.0
FROM rust:1.74.0

WORKDIR /code

Expand Down
13 changes: 13 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::future::Future;

use async_trait::async_trait;
use dogstatsd::EventOptions;

use crate::{ServiceCheckOptions, ServiceStatus, TagsProvider};

Expand Down Expand Up @@ -75,6 +76,11 @@ pub trait DogstatsdClient {
fn event<S>(&self, title: &str, text: &str, tags: impl TagsProvider<S>)
where
S: AsRef<str>;

/// Send a custom event as a title and a body
fn event_with_options<S>(&self, title: &str, text: &str, tags: impl TagsProvider<S>, options: Option<EventOptions>)
where
S: AsRef<str>;
}

#[async_trait]
Expand Down Expand Up @@ -172,4 +178,11 @@ impl DogstatsdClient for dogstatsd::Client {
{
let _ = self.event(title, text, tags.as_ref());
}

fn event_with_options<S>(&self, title: &str, text: &str, tags: impl TagsProvider<S>, options: Option<EventOptions>)
where
S: AsRef<str>,
{
let _ = self.event_with_options(title, text, tags.as_ref(), options);
}
}
30 changes: 30 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
//! service_check!("test", ServiceStatus::OK, ServiceCheckOptions::default());
//! # event!("test", "test event");
//! event!("test", "test event"; "some" => "data");
//! # event_with_options!("test", "test event", EventOptions::new());
//! event_with_options!("test", "test event", EventOptions::new(); "some" => "data");
//! ```
//!
//! This is an example of a custom metric, in this case based on an enum type, but it can really be
Expand Down Expand Up @@ -114,6 +116,7 @@
use std::future::Future;

use configuration::Configuration;
use dogstatsd::EventOptions;
pub use dogstatsd::{ServiceCheckOptions, ServiceStatus};
use once_cell::sync::OnceCell;

Expand Down Expand Up @@ -295,6 +298,18 @@ impl Datadog<dogstatsd::Client> {
}
}

/// Send a custom event as a title, a body and some options
pub fn event_with_options<S: AsRef<str>>(
metric: impl AsRef<str>,
text: impl AsRef<str>,
tags: impl TagsProvider<S>,
options: Option<EventOptions>,
) {
if let Some(instance) = INSTANCE.get() {
instance.do_event_with_options(metric.as_ref(), text.as_ref(), tags, options);
}
}

/// Acquire a timing guard.
/// When this guard is dropped, it will emit a timing metric for the duration it
/// existed. The metric name is metric, and the tags are tags.
Expand Down Expand Up @@ -455,4 +470,19 @@ impl<C: DogstatsdClient> Datadog<C> {
self.tag_tracker.track(&self.inner, metric.as_ref(), tags),
);
}

pub(crate) fn do_event_with_options<S: AsRef<str>>(
&self,
metric: impl AsRef<str>,
text: impl AsRef<str>,
tags: impl TagsProvider<S>,
options: Option<EventOptions>,
) {
self.inner.event_with_options(
metric.as_ref(),
text.as_ref(),
self.tag_tracker.track(&self.inner, metric.as_ref(), tags),
options,
);
}
}
41 changes: 41 additions & 0 deletions src/macros/event_with_options.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/// Send a custom event as a title and a body
/// NOTE: Try to minimise variation in tag values (avoid things like timestamps or ids). See note in lib docs!
#[macro_export]
macro_rules! event_with_options {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT about extending the event! Instead of creating a new one? It could receive the EventOptions as an argument and pass it to $crate::Datadog::event_with_options instead of $crate::Datadog::event.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was debating the two options, I think it may make sense to expose just one macro

($stat:path, $text:expr) => {
$crate::Datadog::event_with_options($stat.as_ref(), $text, $crate::EMPTY_TAGS, None);
};
($stat:path, $text:expr, $options:expr) => {
$crate::Datadog::event_with_options($stat.as_ref(), $text, $crate::EMPTY_TAGS, Some($options));
};
($stat:expr, $text:expr) => {
$crate::Datadog::event_with_options($stat, $text, $crate::EMPTY_TAGS, None);
};
($stat:expr, $text:expr, $options:expr) => {
$crate::Datadog::event_with_options($stat, $text, $crate::EMPTY_TAGS, Some($options));
};
($stat:path, $text:expr; $( $key:literal => $value:literal ), *) => {
$crate::Datadog::event_with_options($stat.as_ref(), $text, &[$(::core::concat!($key, ":", $value)), *], None);
};
($stat:path, $text:expr, $options:expr; $( $key:literal => $value:literal ), *) => {
$crate::Datadog::event_with_options($stat.as_ref(), $text, &[$(::core::concat!($key, ":", $value)), *], Some($options));
};
($stat:expr, $text:expr; $( $key:literal => $value:literal ), *) => {
$crate::Datadog::event_with_options($stat, $text, &[$(::core::concat!($key, ":", $value)), *], None);
};
($stat:expr, $text:expr, $options:expr; $( $key:literal => $value:literal ), *) => {
$crate::Datadog::event_with_options($stat, $text, &[$(::core::concat!($key, ":", $value)), *], Some($options));
};
($stat:path, $text:expr; $( $key:expr => $value:expr ), *) => {
$crate::Datadog::event_with_options($stat.as_ref(), $text, &[$(::std::format!("{}:{}", $key, $value).as_str()), *], None);
};
($stat:path, $text:expr, $options:expr; $( $key:expr => $value:expr ), *) => {
$crate::Datadog::event_with_options($stat.as_ref(), $text, &[$(::std::format!("{}:{}", $key, $value).as_str()), *], Some($options));
};
($stat:expr, $text:expr; $( $key:expr => $value:expr ), *) => {
$crate::Datadog::event_with_options($stat, $text, &[$(::std::format!("{}:{}", $key, $value).as_str()), *], None);
};
($stat:expr, $text:expr, $options:expr; $( $key:expr => $value:expr ), *) => {
$crate::Datadog::event_with_options($stat, $text, &[$(::std::format!("{}:{}", $key, $value).as_str()), *], Some($options));
};
}
1 change: 1 addition & 0 deletions src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod count;
mod decr;
mod distribution;
mod event;
mod event_with_options;
mod gauge;
mod histogram;
mod incr;
Expand Down
91 changes: 91 additions & 0 deletions src/tests/event_with_options.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
use dogstatsd::EventAlertType;
use dogstatsd::EventOptions;
use dogstatsd::EventPriority;

use crate::event_with_options;
use crate::tests::mocks;
use crate::tests::TestEvent;
use crate::Datadog;
use crate::TagTrackerConfiguration;
use crate::EMPTY_TAGS;

#[test]
pub fn event_with_options_with_literal() {
let mock = mocks::event_with_options_mock("test", "test_value", &[], None);
Datadog::new(mock, TagTrackerConfiguration::new()).do_event_with_options("test", "test_value", EMPTY_TAGS, None);
}

#[test]
pub fn event_with_options_with_type() {
let mock = mocks::event_with_options_mock("test1_event", "test_value", &[], None);
Datadog::new(mock, TagTrackerConfiguration::new()).do_event_with_options(
TestEvent::Test1,
"test_value",
EMPTY_TAGS,
None,
);
}

#[test]
pub fn event_with_options_with_literal_and_tags() {
let mock = mocks::event_with_options_mock("test", "test_value", &["added:tag", "env:test"], None);
Datadog::new(mock, TagTrackerConfiguration::new()).do_event_with_options(
"test",
"test_value",
vec!["added:tag".to_string()],
None,
);
}

#[test]
pub fn event_with_options_with_type_and_tags() {
let mock = mocks::event_with_options_mock("test1_event", "test_value", &["added:tag", "env:test"], None);
Datadog::new(mock, TagTrackerConfiguration::new()).do_event_with_options(
TestEvent::Test1,
"test_value",
vec!["added:tag".to_string()],
None,
);
}

#[test]
pub fn event_with_options_with_options() {
let options = Some(
EventOptions::new()
.with_alert_type(EventAlertType::Info)
.with_priority(EventPriority::Low)
.with_aggregation_key("aggregation_key")
.with_source_type_name("source_type_name")
.with_hostname("hostname")
.with_timestamp(12341234),
);

let mock = mocks::event_with_options_mock("test1_event", "test_value", &["added:tag", "env:test"], options);
Datadog::new(mock, TagTrackerConfiguration::new()).do_event_with_options(
TestEvent::Test1,
"test_value",
vec!["added:tag".to_string()],
options,
);
}

#[test]
pub fn test_macro() {
let tag = String::from("tag");
// no tags
event_with_options!("test", "test_value");
// no tags with options
event_with_options!("test", "test_value", EventOptions::new());
// just literal tags
event_with_options!("test", "test_value"; "literal" => 1);
// just literal tags with options
event_with_options!("test", "test_value", EventOptions::new(); "literal" => 1);
// just expression tags
event_with_options!("test", "test_value"; "expression" => tag);
// just expression tags with options
event_with_options!("test", "test_value", EventOptions::new(); "expression" => tag);
// mixed tags
event_with_options!("test", "test_value"; "literal" => 1, "expression" => tag);
// mixed tags with options
event_with_options!("test", "test_value", EventOptions::new(); "literal" => 1, "expression" => tag);
}
14 changes: 14 additions & 0 deletions src/tests/mocks/dogstatsd_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub(super) trait MockDogstatsdClient {
fn set(&self, metric: &str, val: &str, tags: Vec<String>);
fn service_check(&self, metric: &str, val: ServiceStatus, tags: Vec<String>, options: Option<ServiceCheckOptions>);
fn event(&self, title: &str, text: &str, tags: Vec<String>);
fn event_with_options(&self, title: &str, text: &str, tags: Vec<String>, options: Option<EventOptions>);
}

#[async_trait]
Expand Down Expand Up @@ -143,4 +144,17 @@ impl<C: MockDogstatsdClient + Sync> DogstatsdClient for C {
tags.as_ref().iter().map(|s| s.as_ref().to_string()).collect(),
)
}

fn event_with_options<S>(&self, title: &str, text: &str, tags: impl TagsProvider<S>, options: Option<EventOptions>)
where
S: AsRef<str>,
{
MockDogstatsdClient::event_with_options(
self,
title,
text,
tags.as_ref().iter().map(|s| s.as_ref().to_string()).collect(),
options,
)
}
}
27 changes: 27 additions & 0 deletions src/tests/mocks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ mock! {

/// Send a custom event as a title and a body
fn event(&self, title: &str, text: &str, tags: Vec<String>);

/// Send a custom event as a title, a body and some options
fn event_with_options<'a>(&self, title: &str, text: &str, tags: Vec<String>, options: Option<EventOptions<'a>>);
}
}

Expand Down Expand Up @@ -236,6 +239,30 @@ pub fn event_mock(metric: &'static str, text: &'static str, tags: &'static [&str
client_mock
}

#[allow(dead_code)]
pub fn event_with_options_mock(
metric: &'static str,
text: &'static str,
tags: &'static [&str],
options: Option<EventOptions<'static>>,
) -> MockClient {
let mut client_mock = MockClient::new();
client_mock
.expect_event_with_options()
.once()
.with(
eq(metric),
eq(text),
function(move |called_tags: &Vec<String>| called_tags.iter().all(|tag| tags.contains(&tag.as_str()))),
function(move |called_options: &Option<EventOptions<'static>>| {
matches!((called_options, options), (Some(_), Some(_)) | (None, None))
}),
)
.return_const(());

client_mock
}

pub fn expect_incr(mut mock: MockClient, metric: &'static str, tags: impl IntoIterator<Item = String>) -> MockClient {
let tags = tags.into_iter().collect::<Vec<_>>();
mock.expect_incr()
Expand Down
1 change: 1 addition & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod count;
mod decr;
mod distribution;
mod event;
mod event_with_options;
mod gauge;
mod histogram;
mod incr;
Expand Down
Loading