Skip to content

Commit

Permalink
fix: only startup plugin if invoked for runtime (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
sjvans authored Jun 17, 2024
1 parent c043632 commit c46bddb
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion cds-plugin.js
Original file line number Diff line number Diff line change
@@ -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')()
5 changes: 0 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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)",
Expand Down
1 change: 1 addition & 0 deletions test/bookshop/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"name": "bookshop",
"dependencies": {
"@cap-js/telemetry": "*",
"@cap-js/sqlite": "*",
Expand Down
53 changes: 53 additions & 0 deletions test/startup.test.js
Original file line number Diff line number Diff line change
@@ -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)
})
})

0 comments on commit c46bddb

Please sign in to comment.