Skip to content

Commit 9dc8a45

Browse files
committed
changes..
1 parent a95e681 commit 9dc8a45

File tree

4 files changed

+83
-84
lines changed

4 files changed

+83
-84
lines changed

opentelemetry-appender-log/src/lib.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -938,22 +938,17 @@ mod tests {
938938
);
939939

940940
let logs = exporter.get_emitted_logs().unwrap();
941-
let attributes = &logs[0].record.attributes;
942-
943-
let get = |needle: &str| {
944-
attributes.iter().find_map(|attr| {
945-
if let Some((k, v)) = attr {
946-
if k.as_str() == needle {
947-
Some(v.clone())
948-
} else {
949-
None
950-
}
941+
//let attributes = &logs[0].record.attributes;
942+
943+
let get = |needle: &str| -> Option<AnyValue> {
944+
logs[0].record.attributes_iter().find_map(|(k, v)| {
945+
if k.as_str() == needle {
946+
Some(v.clone())
951947
} else {
952948
None
953949
}
954950
})
955951
};
956-
957952
assert_eq!(
958953
AnyValue::String(StringValue::from("a string")),
959954
get("str_value").unwrap()

opentelemetry-appender-tracing/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pprof = { version = "0.13", features = ["flamegraph", "criterion"] }
3232
[features]
3333
experimental_metadata_attributes = ["dep:tracing-log"]
3434
logs_level_enabled = ["opentelemetry/logs_level_enabled"]
35+
default = ["experimental_metadata_attributes"]
3536

3637
[[bench]]
3738
name = "logs"

opentelemetry-appender-tracing/src/layer.rs

+75-72
Original file line numberDiff line numberDiff line change
@@ -252,33 +252,38 @@ mod tests {
252252
assert!(log.record.trace_context.is_none());
253253

254254
// Validate attributes
255-
let attributes = log.record.attributes.clone();
256255
#[cfg(not(feature = "experimental_metadata_attributes"))]
257-
assert_eq!(attributes.len(), 3);
256+
assert_eq!(log.record.attributes_len(), 3);
258257
#[cfg(feature = "experimental_metadata_attributes")]
259-
assert_eq!(attributes.len(), 8);
260-
assert!(attributes.contains(&Some((Key::new("event_id"), 20.into()))));
261-
assert!(attributes.contains(&Some((Key::new("user_name"), "otel".into()))));
262-
assert!(attributes.contains(&Some((
263-
Key::new("user_email"),
264-
"otel@opentelemetry.io".into()
265-
))));
258+
assert_eq!(log.record.attributes_len(), 8);
259+
assert!(log
260+
.record
261+
.attributes_contains(&Key::new("event_id"), &AnyValue::Int(20)));
262+
assert!(log
263+
.record
264+
.attributes_contains(&Key::new("user_name"), &AnyValue::String("otel".into())));
265+
assert!(log.record.attributes_contains(
266+
&Key::new("user_email"),
267+
&AnyValue::String("otel@opentelemetry.io".into())
268+
));
269+
266270
#[cfg(feature = "experimental_metadata_attributes")]
267271
{
268-
assert!(attributes.contains(&Some((Key::new("code.filename"), "layer.rs".into()))));
269-
assert!(attributes.contains(&Some((
270-
Key::new("code.namespace"),
271-
"opentelemetry_appender_tracing::layer::tests".into()
272-
))));
272+
assert!(log.record.attributes_contains(
273+
&Key::new("code.filename"),
274+
&AnyValue::String("layer.rs".into())
275+
));
276+
assert!(log.record.attributes_contains(
277+
&Key::new("code.namespace"),
278+
&AnyValue::String("opentelemetry_appender_tracing::layer::tests".into())
279+
));
273280
// The other 3 experimental_metadata_attributes are too unstable to check their value.
274281
// Ex.: The path will be different on a Windows and Linux machine.
275282
// Ex.: The line can change easily if someone makes changes in this source file.
276-
let attributes_key: Vec<Key> = attributes
277-
.iter()
278-
.filter_map(|attr| match attr {
279-
Some((key, _)) => Some(key.clone()),
280-
None => None,
281-
})
283+
let attributes_key: Vec<Key> = log
284+
.record
285+
.attributes_iter()
286+
.map(|(key, _)| key.clone())
282287
.collect();
283288

284289
assert!(attributes_key.contains(&Key::new("code.filepath")));
@@ -354,35 +359,38 @@ mod tests {
354359
);
355360

356361
// validate attributes.
357-
let attributes = log.record.attributes.clone();
358362
#[cfg(not(feature = "experimental_metadata_attributes"))]
359-
assert_eq!(attributes.len(), 3);
363+
assert_eq!(log.record.attributes_len(), 3);
360364
#[cfg(feature = "experimental_metadata_attributes")]
361-
assert_eq!(attributes.len(), 8);
362-
assert!(attributes.contains(&Some((Key::new("event_id"), 20.into()))));
363-
assert!(attributes.contains(&Some((Key::new("user_name"), "otel".into()))));
364-
assert!(attributes.contains(&Some((
365-
Key::new("user_email"),
366-
"otel@opentelemetry.io".into()
367-
))));
365+
assert_eq!(log.record.attributes_len(), 8);
366+
assert!(log
367+
.record
368+
.attributes_contains(&Key::new("event_id"), &AnyValue::Int(20.into())));
369+
assert!(log
370+
.record
371+
.attributes_contains(&Key::new("user_name"), &AnyValue::String("otel".into())));
372+
assert!(log.record.attributes_contains(
373+
&Key::new("user_email"),
374+
&AnyValue::String("otel@opentelemetry.io".into())
375+
));
368376
#[cfg(feature = "experimental_metadata_attributes")]
369377
{
370-
assert!(attributes.contains(&Some((Key::new("code.filename"), "layer.rs".into()))));
371-
assert!(attributes.contains(&Some((
372-
Key::new("code.namespace"),
373-
"opentelemetry_appender_tracing::layer::tests".into()
374-
))));
378+
assert!(log.record.attributes_contains(
379+
&Key::new("code.filename"),
380+
&AnyValue::String("layer.rs".into())
381+
));
382+
assert!(log.record.attributes_contains(
383+
&Key::new("code.namespace"),
384+
&AnyValue::String("opentelemetry_appender_tracing::layer::tests".into())
385+
));
375386
// The other 3 experimental_metadata_attributes are too unstable to check their value.
376387
// Ex.: The path will be different on a Windows and Linux machine.
377388
// Ex.: The line can change easily if someone makes changes in this source file.
378389

379-
//let attributes_key: Vec<Key> = attributes.iter().map(|(key, _)| key.clone()).collect();
380-
let attributes_key: Vec<Key> = attributes
381-
.iter()
382-
.filter_map(|attr| match attr {
383-
Some((key, _)) => Some(key.clone()),
384-
None => None,
385-
})
390+
let attributes_key: Vec<Key> = log
391+
.record
392+
.attributes_iter()
393+
.map(|(key, _)| key.clone())
386394
.collect();
387395
assert!(attributes_key.contains(&Key::new("code.filepath")));
388396
assert!(attributes_key.contains(&Key::new("code.lineno")));
@@ -426,30 +434,27 @@ mod tests {
426434
// Validate trace context is none.
427435
assert!(log.record.trace_context.is_none());
428436

429-
// Validate attributes
430-
#[cfg(feature = "experimental_metadata_attributes")]
431-
let attributes = log.record.attributes.clone();
432-
433437
// Attributes can be polluted when we don't use this feature.
434438
#[cfg(feature = "experimental_metadata_attributes")]
435-
assert_eq!(attributes.len(), 5);
439+
assert_eq!(log.record.attributes_len(), 5);
436440

437441
#[cfg(feature = "experimental_metadata_attributes")]
438442
{
439-
assert!(attributes.contains(&(Some((Key::new("code.filename"), "layer.rs".into())))));
440-
assert!(attributes.contains(&Some((
441-
Key::new("code.namespace"),
442-
"opentelemetry_appender_tracing::layer::tests".into()
443-
))));
443+
assert!(log.record.attributes_contains(
444+
&Key::new("code.filename"),
445+
&AnyValue::String("layer.rs".into())
446+
));
447+
assert!(log.record.attributes_contains(
448+
&Key::new("code.namespace"),
449+
&AnyValue::String("opentelemetry_appender_tracing::layer::tests".into())
450+
));
444451
// The other 3 experimental_metadata_attributes are too unstable to check their value.
445452
// Ex.: The path will be different on a Windows and Linux machine.
446453
// Ex.: The line can change easily if someone makes changes in this source file.
447-
let attributes_key: Vec<Key> = attributes
448-
.iter()
449-
.filter_map(|attr| match attr {
450-
Some((key, _)) => Some(key.clone()),
451-
None => None,
452-
})
454+
let attributes_key: Vec<Key> = log
455+
.record
456+
.attributes_iter()
457+
.map(|(key, _)| key.clone())
453458
.collect();
454459
assert!(attributes_key.contains(&Key::new("code.filepath")));
455460
assert!(attributes_key.contains(&Key::new("code.lineno")));
@@ -525,29 +530,27 @@ mod tests {
525530
);
526531

527532
// validate attributes.
528-
#[cfg(feature = "experimental_metadata_attributes")]
529-
let attributes = log.record.attributes.clone();
530-
531533
// Attributes can be polluted when we don't use this feature.
532534
#[cfg(feature = "experimental_metadata_attributes")]
533-
assert_eq!(attributes.len(), 5);
535+
assert_eq!(log.record.attributes_len(), 5);
534536

535537
#[cfg(feature = "experimental_metadata_attributes")]
536538
{
537-
assert!(attributes.contains(&Some((Key::new("code.filename"), "layer.rs".into()))));
538-
assert!(attributes.contains(&Some((
539-
Key::new("code.namespace"),
540-
"opentelemetry_appender_tracing::layer::tests".into()
541-
))));
539+
assert!(log.record.attributes_contains(
540+
&Key::new("code.filename"),
541+
&AnyValue::String("layer.rs".into())
542+
));
543+
assert!(log.record.attributes_contains(
544+
&Key::new("code.namespace"),
545+
&AnyValue::String("opentelemetry_appender_tracing::layer::tests".into())
546+
));
542547
// The other 3 experimental_metadata_attributes are too unstable to check their value.
543548
// Ex.: The path will be different on a Windows and Linux machine.
544549
// Ex.: The line can change easily if someone makes changes in this source file.
545-
let attributes_key: Vec<Key> = attributes
546-
.iter()
547-
.filter_map(|attr| match attr {
548-
Some((key, _)) => Some(key.clone()),
549-
None => None,
550-
})
550+
let attributes_key: Vec<Key> = log
551+
.record
552+
.attributes_iter()
553+
.map(|(key, _)| key.clone())
551554
.collect();
552555
assert!(attributes_key.contains(&Key::new("code.filepath")));
553556
assert!(attributes_key.contains(&Key::new("code.lineno")));

opentelemetry-sdk/src/logs/record.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl LogRecord {
115115
}
116116

117117
/// Checks if the `LogRecord` contains the specified attribute.
118-
pub fn contains_attribute(&self, key: &Key, value: &AnyValue) -> bool {
118+
pub fn attributes_contains(&self, key: &Key, value: &AnyValue) -> bool {
119119
self.attributes.iter().any(|opt| {
120120
if let Some((k, v)) = opt {
121121
k == key && v == value

0 commit comments

Comments
 (0)