Skip to content

Commit

Permalink
fix otlp/json exporter (open-telemetry#1882)
Browse files Browse the repository at this point in the history
  • Loading branch information
lalitb authored and ramgdev committed Jul 2, 2024
1 parent fb950df commit a8b8cb0
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 63 deletions.
2 changes: 2 additions & 0 deletions opentelemetry-otlp/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ now use `.with_resource(RESOURCE::default())` to configure Resource when using
- **Breaking** [1869](https://github.com/open-telemetry/opentelemetry-rust/pull/1869) The OTLP logs exporter now overrides the [InstrumentationScope::name](https://github.com/open-telemetry/opentelemetry-proto/blob/b3060d2104df364136d75a35779e6bd48bac449a/opentelemetry/proto/common/v1/common.proto#L73) field with the `target` from `LogRecord`, if target is populated.
- Groups batch of `LogRecord` and `Span` by their resource and instrumentation scope before exporting, for better efficiency [#1873](https://github.com/open-telemetry/opentelemetry-rust/pull/1873).

- Fixing the OTLP HTTP/JSON exporter. [#1882](https://github.com/open-telemetry/opentelemetry-rust/pull/1882) - The exporter was broken in the
previous release.

## v0.16.0

Expand Down
127 changes: 64 additions & 63 deletions opentelemetry-proto/src/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ pub(crate) mod serializers {
Ok(s) => s,
Err(e) => return Err(e), // Handle the error or return it
};

// Attempt to serialize the intValue field
if let Err(e) = state.serialize_field("intValue", &i.to_string()) {
return Err(e); // Handle the error or return it
}

// Finalize the struct serialization
state.end()
},
}
Some(value) => value.serialize(serializer),
None => serializer.serialize_none(),
},
Expand All @@ -74,76 +74,77 @@ pub(crate) mod serializers {
}

pub fn deserialize_from_value<'de, D>(deserializer: D) -> Result<Option<AnyValue>, D::Error>
where
D: Deserializer<'de>,
{
struct ValueVisitor;
where
D: Deserializer<'de>,
{
struct ValueVisitor;

impl<'de> de::Visitor<'de> for ValueVisitor {
type Value = AnyValue;
impl<'de> de::Visitor<'de> for ValueVisitor {
type Value = AnyValue;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a JSON object for AnyValue")
}
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a JSON object for AnyValue")
}

fn visit_map<V>(self, mut map: V) -> Result<AnyValue, V::Error>
where
V: de::MapAccess<'de>,
{
let mut value: Option<any_value::Value> = None;
fn visit_map<V>(self, mut map: V) -> Result<AnyValue, V::Error>
where
V: de::MapAccess<'de>,
{
let mut value: Option<any_value::Value> = None;

while let Some(key) = map.next_key::<String>()? {
let key_str = key.as_str();
match key_str {
"stringValue" => {
let s = map.next_value()?;
value = Some(any_value::Value::StringValue(s));
},
"boolValue" => {
let b = map.next_value()?;
value = Some(any_value::Value::BoolValue(b));
},
"intValue" => {
let value_str = map.next_value::<String>()?;
let int_value = value_str.parse::<i64>()
.map_err(de::Error::custom)?;
value = Some(any_value::Value::IntValue(int_value));
},
"doubleValue" => {
let d = map.next_value()?;
value = Some(any_value::Value::DoubleValue(d));
},
"arrayValue" => {
let a = map.next_value()?;
value = Some(any_value::Value::ArrayValue(a));
},
"kvlistValue" => {
let kv = map.next_value()?;
value = Some(any_value::Value::KvlistValue(kv));
},
"bytesValue" => {
let bytes = map.next_value()?;
value = Some(any_value::Value::BytesValue(bytes));
},
_ => {
//skip unknown keys, and handle error later.
continue
while let Some(key) = map.next_key::<String>()? {
let key_str = key.as_str();
match key_str {
"stringValue" => {
let s = map.next_value()?;
value = Some(any_value::Value::StringValue(s));
}
"boolValue" => {
let b = map.next_value()?;
value = Some(any_value::Value::BoolValue(b));
}
"intValue" => {
let value_str = map.next_value::<String>()?;
let int_value = value_str.parse::<i64>().map_err(de::Error::custom)?;
value = Some(any_value::Value::IntValue(int_value));
}
"doubleValue" => {
let d = map.next_value()?;
value = Some(any_value::Value::DoubleValue(d));
}
"arrayValue" => {
let a = map.next_value()?;
value = Some(any_value::Value::ArrayValue(a));
}
"kvlistValue" => {
let kv = map.next_value()?;
value = Some(any_value::Value::KvlistValue(kv));
}
"bytesValue" => {
let bytes = map.next_value()?;
value = Some(any_value::Value::BytesValue(bytes));
}
_ => {
//skip unknown keys, and handle error later.
continue;
}
}
}
}

if let Some(v) = value {
Ok(AnyValue { value: Some(v) })
} else {
Err(de::Error::custom("Invalid data for AnyValue, no known keys found"))
if let Some(v) = value {
Ok(AnyValue { value: Some(v) })
} else {
Err(de::Error::custom(
"Invalid data for AnyValue, no known keys found",
))
}
}
}

let value = deserializer.deserialize_map(ValueVisitor)?;
Ok(Some(value))
}

let value = deserializer.deserialize_map(ValueVisitor)?;
Ok(Some(value))
}

pub fn serialize_u64_to_string<S>(value: &u64, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
Expand All @@ -167,7 +168,7 @@ where
let s = value.to_string();
serializer.serialize_str(&s)
}

pub fn deserialize_string_to_i64<'de, D>(deserializer: D) -> Result<i64, D::Error>
where
D: Deserializer<'de>,
Expand Down

0 comments on commit a8b8cb0

Please sign in to comment.