From 753e728584355f12ce7bbdec449a4ea883ea9699 Mon Sep 17 00:00:00 2001 From: Xu Desheng Date: Tue, 9 May 2023 02:20:41 -0400 Subject: [PATCH] support Oauth --- CHANGELOG.md | 87 ++++++++++++++++++++++++++++++----------------- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 27 ++++++--------- src/jmxquery.rs | 7 +--- src/tabular.rs | 21 +++++++----- src/testconfig.rs | 11 ------ src/twxquery.rs | 22 +++++++----- 8 files changed, 95 insertions(+), 84 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b84090..a06d84a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,92 +7,115 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## [Unreleased] ### Changed + +## [v4.4.0] - 2023-05-09 + +- support Oauth token for the authentication + +### Changed + ## [v4.3.4] - 2023-01-19 - - Update build cache in order to solve the issue: https://github.com/rustls/rustls/issues/1012 + +- Update build cache in order to solve the issue: https://github.com/rustls/rustls/issues/1012 ### Added + ## [v4.3.3] - 2022-07-05 ### Changed - - Update cargo lock in order to remove the dependency on `smallvec` old version - - Change rust builder version to 1.62.0 + +- Update cargo lock in order to remove the dependency on `smallvec` old version +- Change rust builder version to 1.62.0 ## [v4.3.1] - 2022-05-011 ### Changed - - Support to expose metrics in Prometheus format (http://localhost:19090/metrics). - - Changed the response time unit from nano to milliseconds. + +- Support to expose metrics in Prometheus format (http://localhost:19090/metrics). +- Changed the response time unit from nano to milliseconds. ## [v4.3.0] - 2022-05-011 ### Added - - Support to expose metrics in Prometheus format (http://localhost:19090/metrics). + +- Support to expose metrics in Prometheus format (http://localhost:19090/metrics). ## [v4.2.4] - 2022-05-05 ### Changed - - Fixed the bug: when a query is timed out, the sleep time was not being reset due to u64 overflow. + +- Fixed the bug: when a query is timed out, the sleep time was not being reset due to u64 overflow. ## [v4.2.3] - 2022-04-07 ### Added - - Limited support to flatten the Java TabularData format into InfluxDB Measurement for GC metrics. - - Added sample of `jmx_garbagecollector_status` to capture the GC metrics in the sample configuration file. + +- Limited support to flatten the Java TabularData format into InfluxDB Measurement for GC metrics. +- Added sample of `jmx_garbagecollector_status` to capture the GC metrics in the sample configuration file. ## [v4.2.2] - 2022-04-07 ### Changed - - Support "double" or "java.lang.Double" for the "f64" type. it will be a float in InfluxDB. + +- Support "double" or "java.lang.Double" for the "f64" type. it will be a float in InfluxDB. ## [v4.2.1] - 2022-03-25 ### Changed - - Removed company Email address from the sample configuration file. + +- Removed company Email address from the sample configuration file. ## [v4.2.0] - 2022-03-25 ### Added - - Added `jmx_metrics` in the config, you can grab all possible jmx metrics now. - - added `jmx_c3p0_connections` and `jmx_memory_status` as two examples in the config. + +- Added `jmx_metrics` in the config, you can grab all possible jmx metrics now. +- added `jmx_c3p0_connections` and `jmx_memory_status` as two examples in the config. ### Deleted - - deleted `c3p0_metrics` in the config, it is not used anymore. + +- deleted `c3p0_metrics` in the config, it is not used anymore. ## [v4.1.2] - 2022-03-11 ### Added - - Added `deb` format for downloading. Debian based user can directly install this package. + +- Added `deb` format for downloading. Debian based user can directly install this package. ### Changed - - x86_64 builds will be generated automatically via github actions. - - aarch64 (including Apple Silicon) builds will be generated manually. + +- x86_64 builds will be generated automatically via github actions. +- aarch64 (including Apple Silicon) builds will be generated manually. ## [v4.1.1] - 2022-03-11 ### Changed - - start to use new release workflow + +- start to use new release workflow ## [v4.1.0] - 2022-03-11 ### Added - - support to grab metrics from arbitrary URLs - + +- support to grab metrics from arbitrary URLs + ## [v4.0.1] - 2022-03-11 ### Changed - - This release is only a test for github action workflow. - + +- This release is only a test for github action workflow. + ## [v4.0.0] - 2022-03-09 ### Changed - - Replaced the `influxdb` client driver with the one from the vendor. The original driver was built by myself 3+ years ago and I don't want to maintain it anymore. - - Upgraded the Rust version to the latest stable version (2021 edition 1.59). - - Upgraded tokio library from 0.2 to 1.17. A big jump. - - Replaced the error lib from `failure` to `anyhow` - - Changed the configuration file format from TOML to YAML for readability. - - Changed the optional CSV output format to CSV but JSON payload. - - Support to dynamically grasp the available connection servers. - - Support to summarize the result from the JMX extension output,mainly focus on the C3P0 driver metrics. - - Replace `native-tls` with `rustls` for the TLS library. This will remove the depencency of the OpenSSL installation. - - Added github action to build and release the multiple targets executable files, instead of using local dockers. +- Replaced the `influxdb` client driver with the one from the vendor. The original driver was built by myself 3+ years ago and I don't want to maintain it anymore. +- Upgraded the Rust version to the latest stable version (2021 edition 1.59). +- Upgraded tokio library from 0.2 to 1.17. A big jump. +- Replaced the error lib from `failure` to `anyhow` +- Changed the configuration file format from TOML to YAML for readability. +- Changed the optional CSV output format to CSV but JSON payload. +- Support to dynamically grasp the available connection servers. +- Support to summarize the result from the JMX extension output,mainly focus on the C3P0 driver metrics. +- Replace `native-tls` with `rustls` for the TLS library. This will remove the depencency of the OpenSSL installation. +- Added github action to build and release the multiple targets executable files, instead of using local dockers. diff --git a/Cargo.lock b/Cargo.lock index b3e2be7..3d877ea 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1488,7 +1488,7 @@ checksum = "3528ecfd12c466c6f163363caf2d02a71161dd5e1cc6ae7b34207ea2d42d81ed" [[package]] name = "tsample" -version = "4.3.4" +version = "4.4.0" dependencies = [ "anyhow", "chrono", diff --git a/Cargo.toml b/Cargo.toml index f25ec37..e241712 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tsample" -version = "4.3.4" +version = "4.4.0" authors = ["xudesheng "] edition = "2021" readme = "README.md" diff --git a/README.md b/README.md index 11d1e09..f5a8fcc 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,8 @@ This purpose of this tool is to provide performance metrics for Thingworx. +**Caution**: wherever you need an AppKey, this tool will use Oauth for authentication if the AppKey format is like: `"Bearer [AUTH_TOKEN]"`. Otherwise, it will use AppKey for authentication. + From the version `v4.0.0`, this tool will not support to export the OS level metrics. Instead, it can support the export of the following metrics: 1. Thingworx Subsystems @@ -9,11 +11,12 @@ From the version `v4.0.0`, this tool will not support to export the OS level met 3. JMX C3P0 metrics (you need to install the specific extension first.) 4. Persistent Property metrics (you need to install the specifc extension first.) -* reaching out to PTC TS or your service provider to get the extensions. +- reaching out to PTC TS or your service provider to get the extensions. ## Install ### Provided builds + - [x] Debian format (`deb`) package, best for Debian based systems, like: Ubuntu. - [ ] RPM format (`rpm`) package, best for RedHat based systems, like: Redhat. Please submit an issue to request this build, and tell me which distribution you are using, like: CentOS, Redhat, Fedora etc. The RPM build will be slightly different between the distributions. - [x] Windows (`exe`) package, best for Windows based systems, x86_64 only. @@ -30,27 +33,24 @@ From the version `v4.0.0`, this tool will not support to export the OS level met #### Debian (Ubuntu) based Linux installation 1. Download the `deb` format from the latest release -2. ```sudo dpkg -i ``` +2. `sudo dpkg -i ` 3. Configure `/etc/tsample/tsample.yaml` based on your needs. -4. ```sudo systemctl enable tsample``` (this only needs one time) -5. ```sudo systemctl start tsample``` +4. `sudo systemctl enable tsample` (this only needs one time) +5. `sudo systemctl start tsample` #### Manually Install and configuration 1. The `install.sh` can be a good reference if you are installing on Linux. 2. Download the build based on your OS -3. Export a sample configuration file: ```./tsample -c myconfig.yaml -e``` +3. Export a sample configuration file: `./tsample -c myconfig.yaml -e` 4. Modify your configuration file: `myconfig.yaml` -5. Start to execute it: ```./tsample -c myconfig.yaml``` - - - +5. Start to execute it: `./tsample -c myconfig.yaml` ## How to use ### Help -``` shell +```shell tsample -h ``` @@ -73,8 +73,6 @@ OPTIONS: -V, --version Print version information ``` - - ### Generate Sample Configuration File: ``` @@ -103,19 +101,14 @@ thingworx_servers: app_key: "e5d38c56-c8da-4bff-bba3-06bf3da7474a" ``` - - ### How to run ``` tsample -c myconfig.yml ``` - - ### Workaround to delete all measurements from InfluxDB ```bash DROP SERIES FROM /.*/ ``` - diff --git a/src/jmxquery.rs b/src/jmxquery.rs index 84f8e6f..87a380c 100644 --- a/src/jmxquery.rs +++ b/src/jmxquery.rs @@ -268,12 +268,7 @@ async fn query_jmx_metrics( if let Some(ref name_alternative) = name_alternative { if name_alternative == &row.name { new_sub_name = format!("sub_{}", row.preview); - new_sub_name = new_sub_name - .replace(' ', "_") - .replace('.', "_") - .replace(',', "_") - .replace('[', "_") - .replace(']', "_"); + new_sub_name = new_sub_name.replace([' ', '.', ',', '[', ']'], "_"); } } if row.type_ == "boolean" { diff --git a/src/tabular.rs b/src/tabular.rs index 2916cfa..e1c5d74 100644 --- a/src/tabular.rs +++ b/src/tabular.rs @@ -74,15 +74,20 @@ fn parse_tabular_item_type(items_content: &str) -> HashMap<&str, &str> { for item_type in item_type_iter { let item_name = item_type.get(1); let item_type = item_type.get(2); - match item_name { - Some(item_name) => match item_type { - Some(item_type) => { - result.insert(item_name.as_str(), item_type.as_str()); - } - None => {} - }, - None => {} + if let Some(item_name) = item_name { + if let Some(item_type) = item_type { + result.insert(item_name.as_str(), item_type.as_str()); + } } + // match item_name { + // Some(item_name) => match item_type { + // Some(item_type) => { + // result.insert(item_name.as_str(), item_type.as_str()); + // } + // None => {} + // }, + // None => {} + // } } result diff --git a/src/testconfig.rs b/src/testconfig.rs index 8dd8bb2..f8189af 100644 --- a/src/testconfig.rs +++ b/src/testconfig.rs @@ -158,17 +158,6 @@ fn default_subsystem_enabled() -> bool { true } -impl Default for SubSystem { - fn default() -> Self { - SubSystem { - name: String::from(""), - enabled: false, - options: None, - split_desc_asprefix: false, - sanitize: false, - } - } -} // impl SubSystem { // pub fn new(name: &str) -> Self { // SubSystem { diff --git a/src/twxquery.rs b/src/twxquery.rs index 7d056c7..6a7cc29 100644 --- a/src/twxquery.rs +++ b/src/twxquery.rs @@ -11,7 +11,7 @@ use chrono::offset::Utc; use chrono::DateTime; use influxdb::{/*WriteQuery,*/ Timestamp}; use reqwest::{ - header::{HeaderMap, HeaderValue, CONTENT_TYPE}, + header::{HeaderMap, HeaderValue, CONTENT_TYPE, AUTHORIZATION}, Client, }; use serde_json::Value as JsonValue; @@ -68,7 +68,7 @@ pub async fn launch_twxquery_service( }); } - let scrap_interval = tc.scrap_interval as u64; + let scrap_interval = tc.scrap_interval; let query_timeout = tc.query_time_out; //default should be 20 seconds log::info!("scrap interval is {} seconds, query timeout is:{} seconds.", scrap_interval, query_timeout); while running.load(Ordering::SeqCst) { @@ -173,7 +173,7 @@ pub async fn launch_twxquery_service( } } for task in tasks { - let _ = task.await?; + task.await?; } if !running.load(Ordering::SeqCst) { break; @@ -365,7 +365,13 @@ pub fn construct_headers(app_key: &str) -> HeaderMap { let mut headers = HeaderMap::new(); headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json")); // impossible to fail here - headers.insert("appKey", HeaderValue::from_str(app_key).unwrap()); + if app_key.contains(' ') { + //Oauth token, like "Bearer xxxxx" + headers.insert(AUTHORIZATION, HeaderValue::from_str(app_key).unwrap()); + }else{ + headers.insert("appKey", HeaderValue::from_str(app_key).unwrap()); + } + headers.insert("Accept", HeaderValue::from_static("application/json")); headers @@ -423,17 +429,17 @@ pub async fn query_subsystem_metrics( if row.value.is_none() { continue; } - let row_desc = row.description.unwrap_or_else(|| "".to_string()); + let row_desc = row.description.unwrap_or_default(); let row_value = row.value.unwrap(); //it's safe // Persistent Provider needs to be handled differently let (provider, _description) = match row_desc.find(": ") { Some(start) => ( - (&row_desc[0..start]).to_string(), - (&row_desc[start + 2..]).to_string(), + row_desc[0..start].to_string(), + row_desc[start + 2..].to_string(), ), // common metrics will use 'default' value for provider - None => ("Default".to_string(), (&row_desc).to_string()), + None => ("Default".to_string(), row_desc.to_string()), }; if !metric_value_map.contains_key(&provider) { let value_map: BTreeMap = BTreeMap::new();