Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adriangb committed Jan 9, 2025
1 parent 0ef2c4f commit 58acac6
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
"value": {
"stringValue": "logs-integration-test"
}
},
{
"key": "data",
"value": {
"bytesValue": "gICA"
}
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@
"value": {
"stringValue": "yes"
}
},
{
"key": "data",
"value": {
"bytesValue": "gICA"
}
}
],
"droppedAttributesCount": 0,
Expand Down
7 changes: 7 additions & 0 deletions opentelemetry-otlp/tests/integration_test/tests/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use anyhow::Result;
use ctor::dtor;
use integration_test_runner::logs_asserter::{read_logs_from_json, LogsAsserter};
use integration_test_runner::test_utils;
use opentelemetry::{KeyValue, Value};
use opentelemetry_otlp::LogExporter;
use opentelemetry_sdk::logs::LoggerProvider;
use opentelemetry_sdk::{logs as sdklogs, Resource};
Expand All @@ -29,6 +30,12 @@ fn init_logs() -> Result<sdklogs::LoggerProvider> {
.with_resource(
Resource::builder_empty()
.with_service_name("logs-integration-test")
.with_attribute(
KeyValue::new(
"data",
Value::Bytes(b"\x80\x80\x80".to_vec().into()),
)
)
.build(),
)
.build())
Expand Down
1 change: 1 addition & 0 deletions opentelemetry-otlp/tests/integration_test/tests/traces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub async fn traces() -> Result<()> {
vec![KeyValue::new("bogons", 100)],
);
span.set_attribute(KeyValue::new(ANOTHER_KEY, "yes"));
span.set_attribute(KeyValue::new("data", b"\x80\x80\x80".to_vec()));

tracer.in_span("Sub operation...", |cx| {
let span = cx.span();
Expand Down
2 changes: 2 additions & 0 deletions opentelemetry-proto/src/transform/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,13 @@ pub mod tonic {
Value::I64(val) => Some(any_value::Value::IntValue(val)),
Value::F64(val) => Some(any_value::Value::DoubleValue(val)),
Value::String(val) => Some(any_value::Value::StringValue(val.to_string())),
Value::Bytes(val) => Some(any_value::Value::BytesValue(val.into_vec())),
Value::Array(array) => Some(any_value::Value::ArrayValue(match array {
Array::Bool(vals) => array_into_proto(vals),
Array::I64(vals) => array_into_proto(vals),
Array::F64(vals) => array_into_proto(vals),
Array::String(vals) => array_into_proto(vals),
Array::Bytes(vals) => array_into_proto(vals),
_ => unreachable!("Nonexistent array type"), // Needs to be updated when new array types are added
})),
_ => unreachable!("Nonexistent value type"), // Needs to be updated when new value types are added
Expand Down
146 changes: 139 additions & 7 deletions opentelemetry/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,34 @@ impl hash::Hash for OtelString {
self.as_str().hash(state)
}
}
#[derive(Clone, Debug, Eq)]
enum OtelBytes {
Owned(Vec<u8>),
Static(&'static [u8]),
RefCounted(Arc<[u8]>),
}

impl OtelBytes {
fn as_slice(&self) -> &[u8] {
match self {
OtelBytes::Owned(b) => b.as_ref(),
OtelBytes::Static(b) => b,
OtelBytes::RefCounted(b) => b.as_ref(),
}
}
}

impl PartialEq for OtelBytes {
fn eq(&self, other: &Self) -> bool {
self.as_slice().eq(other.as_slice())
}
}

impl hash::Hash for OtelBytes {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
self.as_slice().hash(state)
}
}

/// A [Value::Array] containing homogeneous values.
#[non_exhaustive]
Expand All @@ -161,6 +189,8 @@ pub enum Array {
F64(Vec<f64>),
/// Array of strings
String(Vec<StringValue>),
/// Array of bytes
Bytes(Vec<BytesValue>),
}

impl fmt::Display for Array {
Expand All @@ -179,6 +209,16 @@ impl fmt::Display for Array {
}
write!(fmt, "]")
}
Array::Bytes(values) => {
write!(fmt, "[")?;
for (i, b) in values.iter().enumerate() {
if i > 0 {
write!(fmt, ",")?;
}
write!(fmt, "{}", b)?;
}
write!(fmt, "]")
}
}
}
}
Expand Down Expand Up @@ -211,8 +251,8 @@ into_array!(
(Vec<i64>, Array::I64),
(Vec<f64>, Array::F64),
(Vec<StringValue>, Array::String),
(Vec<BytesValue>, Array::Bytes),
);

/// The value part of attribute [KeyValue] pairs.
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq)]
Expand All @@ -227,6 +267,8 @@ pub enum Value {
String(StringValue),
/// Array of homogeneous values
Array(Array),
/// Bytes values
Bytes(BytesValue),
}

/// Wrapper for string-like values
Expand Down Expand Up @@ -300,6 +342,80 @@ impl From<Cow<'static, str>> for StringValue {
}
}

/// Wrapper for byte array values
#[non_exhaustive]
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct BytesValue(OtelBytes);

impl fmt::Debug for BytesValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}

impl fmt::Display for BytesValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[")?;
for (i, b) in self.as_slice().iter().enumerate() {
if i > 0 {
write!(f, ",")?;
}
write!(f, "{:#04x}", b)?;
}
write!(f, "]")
}
}

impl AsRef<[u8]> for BytesValue {
fn as_ref(&self) -> &[u8] {
self.0.as_slice()
}
}

impl BytesValue {
/// Returns a slice to the underlying bytes
pub fn as_slice(&self) -> &[u8] {
self.0.as_slice()
}

/// Retruns a Vec<u8> from the BytesValue
pub fn into_vec(self) -> Vec<u8> {
match self.0 {
OtelBytes::Owned(b) => b.to_vec(),
OtelBytes::Static(b) => b.to_vec(),
OtelBytes::RefCounted(b) => b.to_vec(),
}
}
}

impl From<BytesValue> for Vec<u8> {
fn from(b: BytesValue) -> Self {
match b.0 {
OtelBytes::Owned(b) => b.to_vec(),
OtelBytes::Static(b) => b.to_vec(),
OtelBytes::RefCounted(b) => b.to_vec(),
}
}
}

impl From<&'static [u8]> for BytesValue {
fn from(b: &'static [u8]) -> Self {
BytesValue(OtelBytes::Static(b))
}
}

impl From<Vec<u8>> for BytesValue {
fn from(b: Vec<u8>) -> Self {
BytesValue(OtelBytes::Owned(b))
}
}

impl From<Arc<[u8]>> for BytesValue {
fn from(b: Arc<[u8]>) -> Self {
BytesValue(OtelBytes::RefCounted(b))
}
}

impl Value {
/// String representation of the `Value`
///
Expand All @@ -311,16 +427,13 @@ impl Value {
Value::F64(v) => format!("{}", v).into(),
Value::String(v) => Cow::Borrowed(v.as_str()),
Value::Array(v) => format!("{}", v).into(),
Value::Bytes(v) => format!("{}", v).into(),
}
}
}

macro_rules! from_values {
(
$(
($t:ty, $val:expr);
)+
) => {
($(($t:ty, $val:expr);)+) => {
$(
impl From<$t> for Value {
fn from(t: $t) -> Self {
Expand All @@ -336,6 +449,7 @@ from_values!(
(i64, Value::I64);
(f64, Value::F64);
(StringValue, Value::String);
(BytesValue, Value::Bytes);
);

impl From<&'static str> for Value {
Expand All @@ -362,6 +476,24 @@ impl From<Cow<'static, str>> for Value {
}
}

impl From<&'static [u8]> for Value {
fn from(b: &'static [u8]) -> Self {
Value::Bytes(b.into())
}
}

impl From<Vec<u8>> for Value {
fn from(b: Vec<u8>) -> Self {
Value::Bytes(b.into())
}
}

impl From<Arc<[u8]>> for Value {
fn from(b: Arc<[u8]>) -> Self {
Value::Bytes(b.into())
}
}

impl fmt::Display for Value {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Expand All @@ -370,6 +502,7 @@ impl fmt::Display for Value {
Value::F64(v) => v.fmt(fmt),
Value::String(v) => fmt.write_str(v.as_str()),
Value::Array(v) => v.fmt(fmt),
Value::Bytes(v) => v.fmt(fmt),
}
}
}
Expand Down Expand Up @@ -483,7 +616,6 @@ impl InstrumentationScope {
self.attributes.iter()
}
}

/// Configuration options for [InstrumentationScope].
///
/// An instrumentation scope is a library or crate providing instrumentation.
Expand Down
2 changes: 2 additions & 0 deletions opentelemetry/src/metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ impl Hash for KeyValue {
Array::I64(i) => i.hash(state),
Array::F64(f) => f.iter().for_each(|f| F64Hashable(*f).hash(state)),
Array::String(s) => s.hash(state),
Array::Bytes(b) => b.hash(state),
},
Value::Bool(b) => b.hash(state),
Value::I64(i) => i.hash(state),
Value::String(s) => s.hash(state),
Value::Bytes(b) => b.hash(state),
};
}
}
Expand Down

0 comments on commit 58acac6

Please sign in to comment.