diff --git a/package-lock.json b/package-lock.json index ecffb19..1fe66c7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,10 +9,10 @@ "version": "1.0.17", "license": "ISC", "dependencies": { - "@influxdata/influxdb-client": "^1.33.2", "async-retry": "^1.3.3", "dotenv": "^14.3.2", "influxdb-v1": "^1.0.4", + "influxdb-v2": "^1.0.0", "pactum-matchers": "^1.1.6", "performance-results-parser": "latest", "phin-retry": "^1.0.3", @@ -100,11 +100,6 @@ "integrity": "sha512-dGGHpb61hLwifAu7sotuHFDBw6GTdpG8aKC0fsK17EuTzMRvUrH7lEAr6LTJ+sx3AZYed9yZ77rltVDHyg2hRg==", "dev": true }, - "node_modules/@influxdata/influxdb-client": { - "version": "1.33.2", - "resolved": "https://registry.npmjs.org/@influxdata/influxdb-client/-/influxdb-client-1.33.2.tgz", - "integrity": "sha512-RT5SxH+grHAazo/YK3UTuWK/frPWRM0N7vkrCUyqVprDgQzlLP+bSK4ak2Jv3QVF/pazTnsxWjvtKZdwskV5Xw==" - }, "node_modules/@istanbuljs/schema": { "version": "0.1.3", "resolved": "https://registry.npmjs.org/@istanbuljs/schema/-/schema-0.1.3.tgz", @@ -1189,6 +1184,14 @@ "node": ">=8" } }, + "node_modules/influxdb-v2": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/influxdb-v2/-/influxdb-v2-1.0.0.tgz", + "integrity": "sha512-zQEINa2Byhd+iSN4pMjjvRtwIaCI0LeZs8O8Q+K1VPgY1WV+ZMc923LqUwSZXKHMzJkuMP76nSSlkg1PGbaePQ==", + "dependencies": { + "node-fetch": "^2.6.1" + } + }, "node_modules/inherits": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", @@ -1755,7 +1758,6 @@ "version": "2.6.7", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz", "integrity": "sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==", - "dev": true, "dependencies": { "whatwg-url": "^5.0.0" }, @@ -2630,8 +2632,7 @@ "node_modules/tr46": { "version": "0.0.3", "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", - "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==", - "dev": true + "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==" }, "node_modules/trouter": { "version": "2.0.1", @@ -2689,14 +2690,12 @@ "node_modules/webidl-conversions": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", - "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==", - "dev": true + "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==" }, "node_modules/whatwg-url": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", "integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==", - "dev": true, "dependencies": { "tr46": "~0.0.3", "webidl-conversions": "^3.0.0" diff --git a/package.json b/package.json index c146ad1..43a46b3 100644 --- a/package.json +++ b/package.json @@ -46,11 +46,10 @@ }, "homepage": "https://test-results-reporter.github.io", "dependencies": { - "@influxdata/influxdb-client": "^1.33.2", "async-retry": "^1.3.3", "dotenv": "^14.3.2", "influxdb-v1": "^1.0.4", - "pactum-matchers": "^1.1.6", + "influxdb-v2": "^1.0.0", "performance-results-parser": "latest", "phin-retry": "^1.0.3", "pretty-ms": "^7.0.1", diff --git a/src/targets/influx2.js b/src/targets/influx2.js index f731f22..0c3d2a2 100644 --- a/src/targets/influx2.js +++ b/src/targets/influx2.js @@ -1,4 +1,4 @@ -const { Point, InfluxDB, WriteApi } = require("@influxdata/influxdb-client"); +const Influxdb = require("influxdb-v2"); const Metric = require('performance-results-parser/src/models/Metric'); const PerformanceTestResult = require('performance-results-parser/src/models/PerformanceTestResult'); const Transaction = require('performance-results-parser/src/models/Transaction'); @@ -6,7 +6,6 @@ const TestCase = require('test-results-parser/src/models/TestCase'); const TestResult = require('test-results-parser/src/models/TestResult'); const TestSuite = require('test-results-parser/src/models/TestSuite'); - const { STATUS } = require('../helpers/constants'); /** @@ -19,85 +18,35 @@ async function run({ result, target }) { target.inputs = Object.assign({}, default_inputs, target.inputs); const metrics = getMetrics({ result, target }); - const influxDBClient = getInfluxDBClient(target.inputs.url, - target.inputs.token, - target.inputs.organization, - target.inputs.bucket, - target.inputs.precision, - target.inputs.tags, - target.inputs.gzipTransport) - - influxDBClient.writePoints(metrics); - - await influxDBClient.close() -} - -/** - * Creates an API client for writing to InfluxDB 2.x - * @param url - * @param token - * @param organization - * @param bucket - * @param precision - * @param defaultTags - * @param gzipTransport - * @returns {WriteApi} - */ -function getInfluxDBClient(url, token, organization, bucket, precision, defaultTags, gzipTransport) { - const writeOptions = {} - if(!gzipTransport) { - writeOptions['gzipThreshold'] = undefined + const influxDBClient = new Influxdb({ + host: target.inputs.host, + protocol: target.inputs.protocol, + port: target.inputs.port, + token: target.inputs.token + }) + + const writeOptions = { + org: target.inputs.org, + bucket: target.inputs.bucket, + precision: target.inputs.precision } - const client = new InfluxDB({ url, token}) - .getWriteApi(organization, bucket, precision, writeOptions) - client.useDefaultTags(defaultTags) - return client + await influxDBClient.write(writeOptions, metrics); } /** - * Creates a {Point} object + * Creates a metric object * @param measurement * @param tags * @param fields - * @returns {Point} - */ -function createPoint(measurement, tags, fields) { - const point = new Point(measurement) - - for (const [tagName, tagValue] of Object.entries(tags)) { - point.tag(tagName, tagValue) - } - - for (const [fieldName, fieldValue] of Object.entries(fields)) { - addFieldToPoint(point, fieldName, fieldValue) - } - - return point -} - -/** - * Adds a single field to {Point} based on the data type - * @param point - * @param fieldName - * @param fieldValue + * @returns */ -function addFieldToPoint(point, fieldName, fieldValue) { - if (typeof fieldValue === 'boolean') { - point.booleanField(fieldName, fieldValue) - return - } - - if (typeof fieldValue === 'number') { - point.floatField(fieldName, fieldValue) - return - } - - point.stringField(fieldName, fieldValue) +function createMetric(measurement, tags, fields) { + return { measurement, tags, fields } } /** - * Converts the parsed metrics to InfluxDB point format + * Converts the parsed metrics to InfluxDB metric format * @param {object} param0 * @param {PerformanceTestResult | TestResult} param0.result * @param {import('..').Target} param0.target @@ -143,7 +92,7 @@ function getPerfRunInfluxMetric({ result, target }) { setPerfInfluxMetricFields(metric, fields); } - return createPoint(target.inputs.measurement_perf_run, tags, fields) + return createMetric(target.inputs.measurement_perf_run, tags, fields) } /** @@ -184,7 +133,7 @@ function getTransactionInfluxMetric(transaction, target) { setPerfInfluxMetricFields(metric, fields); } - return createPoint(target.inputs.measurement_perf_run, tags, fields) + return createMetric(target.inputs.measurement_perf_transaction, tags, fields) } /** @@ -205,7 +154,7 @@ function getTestInfluxMetric({ result, target }, measurement) { fields.failed = result.failed; fields.duration = result.duration; - return createPoint(measurement, tags, fields) + return createMetric(measurement, tags, fields) } /** @@ -223,16 +172,17 @@ function getTestCaseInfluxMetric({ result, target }) { fields.status = result.status === 'PASS' ? 0 : 1; fields.duration = result.duration; - return createPoint(target.inputs.measurement_test_case, tags, fields) + return createMetric(target.inputs.measurement_test_case, tags, fields) } const default_inputs = { - url: '', + host: '', + protocol: 'https', + port: 443, token: '', - organization: '', + org: '', bucket: '', precision: 'ns', - gzipTransport: true, environment: 'test', measurement_perf_run: 'PerfRun', measurement_perf_transaction: 'PerfTransaction', diff --git a/test/mocks/influx2.mock.js b/test/mocks/influx2.mock.js index a5d650c..2dec32b 100644 --- a/test/mocks/influx2.mock.js +++ b/test/mocks/influx2.mock.js @@ -1,9 +1,7 @@ const { addInteractionHandler } = require('pactum').handler; -const { regex } = require('pactum-matchers'); addInteractionHandler('save perf results to influx2', () => { return { - strict: false, request: { method: 'POST', path: '/api/v2/write', @@ -15,7 +13,9 @@ addInteractionHandler('save perf results to influx2', () => { "bucket": "testbucket", "precision": "ns" }, - body: regex('PerfRun', /PerfRun,Name=TOTAL,Status=PASS data_received_rate=5166.44,data_received_sum=362818330,data_sent_rate=38.87,data_sent_sum=2729683,duration_avg=4660,duration_max=15513,duration_med=3318,duration_min=1135,duration_p90=11354,duration_p95=11446,duration_p99=15513,errors_rate=0,errors_sum=0,samples_rate=0.55535,samples_sum=39,status=0,transactions=2,transactions_failed=0,transactions_passed=2 \d+\nPerfRun,Name=S01_T01_Application_Launch,Status=PASS data_received_rate=2662.79,data_received_sum=184633892,data_sent_rate=5.36,data_sent_sum=371654,duration_avg=3086,duration_max=3797,duration_med=2832,duration_min=2119,duration_p90=3795,duration_p95=3795,duration_p99=3797,errors_rate=0.001,errors_sum=0,samples_rate=0.14422,samples_sum=10,status=0 \d+\nPerfRun,Name=S01_T02_Application_Login,Status=PASS data_received_rate=2754.9,data_received_sum=169706365,data_sent_rate=12.94,data_sent_sum=797125,duration_avg=4355,duration_max=10786,duration_med=3273,duration_min=3042,duration_p90=4416,duration_p95=10786,duration_p99=10786,errors_rate=0,errors_sum=0,samples_rate=0.1461,samples_sum=9,status=0 \d+/) + body: "PerfRun,Name=TOTAL,Status=PASS status=0,transactions=2,transactions_passed=2,transactions_failed=0,samples_sum=39,samples_rate=0.55535,duration_avg=4660,duration_med=3318,duration_max=15513,duration_min=1135,duration_p90=11354,duration_p95=11446,duration_p99=15513,errors_sum=0,errors_rate=0,data_sent_sum=2729683,data_sent_rate=38.87,data_received_sum=362818330,data_received_rate=5166.44\n"+ + "PerfTransaction,Name=S01_T01_Application_Launch,Status=PASS status=0,samples_sum=10,samples_rate=0.14422,duration_avg=3086,duration_med=2832,duration_max=3797,duration_min=2119,duration_p90=3795,duration_p95=3795,duration_p99=3797,errors_sum=0,errors_rate=0.001,data_sent_sum=371654,data_sent_rate=5.36,data_received_sum=184633892,data_received_rate=2662.79\n"+ + "PerfTransaction,Name=S01_T02_Application_Login,Status=PASS status=0,samples_sum=9,samples_rate=0.1461,duration_avg=4355,duration_med=3273,duration_max=10786,duration_min=3042,duration_p90=4416,duration_p95=10786,duration_p99=10786,errors_sum=0,errors_rate=0,data_sent_sum=797125,data_sent_rate=12.94,data_received_sum=169706365,data_received_rate=2754.9" }, response: { status: 204 @@ -25,7 +25,6 @@ addInteractionHandler('save perf results to influx2', () => { addInteractionHandler('save perf results with custom tags and fields to influx2', () => { return { - strict: false, request: { method: 'POST', path: '/api/v2/write', @@ -37,7 +36,9 @@ addInteractionHandler('save perf results with custom tags and fields to influx2' "bucket": "testbucket", "precision": "ns" }, - body: regex("PerfRun", /PerfRun,App=PactumJS,Name=TOTAL,Status=PASS,Team=QA data_received_rate=5166.44,data_received_sum=362818330,data_sent_rate=38.87,data_sent_sum=2729683,duration_avg=4660,duration_max=15513,duration_med=3318,duration_min=1135,duration_p90=11354,duration_p95=11446,duration_p99=15513,errors_rate=0,errors_sum=0,id=123,samples_rate=0.55535,samples_sum=39,status=0,transactions=2,transactions_failed=0,transactions_passed=2 \d+\nPerfRun,App=PactumJS,Name=S01_T01_Application_Launch,Status=PASS,Team=QA data_received_rate=2662.79,data_received_sum=184633892,data_sent_rate=5.36,data_sent_sum=371654,duration_avg=3086,duration_max=3797,duration_med=2832,duration_min=2119,duration_p90=3795,duration_p95=3795,duration_p99=3797,errors_rate=0.001,errors_sum=0,id=123,samples_rate=0.14422,samples_sum=10,status=0 \d+\nPerfRun,App=PactumJS,Name=S01_T02_Application_Login,Status=PASS,Team=QA data_received_rate=2754.9,data_received_sum=169706365,data_sent_rate=12.94,data_sent_sum=797125,duration_avg=4355,duration_max=10786,duration_med=3273,duration_min=3042,duration_p90=4416,duration_p95=10786,duration_p99=10786,errors_rate=0,errors_sum=0,id=123,samples_rate=0.1461,samples_sum=9,status=0 \d+/) + body: "PerfRun,Team=QA,App=PactumJS,Name=TOTAL,Status=PASS id=123,status=0,transactions=2,transactions_passed=2,transactions_failed=0,samples_sum=39,samples_rate=0.55535,duration_avg=4660,duration_med=3318,duration_max=15513,duration_min=1135,duration_p90=11354,duration_p95=11446,duration_p99=15513,errors_sum=0,errors_rate=0,data_sent_sum=2729683,data_sent_rate=38.87,data_received_sum=362818330,data_received_rate=5166.44\n"+ + "PerfTransaction,Team=QA,App=PactumJS,Name=S01_T01_Application_Launch,Status=PASS id=123,status=0,samples_sum=10,samples_rate=0.14422,duration_avg=3086,duration_med=2832,duration_max=3797,duration_min=2119,duration_p90=3795,duration_p95=3795,duration_p99=3797,errors_sum=0,errors_rate=0.001,data_sent_sum=371654,data_sent_rate=5.36,data_received_sum=184633892,data_received_rate=2662.79\n"+ + "PerfTransaction,Team=QA,App=PactumJS,Name=S01_T02_Application_Login,Status=PASS id=123,status=0,samples_sum=9,samples_rate=0.1461,duration_avg=4355,duration_med=3273,duration_max=10786,duration_min=3042,duration_p90=4416,duration_p95=10786,duration_p99=10786,errors_sum=0,errors_rate=0,data_sent_sum=797125,data_sent_rate=12.94,data_received_sum=169706365,data_received_rate=2754.9" }, response: { status: 204 @@ -47,7 +48,6 @@ addInteractionHandler('save perf results with custom tags and fields to influx2' addInteractionHandler('save test results to influx2', () => { return { - strict: false, request: { method: 'POST', path: '/api/v2/write', @@ -59,7 +59,12 @@ addInteractionHandler('save test results to influx2', () => { "bucket": "testbucket", "precision": "ns" }, - body: regex("TestRun", /TestRun,Name=Default\\ suite,Status=PASS duration=2000,failed=0,passed=4,status=0,total=4 \d+\nTestSuite,Name=Default\\ test,Status=PASS duration=2000,failed=0,passed=4,status=0,total=4 \d+\nTestCase,Name=c2,Status=PASS duration=0,status=0 \d+\nTestCase,Name=c3,Status=PASS duration=10,status=0 \d+\nTestCase,Name=c1,Status=PASS duration=0,status=0 \d+\nTestCase,Name=c4,Status=PASS duration=0,status=0 \d+/) + body: "TestRun,Name=Default\\ suite,Status=PASS status=0,total=4,passed=4,failed=0,duration=2000\n"+ + "TestSuite,Name=Default\\ test,Status=PASS status=0,total=4,passed=4,failed=0,duration=2000\n"+ + "TestCase,Name=c2,Status=PASS status=0,duration=0\n"+ + "TestCase,Name=c3,Status=PASS status=0,duration=10\n"+ + "TestCase,Name=c1,Status=PASS status=0,duration=0\n"+ + "TestCase,Name=c4,Status=PASS status=0,duration=0" }, response: { status: 204 @@ -69,7 +74,6 @@ addInteractionHandler('save test results to influx2', () => { addInteractionHandler('save test results with custom tags and fields to influx2', () => { return { - strict: false, request: { method: 'POST', path: '/api/v2/write', @@ -81,7 +85,11 @@ addInteractionHandler('save test results with custom tags and fields to influx2' "bucket": "testbucket", "precision": "ns" }, - body: regex("TestRun", /TestRun,App=PactumJS,Name=Staging\\ -\\ UI\\ Smoke\\ Test\\ Run,Status=FAIL,Team=QA duration=1883597,failed=1,id=123,passed=1,status=1,stringfield="coolvalue",total=2 \d+\nTestSuite,App=PactumJS,Name=desktop-chrome,Status=PASS,Team=QA duration=1164451,failed=0,id=123,passed=1,status=0,stringfield="coolvalue",total=1 \d+\nTestCase,App=PactumJS,Name=GU,Status=PASS,Team=QA duration=243789,id=123,status=0,stringfield="coolvalue" \d+\nTestSuite,App=PactumJS,Name=mobile-andoid,Status=FAIL,Team=QA duration=714100,failed=1,id=123,passed=0,status=1,stringfield="coolvalue",total=1 \d+\nTestCase,App=PactumJS,Name=GU,Status=FAIL,Team=QA duration=156900,id=123,status=1,stringfield="coolvalue" \d+/) + body: "TestRun,Team=QA,App=PactumJS,Name=Staging\\ -\\ UI\\ Smoke\\ Test\\ Run,Status=FAIL id=123,stringfield=\"coolvalue\",status=1,total=2,passed=1,failed=1,duration=1883597\n" + + "TestSuite,Team=QA,App=PactumJS,Name=desktop-chrome,Status=PASS id=123,stringfield=\"coolvalue\",status=0,total=1,passed=1,failed=0,duration=1164451\n" + + "TestCase,Team=QA,App=PactumJS,Name=GU,Status=PASS id=123,stringfield=\"coolvalue\",status=0,duration=243789\n" + + "TestSuite,Team=QA,App=PactumJS,Name=mobile-andoid,Status=FAIL id=123,stringfield=\"coolvalue\",status=1,total=1,passed=0,failed=1,duration=714100\n" + + "TestCase,Team=QA,App=PactumJS,Name=GU,Status=FAIL id=123,stringfield=\"coolvalue\",status=1,duration=156900" }, response: { status: 204 diff --git a/test/targets.influx2.spec.js b/test/targets.influx2.spec.js index 58d9329..230aa98 100644 --- a/test/targets.influx2.spec.js +++ b/test/targets.influx2.spec.js @@ -14,12 +14,13 @@ describe('targets - influx2 - performance', () => { { "name": "influx2", "inputs": { - "url": "http://localhost:9393", + "host": "localhost", + "port": 9393, + "protocol": "http", "token": "testtoken", - "organization": "testorg", + "org": "testorg", "bucket": "testbucket", - "precision": "ns", - "gzipTransport": false // Turn off the compression, so we can match the write queries + "precision": "ns" } } ], @@ -48,9 +49,11 @@ describe('targets - influx2 - performance', () => { { "name": "influx2", "inputs": { - "url": "http://localhost:9393", + "host": "localhost", + "port": 9393, + "protocol": "http", "token": "testtoken", - "organization": "testorg", + "org": "testorg", "bucket": "testbucket", "precision": "ns", "tags": { @@ -59,8 +62,7 @@ describe('targets - influx2 - performance', () => { }, "fields": { "id": 123, - }, - "gzipTransport": false // Turn off the compression, so we can match the write queries + } } } ], @@ -98,12 +100,13 @@ describe('targets - influx2 - functional', () => { { "name": "influx2", "inputs": { - "url": "http://localhost:9393", + "host": "localhost", + "port": 9393, + "protocol": "http", "token": "testtoken", - "organization": "testorg", + "org": "testorg", "bucket": "testbucket", - "precision": "ns", - "gzipTransport": false // Turn off the compression, so we can match the write queries + "precision": "ns" } } ], @@ -132,9 +135,11 @@ describe('targets - influx2 - functional', () => { { "name": "influx2", "inputs": { - "url": "http://localhost:9393", + "host": "localhost", + "port": 9393, + "protocol": "http", "token": "testtoken", - "organization": "testorg", + "org": "testorg", "bucket": "testbucket", "precision": "ns", "tags": { @@ -144,8 +149,7 @@ describe('targets - influx2 - functional', () => { "fields": { "id": 123, "stringfield": "coolvalue" - }, - "gzipTransport": false // Turn off the compression, so we can match the write queries + } } } ],