Skip to content

Commit

Permalink
support Oauth
Browse files Browse the repository at this point in the history
  • Loading branch information
xudesheng committed May 9, 2023
1 parent 84f04a2 commit 753e728
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 84 deletions.
87 changes: 55 additions & 32 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tsample"
version = "4.3.4"
version = "4.4.0"
authors = ["xudesheng <[email protected]>"]
edition = "2021"
readme = "README.md"
Expand Down
27 changes: 10 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@

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
2. Thingworx Connection Server
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.
Expand All @@ -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 <the downloaded deb file>```
2. `sudo dpkg -i <the downloaded deb file>`
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
```

Expand All @@ -73,8 +73,6 @@ OPTIONS:
-V, --version Print version information
```



### Generate Sample Configuration File:

```
Expand Down Expand Up @@ -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 /.*/
```

7 changes: 1 addition & 6 deletions src/jmxquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
21 changes: 13 additions & 8 deletions src/tabular.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 0 additions & 11 deletions src/testconfig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
22 changes: 14 additions & 8 deletions src/twxquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<String, JsonValue> = BTreeMap::new();
Expand Down

0 comments on commit 753e728

Please sign in to comment.