Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix http json example #1835

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
6d8f830
fix event attribute
ramgdev May 27, 2024
abaeaaf
cleanup
ramgdev May 27, 2024
640b886
fix serializing value
ramgdev May 27, 2024
4f2b65a
Merge branch 'main' into http-json-example
TommyCpp May 28, 2024
61a6e5e
Merge branch 'main' into http-json-example
TommyCpp May 29, 2024
dc85963
Merge branch 'main' into http-json-example
ramgdev May 29, 2024
227f959
Merge branch 'main' into http-json-example
cijothomas May 29, 2024
9637246
Merge branch 'main' into http-json-example
ramgdev May 30, 2024
5f33ca8
Merge branch 'main' into http-json-example
ramgdev May 30, 2024
d6ee9aa
Merge branch 'main' into http-json-example
ramgdev Jun 1, 2024
e395d01
Merge branch 'main' into http-json-example
ramgdev Jun 3, 2024
d524e7e
Merge branch 'main' into http-json-example
ramgdev Jun 4, 2024
b7d182b
remove commented code
ramgdev Jun 4, 2024
b281d76
Merge branch 'main' into http-json-example
ramgdev Jun 4, 2024
3a7a119
Merge branch 'main' into http-json-example
ramgdev Jun 4, 2024
6b0c8ae
Merge branch 'main' into http-json-example
ramgdev Jun 7, 2024
6580f3e
Merge branch 'main' into http-json-example
ramgdev Jun 9, 2024
c51d0eb
Merge branch 'main' into http-json-example
ramgdev Jun 12, 2024
95e6d33
Merge branch 'main' into http-json-example
ramgdev Jun 13, 2024
91498a7
Merge branch 'main' into http-json-example
ramgdev Jun 19, 2024
f3e4fa4
revert proto.rs
ramgdev Jun 19, 2024
89d71fc
Merge branch 'main' into http-json-example
ramgdev Jun 21, 2024
e63bb17
specify protocol
ramgdev Jun 21, 2024
8151c6d
Merge branch 'main' into http-json-example
ramgdev Jun 21, 2024
f2bd44f
Update opentelemetry-otlp/examples/basic-otlp-http/src/main.rs
lalitb Jun 27, 2024
1c01581
Merge branch 'main' into http-json-example
lalitb Jun 27, 2024
fb950df
Merge branch 'main' into http-json-example
ramgdev Jun 28, 2024
a8b8cb0
fix otlp/json exporter (#1882)
lalitb Jun 18, 2024
028ad0e
fix host
ramgdev Jul 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 0 additions & 1 deletion opentelemetry-otlp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ tls-webpki-roots = ["tls", "tonic/tls-webpki-roots"]

# http binary
http-proto = ["prost", "opentelemetry-http", "opentelemetry-proto/gen-tonic-messages", "http", "trace", "metrics"]
# http json This does not work today due to known issue. See https://github.com/open-telemetry/opentelemetry-rust/issues/1763.
http-json = ["serde_json", "prost", "opentelemetry-http", "opentelemetry-proto/gen-tonic-messages", "opentelemetry-proto/with-serde", "http", "trace", "metrics"]
reqwest-blocking-client = ["reqwest/blocking", "opentelemetry-http/reqwest"]
reqwest-client = ["reqwest", "opentelemetry-http/reqwest"]
Expand Down
2 changes: 1 addition & 1 deletion opentelemetry-otlp/examples/basic-otlp-http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ publish = false
once_cell = { workspace = true }
opentelemetry = { path = "../../../opentelemetry" }
opentelemetry_sdk = { path = "../../../opentelemetry-sdk", features = ["rt-tokio", "metrics", "logs"] }
opentelemetry-otlp = { path = "../..", features = ["http-proto", "reqwest-client", "logs"] }
opentelemetry-otlp = { path = "../..", features = ["http-proto", "http-json", "reqwest-client", "logs"] }
lalitb marked this conversation as resolved.
Show resolved Hide resolved
opentelemetry-appender-tracing = { path = "../../../opentelemetry-appender-tracing", default-features = false}
opentelemetry-semantic-conventions = { path = "../../../opentelemetry-semantic-conventions" }

Expand Down
4 changes: 2 additions & 2 deletions opentelemetry-otlp/examples/basic-otlp-http/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Basic OTLP exporter Example

This example shows how to setup OpenTelemetry OTLP exporter for logs, metrics
and traces to exports them to the [OpenTelemetry
and traces to export them to the [OpenTelemetry
Collector](https://github.com/open-telemetry/opentelemetry-collector) via OTLP
over HTTP/protobuf. The Collector then sends the data to the appropriate
using different protocols such as HTTP/json and HTTP/protobuf. The Collector then sends the data to the appropriate
backend, in this case, the logging Exporter, which displays data to console.

## Usage
Expand Down
3 changes: 3 additions & 0 deletions opentelemetry-otlp/examples/basic-otlp-http/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ fn init_logs() -> Result<sdklogs::LoggerProvider, opentelemetry::logs::LogError>
.with_exporter(
opentelemetry_otlp::new_exporter()
.http()
.with_protocol(opentelemetry_otlp::Protocol::HttpBinary)
.with_endpoint("http://localhost:4318/v1/logs"),
)
.install_batch(opentelemetry_sdk::runtime::Tokio)
Expand All @@ -43,6 +44,7 @@ fn init_tracer_provider() -> Result<sdktrace::TracerProvider, TraceError> {
.with_exporter(
opentelemetry_otlp::new_exporter()
.http()
.with_protocol(opentelemetry_otlp::Protocol::HttpBinary)
.with_endpoint("http://localhost:4318/v1/traces"),
)
.with_trace_config(Config::default().with_resource(RESOURCE.clone()))
Expand All @@ -55,6 +57,7 @@ fn init_metrics() -> Result<opentelemetry_sdk::metrics::SdkMeterProvider, Metric
.with_exporter(
opentelemetry_otlp::new_exporter()
.http()
.with_protocol(opentelemetry_otlp::Protocol::HttpBinary)
.with_endpoint("http://localhost:4318/v1/metrics"),
)
.with_resource(RESOURCE.clone())
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