fix: Improve debugging ability for influxdb2_client (#5533)

* fix: Separate errors to make debugging easier

* feat: Turn on reqwest verbose connection logging for debugging

https://docs.rs/reqwest/latest/reqwest/struct.ClientBuilder.html#method.connection_verbose

> Enabling this option will emit log messages at the TRACE level for read and write operations on connections.

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
pull/24376/head
Carol (Nichols || Goulding) 2022-09-02 01:09:10 -04:00 committed by GitHub
parent d219f93241
commit d84b062a69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 6 deletions

View File

@ -2,7 +2,10 @@
//!
//! Query InfluxDB using InfluxQL or Flux Query
use crate::{Client, HttpSnafu, RequestError, ReqwestProcessingSnafu, SerializingSnafu};
use crate::{
Client, HttpSnafu, RequestError, ReqwestProcessingSnafu, ResponseBytesSnafu,
ResponseStringSnafu, SerializingSnafu,
};
use reqwest::{Method, StatusCode};
use snafu::ResultExt;
@ -73,10 +76,20 @@ impl Client {
.context(ReqwestProcessingSnafu)?;
match response.status() {
StatusCode::OK => Ok(response
.json::<String>()
.await
.context(ReqwestProcessingSnafu)?),
StatusCode::OK => {
let bytes = response.bytes().await.context(ResponseBytesSnafu)?;
let json_result = serde_json::from_slice::<String>(&bytes);
match json_result {
Ok(json) => Ok(json),
Err(source) => {
let text =
String::from_utf8(bytes.to_vec()).context(ResponseStringSnafu)?;
Err(RequestError::DeserializingJsonResponse { source, text })
}
}
}
status => {
let text = response.text().await.context(ReqwestProcessingSnafu)?;
HttpSnafu { status, text }.fail()?

View File

@ -92,6 +92,29 @@ pub enum RequestError {
/// The underlying error object from `serde_json`.
source: serde_json::error::Error,
},
/// While deserializing the response as JSON, something went wrong.
#[snafu(display("Could not deserialize as JSON. Error: {source}\nText: `{text}`"))]
DeserializingJsonResponse {
/// The text of the response
text: String,
/// The underlying error object from serde
source: serde_json::Error,
},
/// Something went wrong getting the raw bytes of the response
#[snafu(display("Could not get response bytes: {source}"))]
ResponseBytes {
/// The underlying error object from reqwest
source: reqwest::Error,
},
/// Something went wrong converting the raw bytes of the response to a UTF-8 string
#[snafu(display("Invalid UTF-8: {source}"))]
ResponseString {
/// The underlying error object from std
source: std::string::FromUtf8Error,
},
}
/// Client to a server supporting the InfluxData 2.0 API.
@ -128,7 +151,10 @@ impl Client {
Self {
url: url.into(),
auth_header,
reqwest: reqwest::Client::new(),
reqwest: reqwest::Client::builder()
.connection_verbose(true)
.build()
.expect("reqwest::Client should have built"),
jaeger_debug_header: None,
}
}