From 0cff389f87d23c4c0da171086d0db5f3ea582196 Mon Sep 17 00:00:00 2001 From: Hamid Date: Wed, 16 Jan 2019 10:49:32 +0100 Subject: [PATCH] feat: add OpenTracing support (#138) * feat: add OpenTracing support add a separate bundle for opentracing minor fixes * add opentracing-entry bundle opentracing seperately but including the apm-base * update js-core to 2.4 --- .eslintrc.js | 4 +- package-lock.json | 12 +++-- package.json | 6 +-- src/opentracing-entry.js | 4 ++ src/opentracing/index.js | 15 +++++++ test/e2e/general-usecase/app.e2e-spec.js | 4 +- test/e2e/general-usecase/app.js | 7 +++ test/e2e/standalone-html/app.e2e-spec.js | 17 ++++++++ test/e2e/standalone-html/index.html | 4 +- test/e2e/standalone-html/opentracing.html | 53 +++++++++++++++++++++++ test/specs/apm-base.spec.js | 2 +- webpack.config.js | 11 +++-- 12 files changed, 124 insertions(+), 15 deletions(-) create mode 100644 src/opentracing-entry.js create mode 100644 src/opentracing/index.js create mode 100644 test/e2e/standalone-html/opentracing.html diff --git a/.eslintrc.js b/.eslintrc.js index 724f710f8..c2f325457 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -3,6 +3,8 @@ module.exports = { "es6": true }, "rules": { - "max-len": ["error", { "code": 100 }] + "max-len": ["error", { "code": 100 }], + 'no-unused-vars': 'error', + 'space-before-function-paren': 'error' } } \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index a37f3d8e2..4f42ded75 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3688,11 +3688,12 @@ "dev": true }, "elastic-apm-js-core": { - "version": "2.3.0", - "resolved": "https://registry.npmjs.org/elastic-apm-js-core/-/elastic-apm-js-core-2.3.0.tgz", - "integrity": "sha512-u+/WpM3nR1aN7nW0QDyhRC5YNLQDAJV6EMofhJNFabKpTo+yFRtfAngSrSRaN/v5F5dkhQwPIa7Ca47r0OGWoA==", + "version": "2.4.0", + "resolved": "https://registry.npmjs.org/elastic-apm-js-core/-/elastic-apm-js-core-2.4.0.tgz", + "integrity": "sha512-wW3aEMp0eVI5iJVuUbg767J2D+hQKnMIo2yhxdgjz69GO0/zEB9VETGD9+fz14x9+O/0RkfxbfmhiAL12EEOdQ==", "requires": { "error-stack-parser": "^1.3.5", + "opentracing": "^0.14.3", "stack-generator": "^1.0.7", "url-parse": "^1.4.3", "uuid": "^3.1.0" @@ -8143,6 +8144,11 @@ "integrity": "sha1-XG2ixdflgx6P+jlklQ+NZnSskLg=", "dev": true }, + "opentracing": { + "version": "0.14.3", + "resolved": "https://registry.npmjs.org/opentracing/-/opentracing-0.14.3.tgz", + "integrity": "sha1-I+OtAp+mamU5Jq2+V+g0Rp+FUKo=" + }, "optimist": { "version": "0.6.1", "resolved": "https://registry.npmjs.org/optimist/-/optimist-0.6.1.tgz", diff --git a/package.json b/package.json index 538e5016a..b5a9975d4 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "run-script": "node ./scripts/run-script.js", "test": "npm run lint && npm run build && npm run buildE2eBundles && npm run runNodeTests && npm run runUnitTests && npm run run-e2e && npm run build-docs", "lint": "standard", - "format": "prettier-standard src/**/*.js test/**/*.js" + "format": "prettier-standard 'src/**/*.js' test/**/*.js" }, "files": [ "src/**/*.js", @@ -77,7 +77,7 @@ "webpack-cli": "^3.1.2" }, "dependencies": { - "elastic-apm-js-core": "^2.3.0", + "elastic-apm-js-core": "^2.4.0", "es6-promise": "^4.2.4" }, "standard": { @@ -92,4 +92,4 @@ "test/e2e/sourcemap/" ] } -} \ No newline at end of file +} diff --git a/src/opentracing-entry.js b/src/opentracing-entry.js new file mode 100644 index 000000000..12dca91e6 --- /dev/null +++ b/src/opentracing-entry.js @@ -0,0 +1,4 @@ +const indexExports = require('./index') +const opentracing = require('./opentracing') +const { extend } = require('elastic-apm-js-core/src/common/utils') +module.exports = extend({}, indexExports, opentracing) diff --git a/src/opentracing/index.js b/src/opentracing/index.js new file mode 100644 index 000000000..a40e36cbd --- /dev/null +++ b/src/opentracing/index.js @@ -0,0 +1,15 @@ +const { createTracer: createElasticTracer } = require('elastic-apm-js-core/src/opentracing') + +function createTracer (apmBase) { + return createElasticTracer(apmBase.serviceFactory) +} + +if (window && window.elasticApm) { + window.elasticApm.createTracer = createTracer.bind(window.elasticApm, window.elasticApm) +} + +module.exports = { + __esModule: true, + default: createTracer, + createTracer +} diff --git a/test/e2e/general-usecase/app.e2e-spec.js b/test/e2e/general-usecase/app.e2e-spec.js index f141f692c..567c59e43 100644 --- a/test/e2e/general-usecase/app.e2e-spec.js +++ b/test/e2e/general-usecase/app.e2e-spec.js @@ -74,9 +74,9 @@ describe('general-usercase', function () { var transactionPayload = serverCalls.sendTransactions[0].args[0][0] expect(transactionPayload.type).toBe('page-load') expect(transactionPayload.name).toBe('general-usecase-initial-page-load') - expect(transactionPayload.spans.length).toBeGreaterThan(3) + expect(transactionPayload.spans.length).toBeGreaterThan(2) var span = transactionPayload.spans.find(function (s) { - return s.name === 'GET /test/e2e/common/data.json?test=hamid' + return s.name === 'GET /test/e2e/common/data.json' }) expect(span).toBeDefined() diff --git a/test/e2e/general-usecase/app.js b/test/e2e/general-usecase/app.js index ea4db7944..f9fe94fb9 100644 --- a/test/e2e/general-usecase/app.js +++ b/test/e2e/general-usecase/app.js @@ -14,6 +14,13 @@ var elasticApm = createApmBase({ pageLoadSampled: true }) +const { createTracer } = require('../../../src/opentracing') +var tracer = createTracer(elasticApm) + +var otSpan = tracer.startSpan('OpenTracing span') + +otSpan.finish(Date.now() + 200) + elasticApm.setInitialPageLoadName('general-usecase-initial-page-load') elasticApm.setUserContext({ diff --git a/test/e2e/standalone-html/app.e2e-spec.js b/test/e2e/standalone-html/app.e2e-spec.js index 7004a48f1..9a678d7d0 100644 --- a/test/e2e/standalone-html/app.e2e-spec.js +++ b/test/e2e/standalone-html/app.e2e-spec.js @@ -18,4 +18,21 @@ describe('standalone-html', function () { return utils.allowSomeBrowserErrors(['timeout test error with a secret']) } }) + + it('should run the opentracing use-case', function () { + browser.timeouts('script', 30000) + browser.url('/test/e2e/standalone-html/opentracing.html') + + browser.waitUntil( + function () { + return browser.getText('#test-element') === 'Passed' + }, + 10000, + 'expected element #test-element' + ) + + if (isChrome()) { + return utils.allowSomeBrowserErrors(['timeout test error with a secret']) + } + }) }) diff --git a/test/e2e/standalone-html/index.html b/test/e2e/standalone-html/index.html index e6e642e97..8b0cbac1d 100644 --- a/test/e2e/standalone-html/index.html +++ b/test/e2e/standalone-html/index.html @@ -12,10 +12,12 @@ + + + diff --git a/test/specs/apm-base.spec.js b/test/specs/apm-base.spec.js index e88b3f378..83f048d01 100644 --- a/test/specs/apm-base.spec.js +++ b/test/specs/apm-base.spec.js @@ -57,7 +57,7 @@ describe('ApmBase', function () { spyOn(tr, 'startSpan').and.callThrough() apmBase.startSpan('test-span', 'test-type') - expect(tr.startSpan).toHaveBeenCalledWith('test-span', 'test-type') + expect(tr.startSpan).toHaveBeenCalledWith('test-span', 'test-type', undefined) expect(apmBase.getCurrentTransaction()).toBe(tr) expect(apmBase.getTransactionService()).toBe(trService) diff --git a/webpack.config.js b/webpack.config.js index b9f0e4921..194246b4f 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -6,11 +6,14 @@ const UglifyJSPlugin = require('uglifyjs-webpack-plugin') const OUTPUT_DIR = path.join(__dirname, 'dist', 'bundles') const baseConfig = { - entry: './src/index.js', + entry: { + 'elastic-apm-js-base': './src/index.js', + 'elastic-apm-opentracing': './src/opentracing-entry.js' + }, output: { - filename: 'elastic-apm-js-base.umd.js', + filename: '[name].umd.js', path: OUTPUT_DIR, - library: 'elastic-apm-js-base', + library: '[name]', libraryTarget: 'umd' }, mode: 'development', @@ -40,7 +43,7 @@ const baseConfig = { const optimizeConfig = Object.assign({}, baseConfig, { output: { - filename: 'elastic-apm-js-base.umd.min.js', + filename: '[name].umd.min.js', path: OUTPUT_DIR }, mode: 'production',