diff --git a/CHANGELOG.md b/CHANGELOG.md index f55e437..7717825 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). The format is based on [Keep a Changelog](http://keepachangelog.com/). +## Version 0.2.3 - 2024-06-17 + +### Fixed + +- Only startup plugin if invoked for runtime (e.g., via cli `cds serve`) + ## Version 0.2.2 - 2024-06-03 ### Fixed diff --git a/cds-plugin.js b/cds-plugin.js index 50d9615..c8462f5 100644 --- a/cds-plugin.js +++ b/cds-plugin.js @@ -1 +1,12 @@ -if (!process.env.NO_TELEMETRY || process.env.NO_TELEMETRY === 'false') require('./lib')() +let _startup = true + +const cds = require('@sap/cds') +if (!(cds.cli?.command in { '': 1, serve: 1, run: 1 })) _startup = false + +// cds add XXX currently also has cli.command === '' +const i = process.argv.indexOf('add') +if (i > 1 && process.argv[i - 1].match(/cds(\.js)?$/)) _startup = false + +if (!!process.env.NO_TELEMETRY && process.env.NO_TELEMETRY !== 'false') _startup = false + +if (_startup) require('./lib')() diff --git a/lib/index.js b/lib/index.js index a23076e..53c99d3 100644 --- a/lib/index.js +++ b/lib/index.js @@ -7,11 +7,6 @@ const metrics = require('./metrics') const { getDiagLogLevel, getResource } = require('./utils') module.exports = function () { - // REVISIT: better - // no need to fire up plugin during build (avoid credentials validation) - const i = process.argv.indexOf('build') - if (i > 1 && process.argv[i - 1].match(/cds(\.js)?$/)) return - // set logger and propagate log level diag.setLogger(cds.log('telemetry'), process.env.OTEL_LOG_LEVEL || getDiagLogLevel()) diff --git a/package.json b/package.json index 74e4600..f4e1135 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@cap-js/telemetry", - "version": "0.2.2", + "version": "0.2.3", "description": "CDS plugin providing observability features, incl. automatic OpenTelemetry instrumentation.", "repository": "cap-js/telemetry", "author": "SAP SE (https://www.sap.com)", diff --git a/test/bookshop/package.json b/test/bookshop/package.json index 03159fb..28f1748 100644 --- a/test/bookshop/package.json +++ b/test/bookshop/package.json @@ -1,4 +1,5 @@ { + "name": "bookshop", "dependencies": { "@cap-js/telemetry": "*", "@cap-js/sqlite": "*", diff --git a/test/startup.test.js b/test/startup.test.js new file mode 100644 index 0000000..3f92868 --- /dev/null +++ b/test/startup.test.js @@ -0,0 +1,53 @@ +const childProcess = require('child_process') + +const startup = cmd => { + return new Promise(resolve => { + const p = childProcess.exec(cmd, { + env: Object.assign({}, process.env, { CDS_REQUIRES_TELEMETRY_KIND: 'to-cloud-logging' }), + cwd: __dirname + '/bookshop' + }) + p.on('exit', () => { + resolve(!p.exitCode ? false : true) + }) + p.stdout.on('data', data => { + if (data.match(/server listening on/)) p.kill() + }) + }) +} + +describe('plugin started', () => { + afterAll(() => { + require('fs').unlinkSync(__dirname + '/bookshop/mta.yaml') + require('fs').rmdirSync(__dirname + '/bookshop/gen', { recursive: true }) + }) + + test('not for NO_TELEMETRY=true', async () => { + const started = await startup('NO_TELEMETRY=true cds serve') + expect(started).toBe(false) + }) + + test('for NO_TELEMETRY=false', async () => { + const started = await startup('NO_TELEMETRY=false cds serve') + expect(started).toBe(true) + }) + + test('for cds serve', async () => { + const started = await startup('cds serve') + expect(started).toBe(true) + }) + + test('for cds run', async () => { + const started = await startup('cds run') + expect(started).toBe(true) + }) + + test('not for cds build', async () => { + const started = await startup('cds build --production') + expect(started).toBe(false) + }) + + test('not for cds add', async () => { + const started = await startup('cds add mta') + expect(started).toBe(false) + }) +})