Skip to content

Commit

Permalink
do not convert the opentelemetry value to a string then an i64 (#5376)
Browse files Browse the repository at this point in the history
  • Loading branch information
Geal authored Jun 11, 2024
1 parent e2d3163 commit a0b9d34
Showing 1 changed file with 28 additions and 54 deletions.
82 changes: 28 additions & 54 deletions apollo-router/src/plugins/telemetry/config_new/instruments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1141,6 +1141,16 @@ pub(crate) enum Increment {
FieldCustom(Option<i64>),
}

fn to_i64(value: opentelemetry::Value) -> Option<i64> {
match value {
opentelemetry::Value::I64(i) => Some(i),
opentelemetry::Value::String(s) => s.as_str().parse::<i64>().ok(),
opentelemetry::Value::F64(f) => Some(f.floor() as i64),
opentelemetry::Value::Bool(_) => None,
opentelemetry::Value::Array(_) => None,
}
}

pub(crate) struct CustomCounter<Request, Response, A, T>
where
A: Selectors<Request = Request, Response = Response> + Default,
Expand Down Expand Up @@ -1187,12 +1197,8 @@ where

if let Some(selected_value) = inner.selector.as_ref().and_then(|s| s.on_request(request)) {
let new_incr = match &inner.increment {
Increment::EventCustom(None) => {
Increment::EventCustom(selected_value.as_str().parse::<i64>().ok())
}
Increment::Custom(None) => {
Increment::Custom(selected_value.as_str().parse::<i64>().ok())
}
Increment::EventCustom(None) => Increment::EventCustom(to_i64(selected_value)),
Increment::Custom(None) => Increment::Custom(to_i64(selected_value)),
other => {
failfast_error!("this is a bug and should not happen, the increment should only be Custom or EventCustom, please open an issue: {other:?}");
return;
Expand Down Expand Up @@ -1231,12 +1237,8 @@ where
.and_then(|s| s.on_response(response))
{
let new_incr = match &inner.increment {
Increment::EventCustom(None) => {
Increment::Custom(selected_value.as_str().parse::<i64>().ok())
}
Increment::Custom(None) => {
Increment::Custom(selected_value.as_str().parse::<i64>().ok())
}
Increment::EventCustom(None) => Increment::Custom(to_i64(selected_value)),
Increment::Custom(None) => Increment::Custom(to_i64(selected_value)),
other => {
failfast_error!("this is a bug and should not happen, the increment should only be Custom or EventCustom, please open an issue: {other:?}");
return;
Expand Down Expand Up @@ -1292,12 +1294,8 @@ where
.and_then(|s| s.on_response_event(response, ctx))
{
let new_incr = match &inner.increment {
Increment::EventCustom(None) => {
Increment::EventCustom(selected_value.as_str().parse::<i64>().ok())
}
Increment::Custom(None) => {
Increment::EventCustom(selected_value.as_str().parse::<i64>().ok())
}
Increment::EventCustom(None) => Increment::EventCustom(to_i64(selected_value)),
Increment::Custom(None) => Increment::EventCustom(to_i64(selected_value)),
other => {
failfast_error!("this is a bug and should not happen, the increment should only be Custom or EventCustom, please open an issue: {other:?}");
return;
Expand Down Expand Up @@ -1384,12 +1382,8 @@ where
.and_then(|s| s.on_response_field(ty, field, value, ctx))
{
let new_incr = match &inner.increment {
Increment::FieldCustom(None) => {
Increment::FieldCustom(selected_value.as_str().parse::<i64>().ok())
}
Increment::Custom(None) => {
Increment::FieldCustom(selected_value.as_str().parse::<i64>().ok())
}
Increment::FieldCustom(None) => Increment::FieldCustom(to_i64(selected_value)),
Increment::Custom(None) => Increment::FieldCustom(to_i64(selected_value)),
other => {
failfast_error!("this is a bug and should not happen, the increment should only be Custom or FieldCustom, please open an issue: {other:?}");
return;
Expand Down Expand Up @@ -1603,15 +1597,9 @@ where
}
if let Some(selected_value) = inner.selector.as_ref().and_then(|s| s.on_request(request)) {
let new_incr = match &inner.increment {
Increment::EventCustom(None) => {
Increment::EventCustom(selected_value.as_str().parse::<i64>().ok())
}
Increment::FieldCustom(None) => {
Increment::FieldCustom(selected_value.as_str().parse::<i64>().ok())
}
Increment::Custom(None) => {
Increment::Custom(selected_value.as_str().parse::<i64>().ok())
}
Increment::EventCustom(None) => Increment::EventCustom(to_i64(selected_value)),
Increment::FieldCustom(None) => Increment::FieldCustom(to_i64(selected_value)),
Increment::Custom(None) => Increment::Custom(to_i64(selected_value)),
other => {
failfast_error!("this is a bug and should not happen, the increment should only be Custom or EventCustom, please open an issue: {other:?}");
return;
Expand Down Expand Up @@ -1648,15 +1636,9 @@ where
.and_then(|s| s.on_response(response))
{
let new_incr = match &inner.increment {
Increment::EventCustom(None) => {
Increment::EventCustom(selected_value.as_str().parse::<i64>().ok())
}
Increment::FieldCustom(None) => {
Increment::FieldCustom(selected_value.as_str().parse::<i64>().ok())
}
Increment::Custom(None) => {
Increment::Custom(selected_value.as_str().parse::<i64>().ok())
}
Increment::EventCustom(None) => Increment::EventCustom(to_i64(selected_value)),
Increment::FieldCustom(None) => Increment::FieldCustom(to_i64(selected_value)),
Increment::Custom(None) => Increment::Custom(to_i64(selected_value)),
other => {
failfast_error!("this is a bug and should not happen, the increment should only be Custom or EventCustom, please open an issue: {other:?}");
return;
Expand Down Expand Up @@ -1708,12 +1690,8 @@ where
.and_then(|s| s.on_response_event(response, ctx))
{
let new_incr = match &inner.increment {
Increment::EventCustom(None) => {
Increment::EventCustom(selected_value.as_str().parse::<i64>().ok())
}
Increment::Custom(None) => {
Increment::EventCustom(selected_value.as_str().parse::<i64>().ok())
}
Increment::EventCustom(None) => Increment::EventCustom(to_i64(selected_value)),
Increment::Custom(None) => Increment::EventCustom(to_i64(selected_value)),
other => {
failfast_error!("this is a bug and should not happen, the increment should only be Custom or EventCustom, please open an issue: {other:?}");
return;
Expand Down Expand Up @@ -1796,12 +1774,8 @@ where
.and_then(|s| s.on_response_field(ty, field, value, ctx))
{
let new_incr = match &inner.increment {
Increment::FieldCustom(None) => {
Increment::FieldCustom(selected_value.as_str().parse::<i64>().ok())
}
Increment::Custom(None) => {
Increment::FieldCustom(selected_value.as_str().parse::<i64>().ok())
}
Increment::FieldCustom(None) => Increment::FieldCustom(to_i64(selected_value)),
Increment::Custom(None) => Increment::FieldCustom(to_i64(selected_value)),
other => {
failfast_error!("this is a bug and should not happen, the increment should only be Custom or FieldCustom, please open an issue: {other:?}");
return;
Expand Down

0 comments on commit a0b9d34

Please sign in to comment.