Skip to content

Commit a0fc1f3

Browse files
authored
Merge branch 'main' into preallocate-attribute-mem
2 parents 0a876bf + 1719244 commit a0fc1f3

File tree

6 files changed

+83
-37
lines changed

6 files changed

+83
-37
lines changed

opentelemetry-appender-tracing/src/layer.rs

+53-33
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,19 @@ mod tests {
208208
use opentelemetry::trace::TracerProvider as _;
209209
use opentelemetry::trace::{TraceContextExt, TraceFlags, Tracer};
210210
use opentelemetry::{logs::AnyValue, Key};
211-
use opentelemetry_sdk::logs::LoggerProvider;
211+
use opentelemetry_sdk::logs::{LogRecord, LoggerProvider};
212212
use opentelemetry_sdk::testing::logs::InMemoryLogsExporter;
213213
use opentelemetry_sdk::trace;
214214
use opentelemetry_sdk::trace::{Sampler, TracerProvider};
215215
use tracing::error;
216216
use tracing_subscriber::layer::SubscriberExt;
217217

218+
pub fn attributes_contains(log_record: &LogRecord, key: &Key, value: &AnyValue) -> bool {
219+
log_record
220+
.attributes_iter()
221+
.any(|(k, v)| k == key && v == value)
222+
}
223+
218224
// cargo test --features=testing
219225
#[test]
220226
fn tracing_appender_standalone() {
@@ -253,27 +259,33 @@ mod tests {
253259

254260
// Validate attributes
255261
#[cfg(not(feature = "experimental_metadata_attributes"))]
256-
assert_eq!(log.record.attributes_len(), 3);
262+
assert_eq!(log.record.attributes_iter().count(), 3);
257263
#[cfg(feature = "experimental_metadata_attributes")]
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(
264+
assert_eq!(log.record.attributes_iter().count(), 8);
265+
assert!(attributes_contains(
266+
&log.record,
267+
&Key::new("event_id"),
268+
&AnyValue::Int(20)
269+
));
270+
assert!(attributes_contains(
271+
&log.record,
272+
&Key::new("user_name"),
273+
&AnyValue::String("otel".into())
274+
));
275+
assert!(attributes_contains(
276+
&log.record,
266277
&Key::new("user_email"),
267278
&AnyValue::String("otel@opentelemetry.io".into())
268279
));
269-
270280
#[cfg(feature = "experimental_metadata_attributes")]
271281
{
272-
assert!(log.record.attributes_contains(
282+
assert!(attributes_contains(
283+
&log.record,
273284
&Key::new("code.filename"),
274285
&AnyValue::String("layer.rs".into())
275286
));
276-
assert!(log.record.attributes_contains(
287+
assert!(attributes_contains(
288+
&log.record,
277289
&Key::new("code.namespace"),
278290
&AnyValue::String("opentelemetry_appender_tracing::layer::tests".into())
279291
));
@@ -285,7 +297,6 @@ mod tests {
285297
.attributes_iter()
286298
.map(|(key, _)| key.clone())
287299
.collect();
288-
289300
assert!(attributes_key.contains(&Key::new("code.filepath")));
290301
assert!(attributes_key.contains(&Key::new("code.lineno")));
291302
assert!(attributes_key.contains(&Key::new("log.target")));
@@ -360,33 +371,39 @@ mod tests {
360371

361372
// validate attributes.
362373
#[cfg(not(feature = "experimental_metadata_attributes"))]
363-
assert_eq!(log.record.attributes_len(), 3);
374+
assert_eq!(log.record.attributes_iter().count(), 3);
364375
#[cfg(feature = "experimental_metadata_attributes")]
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(
376+
assert_eq!(log.record.attributes_iter().count(), 8);
377+
assert!(attributes_contains(
378+
&log.record,
379+
&Key::new("event_id"),
380+
&AnyValue::Int(20.into())
381+
));
382+
assert!(attributes_contains(
383+
&log.record,
384+
&Key::new("user_name"),
385+
&AnyValue::String("otel".into())
386+
));
387+
assert!(attributes_contains(
388+
&log.record,
373389
&Key::new("user_email"),
374390
&AnyValue::String("otel@opentelemetry.io".into())
375391
));
376392
#[cfg(feature = "experimental_metadata_attributes")]
377393
{
378-
assert!(log.record.attributes_contains(
394+
assert!(attributes_contains(
395+
&log.record,
379396
&Key::new("code.filename"),
380397
&AnyValue::String("layer.rs".into())
381398
));
382-
assert!(log.record.attributes_contains(
399+
assert!(attributes_contains(
400+
&log.record,
383401
&Key::new("code.namespace"),
384402
&AnyValue::String("opentelemetry_appender_tracing::layer::tests".into())
385403
));
386404
// The other 3 experimental_metadata_attributes are too unstable to check their value.
387405
// Ex.: The path will be different on a Windows and Linux machine.
388406
// Ex.: The line can change easily if someone makes changes in this source file.
389-
390407
let attributes_key: Vec<Key> = log
391408
.record
392409
.attributes_iter()
@@ -436,15 +453,17 @@ mod tests {
436453

437454
// Attributes can be polluted when we don't use this feature.
438455
#[cfg(feature = "experimental_metadata_attributes")]
439-
assert_eq!(log.record.attributes_len(), 5);
456+
assert_eq!(log.record.attributes_iter().count(), 5);
440457

441458
#[cfg(feature = "experimental_metadata_attributes")]
442459
{
443-
assert!(log.record.attributes_contains(
460+
assert!(attributes_contains(
461+
&log.record,
444462
&Key::new("code.filename"),
445463
&AnyValue::String("layer.rs".into())
446464
));
447-
assert!(log.record.attributes_contains(
465+
assert!(attributes_contains(
466+
&log.record,
448467
&Key::new("code.namespace"),
449468
&AnyValue::String("opentelemetry_appender_tracing::layer::tests".into())
450469
));
@@ -529,18 +548,19 @@ mod tests {
529548
TraceFlags::SAMPLED
530549
);
531550

532-
// validate attributes.
533551
// Attributes can be polluted when we don't use this feature.
534552
#[cfg(feature = "experimental_metadata_attributes")]
535-
assert_eq!(log.record.attributes_len(), 5);
553+
assert_eq!(log.record.attributes_iter().count(), 5);
536554

537555
#[cfg(feature = "experimental_metadata_attributes")]
538556
{
539-
assert!(log.record.attributes_contains(
557+
assert!(attributes_contains(
558+
&log.record,
540559
&Key::new("code.filename"),
541560
&AnyValue::String("layer.rs".into())
542561
));
543-
assert!(log.record.attributes_contains(
562+
assert!(attributes_contains(
563+
&log.record,
544564
&Key::new("code.namespace"),
545565
&AnyValue::String("opentelemetry_appender_tracing::layer::tests".into())
546566
));

opentelemetry-otlp/examples/basic-otlp-http/otel-collector-config.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ receivers:
88
otlp:
99
protocols:
1010
grpc:
11+
endpoint: 0.0.0.0:4317
1112
http:
13+
endpoint: 0.0.0.0:4318
1214

1315
exporters:
1416
debug:

opentelemetry-proto/src/transform/logs.rs

-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ pub mod tonic {
9898
}),
9999
})
100100
.collect();
101-
102101
if let Some(event_name) = log_record.event_name.as_ref() {
103102
attributes.push(KeyValue {
104103
key: "name".into(),

opentelemetry-sdk/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
first interval instead of doing it right away.
1212
[#1970](https://github.com/open-telemetry/opentelemetry-rust/pull/1970)
1313
[#1973](https://github.com/open-telemetry/opentelemetry-rust/pull/1973)
14+
- **Breaking** [#1985](https://github.com/open-telemetry/opentelemetry-rust/pull/1985)
15+
Hide LogRecord attributes Implementation Details from processors and exporters.
16+
The custom exporters and processors can't directly access the `LogData::LogRecord::attributes`, as
17+
these are private to opentelemetry-sdk. Instead, they would now use LogRecord::attributes_iter()
18+
method to access them.
1419

1520

1621
## v0.24.1

opentelemetry-sdk/src/logs/record.rs

+23-2
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,29 @@ impl LogRecord {
125125
}
126126
}
127127

128+
impl LogRecord {
129+
/// Provides an iterator over the attributes in the `LogRecord`.
130+
pub fn attributes_iter(&self) -> impl Iterator<Item = &(Key, AnyValue)> {
131+
self.attributes
132+
.as_ref()
133+
.map_or_else(|| [].iter(), |attrs| attrs.iter())
134+
}
135+
136+
#[allow(dead_code)]
137+
/// Returns the number of attributes in the `LogRecord`.
138+
pub(crate) fn attributes_len(&self) -> usize {
139+
self.attributes.as_ref().map_or(0, |attrs| attrs.len())
140+
}
141+
142+
#[allow(dead_code)]
143+
/// Returns true if the `LogRecord` contains the specified attribute.
144+
pub(crate) fn attributes_contains(&self, key: &Key, value: &AnyValue) -> bool {
145+
self.attributes.as_ref().map_or(false, |attrs| {
146+
attrs.iter().any(|(k, v)| k == key && v == value)
147+
})
148+
}
149+
}
150+
128151
/// TraceContext stores the trace context for logs that have an associated
129152
/// span.
130153
#[derive(Debug, Clone, PartialEq)]
@@ -214,7 +237,6 @@ mod tests {
214237
let mut log_record = LogRecord::default();
215238
let attributes = vec![(Key::new("key"), AnyValue::String("value".into()))];
216239
log_record.add_attributes(attributes.clone());
217-
218240
for (key, value) in attributes {
219241
assert!(log_record.attributes_contains(&key, &value));
220242
}
@@ -224,7 +246,6 @@ mod tests {
224246
fn test_set_attribute() {
225247
let mut log_record = LogRecord::default();
226248
log_record.add_attribute("key", "value");
227-
228249
let key = Key::new("key");
229250
let value = AnyValue::String("value".into());
230251
assert!(log_record.attributes_contains(&key, &value));

opentelemetry-stdout/src/logs/transform.rs

-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ impl From<opentelemetry_sdk::export::logs::LogData> for LogRecord {
113113
.attributes_iter()
114114
.map(|(k, v)| KeyValue::from((k.clone(), v.clone()))) // Map each pair to a KeyValue
115115
.collect::<Vec<KeyValue>>(); // Collect into a Vec<KeyValue>s
116-
117116
if let Some(event_name) = &value.record.event_name {
118117
attributes.push(KeyValue::from((
119118
"name".into(),

0 commit comments

Comments
 (0)