Skip to content

Commit

Permalink
Merge pull request #11 from vs4vijay/influxdb-2-compatible
Browse files Browse the repository at this point in the history
Influxdb 2 compatible
  • Loading branch information
vs4vijay authored Jan 9, 2021
2 parents bcc3fc0 + 81735ce commit 6ebeb61
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 34 deletions.
57 changes: 34 additions & 23 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,57 @@ PROJECT = "newman-reporter-influxdb"

.PHONY: local-install

install-local:
local-install:
npm uninstall -g ${PROJECT}
npm pack
npm install -g ${PROJECT}-*.tgz

test:
test-v1:
newman run https://www.getpostman.com/collections/631643-f695cab7-6878-eb55-7943-ad88e1ccfd65-JsLv -r influxdb \
--reporter-influxdb-server localhost \
--reporter-influxdb-port 8086 \
--reporter-influxdb-name vijay \
--reporter-influxdb-measurement okay
--reporter-influxdb-server localhost \
--reporter-influxdb-port 8086 \
--reporter-influxdb-name vijay \
--reporter-influxdb-measurement okay

test-v2:
newman run https://www.getpostman.com/collections/631643-f695cab7-6878-eb55-7943-ad88e1ccfd65-JsLv -r influxdb \
--reporter-influxdb-server localhost \
--reporter-influxdb-port 8086 \
--reporter-influxdb-org viz \
--reporter-influxdb-version 2 \
--reporter-influxdb-username viz \
--reporter-influxdb-password db123456 \
--reporter-influxdb-name viz \
--reporter-influxdb-measurement api_results

test-failed:
newman run test/failed.json -r influxdb \
--reporter-influxdb-server localhost \
--reporter-influxdb-port 8086 \
--reporter-influxdb-name vijay \
--reporter-influxdb-measurement okay
--reporter-influxdb-server localhost \
--reporter-influxdb-port 8086 \
--reporter-influxdb-name vijay \
--reporter-influxdb-measurement okay

failed:
newman run test/failed.json -r influxdb \
--reporter-influxdb-server localhost \
--reporter-influxdb-port 8086 \
--reporter-influxdb-name vijay \
--reporter-influxdb-measurement okay \
--reporter-influxdb-mode udp
--reporter-influxdb-server localhost \
--reporter-influxdb-port 8086 \
--reporter-influxdb-name vijay \
--reporter-influxdb-measurement okay \
--reporter-influxdb-mode udp

test-remote:
newman run test/failed.json -r influxdb \
--reporter-influxdb-server 192.168.100.40 \
--reporter-influxdb-port 8086 \
--reporter-influxdb-name failed \
--reporter-influxdb-measurement failed6
--reporter-influxdb-server 192.168.100.40 \
--reporter-influxdb-port 8086 \
--reporter-influxdb-name failed \
--reporter-influxdb-measurement failed6

test-udp:
newman run https://www.getpostman.com/collections/631643-f695cab7-6878-eb55-7943-ad88e1ccfd65-JsLv -r influxdb \
--reporter-influxdb-server localhost \
--reporter-influxdb-port 8086 \
--reporter-influxdb-name vijay \
--reporter-influxdb-mode udp
--reporter-influxdb-server localhost \
--reporter-influxdb-port 8086 \
--reporter-influxdb-name vijay \
--reporter-influxdb-mode udp

bump:
npm version patch -m "Bumped to version %s"
Expand Down
42 changes: 38 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# newman-reporter-influxdb

InfluxDB reporter for [Newman](https://github.com/postmanlabs/newman) that sends the test results information to InfluxDB which can be used from Grafana to build dashboard.
InfluxDB reporter for [Newman](https://github.com/postmanlabs/newman) that sends the test results information to InfluxDB (1.x, 2.x) which can be used from Grafana to build dashboard.

<a href="https://www.npmjs.com/package/newman-reporter-influxdb">
<img alt="npm version" src="https://img.shields.io/npm/v/newman-reporter-influxdb.svg">
Expand Down Expand Up @@ -45,23 +45,46 @@ newman run <collection-url> -r influxdb \
--reporter-influxdb-measurement <measurement-name>
```

- By default, reporter consider influxdb version 1.x (i.e 1.7, 1.8)
- In case of InfluxDB version 2, specify version, org and bucket name as well
- `--reporter-influxdb-version 2`
- `--reporter-influxdb-org <org-name>`
- `--reporter-influxdb-name <bucket-name>`

Example:

```
# For InfluxDB version 1.x
newman run https://www.getpostman.com/collections/631643-f695cab7-6878-eb55-7943-ad88e1ccfd65-JsLv -r influxdb \
--reporter-influxdb-server localhost \
--reporter-influxdb-port 8086 \
--reporter-influxdb-name newman_reports \
--reporter-influxdb-measurement api_results
# For InfluxDB version 2.x
newman run https://www.getpostman.com/collections/631643-f695cab7-6878-eb55-7943-ad88e1ccfd65-JsLv -r influxdb \
--reporter-influxdb-server localhost \
--reporter-influxdb-port 8086 \
--reporter-influxdb-org viz \
--reporter-influxdb-version 2 \
--reporter-influxdb-username viz \
--reporter-influxdb-password db123456 \
--reporter-influxdb-name viz \
--reporter-influxdb-measurement api_results
```

#### Options:
### Options:

**Option** | **Remarks**
--- | ---
`--reporter-influxdb-server` | IP Address or Host of InfluxDB
`--reporter-influxdb-port` | Port no. (Usually `8086`)
`--reporter-influxdb-name` | Database name
`--reporter-influxdb-version` | InfluxDB Version (default `1`)
`--reporter-influxdb-org` | InfluxDB Org (For InfluxDB version 2.x)
`--reporter-influxdb-port` | Port no. (Usually `8086`)
`--reporter-influxdb-name` | Database name (or Bucket name for InfluxDB version 2.x)
`--reporter-influxdb-measurement` | Measurement Point name (If not provided, then reporter will create measurement with prefix `newman_results-<timestamp>`)
`--reporter-influxdb-username` (*Optional*) | Username created for InfluxDB (e.g. `newman_user`)
`--reporter-influxdb-password` (*Optional*) | Password of the user (e.g. `p@ssw0rd`)
Expand All @@ -74,9 +97,10 @@ newman run https://www.getpostman.com/collections/631643-f695cab7-6878-eb55-7943
**newman-reporter-influxdb** | **InfluxDB**
--- | ---
v1.0.0+ | v1.7
v2.0.0+ | v1.8, v2.x

#### Notes:
- This reporter currently uses InfluxDB HTTP APIs to send data
- This reporter currently uses InfluxDB HTTP APIs / TCP Protocol to send data

---

Expand All @@ -91,13 +115,17 @@ v1.0.0+ | v1.7
- [x] CI/CD with Github Actions
- [ ] HealthCheck to InfluxDB
- [ ] Remove axios to make it lightweight
- [x] Compatibility with InfluxDB 2.x
- [ ] Tests

---

## Development

- `npm pack`
- `npm i -g newman-reporter-<name>.<version>.tgz`
- OR `make local-install`
- `make test-v1` / `make test-v2`

---

Expand Down Expand Up @@ -168,4 +196,10 @@ name: newman_results
time value
2020-26-06T11:25:15.321527811Z 1
curl -G http://localhost:8086/query --data-urlencode "q=CREATE DATABASE newman_reports"
docker run --name influxdb-1.7 -p 8086:8086 influxdb:1.7
docker run --name influxdb-1.8 -p 8086:8086 influxdb:1.8
docker run --name influxdb-2 -p 8086:8086 quay.io/influxdb/influxdb:v2.0.3
```
2 changes: 1 addition & 1 deletion package-lock.json

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

64 changes: 60 additions & 4 deletions src/http.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@ class HttpService {

constructor(context) {
this.context = context;
const axiosOptions = {
baseURL: `http://${this.context.server}:${this.context.port}`
};

if(this.context.version == 2) {
// For InfluxDB version 2.x
axiosOptions.auth = {
username: this.context.username,
password: this.context.password,
}
}

this.client = axios.create(axiosOptions);

if(this.context.version == 2) {
this.signIn();
}
}

_buildInfluxDBUrl(path='write') {
Expand All @@ -24,6 +41,25 @@ class HttpService {
return connectionUrl;
}

async signIn() {
console.log('[+] Signing In to InfluxDB');
try {
const res = await this.client.post('/api/v2/signin');
this.client.defaults.headers.common['cookie'] = res.headers['set-cookie'];
} catch (error) {
console.log('[-] ERROR: while signing in to InfluxDB', error);
}
}

async signOut() {
console.log('[+] Signing Out from InfluxDB');
try {
await this.client.post('/api/v2/signout');
} catch (error) {
console.log('[-] ERROR: while signing out to InfluxDB', error);
}
}

async healthCheck() {
let connectionUrl = this._buildInfluxDBUrl('ping');

Expand All @@ -35,17 +71,37 @@ class HttpService {
}

async sendData(data) {
let connectionUrl = this._buildInfluxDBUrl();
let url;

if(this.context.version == 1) {
const params = {
db: this.context.name,
u: this.context.username,
p: this.context.password,
};
const paramsQuerystring = querystring.stringify(params);
url = `/write?${paramsQuerystring}`;
} else {
// For InfluxDB version 2
const params = {
bucket: this.context.name,
org: this.context.org,
};
const paramsQuerystring = querystring.stringify(params);
url = `/api/v2/write?${paramsQuerystring}`;
}

try {
await axios.post(connectionUrl, data);
await this.client.post(url, data);
} catch (error) {
console.log('[-] ERROR: while sending data to InfluxDB', error);
}
}

close() {

disconnect() {
if(this.context.version == 2) {
this.signOut();
}
}

};
Expand Down
12 changes: 10 additions & 2 deletions src/influxdb-reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class InfluxDBReporter {
start(error, args) {
this.context.server = this.reporterOptions.influxdbServer || this.reporterOptions.server;
this.context.port = this.reporterOptions.influxdbPort || this.reporterOptions.port;
this.context.version = this.reporterOptions.influxdbVersion || this.reporterOptions.version || 1;
this.context.org = this.reporterOptions.influxdbOrg || this.reporterOptions.org;
this.context.name = this.reporterOptions.influxdbName || this.reporterOptions.name;
this.context.measurement = this.reporterOptions.influxdbMeasurement || this.reporterOptions.measurement;
this.context.username = this.reporterOptions.influxdbUsername || this.reporterOptions.username;
Expand All @@ -43,8 +45,13 @@ class InfluxDBReporter {
if (!this.context.port) {
throw new Error('[-] ERROR: InfluxDB Server Port is missing! Add --reporter-influxdb-port <port-number>.');
}
if(this.context.version == 2) {
if (!this.context.org) {
throw new Error('[-] ERROR: InfluxDB v2.x Org is missing! Add --reporter-influxdb-org <org-name>.');
}
}
if (!this.context.name) {
throw new Error('[-] ERROR: InfluxDB Database Name is missing! Add --reporter-influxdb-name <database-name>.');
throw new Error('[-] ERROR: InfluxDB Database/Bucket Name is missing! Add --reporter-influxdb-name <database-name>.');
}
if (!this.context.measurement) {
// this.context.measurement = `api_results_${new Date().getTime()}`;
Expand All @@ -53,10 +60,10 @@ class InfluxDBReporter {
if (!this.context.mode) {
this.context.mode = 'http';
}
console.log(`[+] Starting collection: ${this.options.collection.name} ${this.context.id}`);

const DataService = this.context.mode === 'udp' ? UdpService : HttpService;
this.service = new DataService(this.context);
console.log(`Starting collection: ${this.options.collection.name} ${this.context.id}`);
}

beforeItem(error, args) {
Expand Down Expand Up @@ -133,6 +140,7 @@ class InfluxDBReporter {
}

done() {
this.service.disconnect();
console.log(`[+] Finished collection: ${this.options.collection.name} (${this.context.id})`);

// console.log('this.context', this.context);
Expand Down

0 comments on commit 6ebeb61

Please sign in to comment.