Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
zohnannor committed Nov 7, 2023
1 parent 449b21e commit ba53763
Showing 1 changed file with 20 additions and 70 deletions.
90 changes: 20 additions & 70 deletions metrics-tracing-context/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ static USER_EMAIL: &[Label] = &[
Label::from_static_parts("user.email", "[email protected]"),
];
static USER_ID: &[Label] = &[Label::from_static_parts("user.id", "42")];
static USER_EMAIL_ID: &[Label] = &[
Label::from_static_parts("user", "ferris"),
Label::from_static_parts("user.email", "[email protected]"),
Label::from_static_parts("user.id", "42"),
];
static EMAIL_USER: &[Label] = &[
Label::from_static_parts("user.email", "[email protected]"),
Label::from_static_parts("user", "ferris"),
Expand All @@ -48,25 +43,12 @@ static NODE_USER_EMAIL: &[Label] = &[
Label::from_static_parts("user", "ferris"),
Label::from_static_parts("user.email", "[email protected]"),
];
static NODE_USER_EMAIL_ID: &[Label] = &[
Label::from_static_parts("node_name", "localhost"),
Label::from_static_parts("user", "ferris"),
Label::from_static_parts("user.email", "[email protected]"),
Label::from_static_parts("user.id", "42"),
];
static SVC_NODE_USER_EMAIL: &[Label] = &[
Label::from_static_parts("service", "login_service"),
Label::from_static_parts("node_name", "localhost"),
Label::from_static_parts("user", "ferris"),
Label::from_static_parts("user.email", "[email protected]"),
];
static SVC_NODE_USER_EMAIL_ID: &[Label] = &[
Label::from_static_parts("service", "login_service"),
Label::from_static_parts("node_name", "localhost"),
Label::from_static_parts("user", "ferris"),
Label::from_static_parts("user.email", "[email protected]"),
Label::from_static_parts("user.id", "42"),
];
static COMBINED_LABELS: &[Label] = &[
Label::from_static_parts("shared_field", "inner"),
Label::from_static_parts("inner_specific", "foo"),
Expand Down Expand Up @@ -225,28 +207,21 @@ fn test_basic_functionality_then_record() {
}

#[test]
fn test_macro_forms() {
fn test_rerecord() {
static USER_ID_42: &[Label] = &[Label::from_static_parts("user.id", "42")];
static USER_ID_123: &[Label] =
&[Label::from_static_parts("user.id", "42"), Label::from_static_parts("user.id", "123")];

let (_guard, snapshotter) = setup(TracingContextLayer::all());

let user = "ferris";
let email = "[email protected]";
let span = span!(Level::TRACE, "login", user, user.email = email);
let span = span!(Level::TRACE, "login", user.id = tracing_core::field::Empty);
let _guard = span.enter();

// No labels.
counter!("login_attempts_no_labels").increment(1);
// Static labels only.
counter!("login_attempts_static_labels", "service" => "login_service").increment(1);
// Dynamic labels only.
let node_name = "localhost".to_string();
counter!("login_attempts_dynamic_labels", "node_name" => node_name.clone()).increment(1);
// Static and dynamic.
counter!(
"login_attempts_static_and_dynamic_labels",
"service" => "login_service",
"node_name" => node_name,
)
.increment(1);
span.record("user.id", 42);
counter!("login_attempts").increment(1);

span.record("user.id", 123);
counter!("login_attempts").increment(1);

let snapshot = snapshotter.snapshot().into_vec();

Expand All @@ -256,7 +231,7 @@ fn test_macro_forms() {
(
CompositeKey::new(
MetricKind::Counter,
Key::from_static_parts(LOGIN_ATTEMPTS_NONE, USER_EMAIL)
Key::from_static_parts(LOGIN_ATTEMPTS, USER_ID_42)
),
None,
None,
Expand All @@ -265,50 +240,25 @@ fn test_macro_forms() {
(
CompositeKey::new(
MetricKind::Counter,
Key::from_static_parts(LOGIN_ATTEMPTS_STATIC, SVC_USER_EMAIL),
Key::from_static_parts(LOGIN_ATTEMPTS, USER_ID_123)
),
None,
None,
DebugValue::Counter(1),
),
(
CompositeKey::new(
MetricKind::Counter,
Key::from_static_parts(LOGIN_ATTEMPTS_DYNAMIC, NODE_USER_EMAIL),
),
None,
None,
DebugValue::Counter(1),
),
(
CompositeKey::new(
MetricKind::Counter,
Key::from_static_parts(LOGIN_ATTEMPTS_BOTH, SVC_NODE_USER_EMAIL),
),
None,
None,
DebugValue::Counter(1),
),
)
]
);
}

#[test]
fn test_macro_forms_record() {
fn test_macro_forms() {
let (_guard, snapshotter) = setup(TracingContextLayer::all());

let user = "ferris";
let email = "[email protected]";
let span = span!(
Level::TRACE,
"login",
user,
user.email = email,
user.id = tracing_core::field::Empty,
);
let span = span!(Level::TRACE, "login", user, user.email = email);
let _guard = span.enter();

span.record("user.id", 42);
// No labels.
counter!("login_attempts_no_labels").increment(1);
// Static labels only.
Expand All @@ -332,7 +282,7 @@ fn test_macro_forms_record() {
(
CompositeKey::new(
MetricKind::Counter,
Key::from_static_parts(LOGIN_ATTEMPTS_NONE, USER_EMAIL_ID)
Key::from_static_parts(LOGIN_ATTEMPTS_NONE, USER_EMAIL)
),
None,
None,
Expand All @@ -341,7 +291,7 @@ fn test_macro_forms_record() {
(
CompositeKey::new(
MetricKind::Counter,
Key::from_static_parts(LOGIN_ATTEMPTS_STATIC, SVC_USER_EMAIL_ID),
Key::from_static_parts(LOGIN_ATTEMPTS_STATIC, SVC_USER_EMAIL),
),
None,
None,
Expand All @@ -350,7 +300,7 @@ fn test_macro_forms_record() {
(
CompositeKey::new(
MetricKind::Counter,
Key::from_static_parts(LOGIN_ATTEMPTS_DYNAMIC, NODE_USER_EMAIL_ID),
Key::from_static_parts(LOGIN_ATTEMPTS_DYNAMIC, NODE_USER_EMAIL),
),
None,
None,
Expand All @@ -359,7 +309,7 @@ fn test_macro_forms_record() {
(
CompositeKey::new(
MetricKind::Counter,
Key::from_static_parts(LOGIN_ATTEMPTS_BOTH, SVC_NODE_USER_EMAIL_ID),
Key::from_static_parts(LOGIN_ATTEMPTS_BOTH, SVC_NODE_USER_EMAIL),
),
None,
None,
Expand Down

0 comments on commit ba53763

Please sign in to comment.