Skip to content

Commit

Permalink
fix(google-cloud-run): detect Node.js version, use matching @instana/…
Browse files Browse the repository at this point in the history
…google-cloud-run version
  • Loading branch information
basti1302 authored and kirrg001 committed Apr 4, 2022
1 parent 0d1f955 commit 451ad1f
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
FROM node:12.22.7-buster AS instana-google-cloud-run-build-nodejs

ARG legacy_package_version_range="<2.0.0"

# ---- @instana/[email protected] for Node.js < 10.x ----
WORKDIR /instana/legacy-1x
COPY package.json.npm ./
RUN sed -e s/SELF_VERSION/1.0.0/g \
-e s/INSTANA_GOOGLE_CLOUD_RUN_VERSION/$legacy_package_version_range/g \
package.json.npm > package.json
COPY .npmrc ./
RUN npm install --only=production
RUN rm -f instana-*.tgz && \
rm -f package.json && \
rm -f package.json.npm && \
rm -f .npmrc

# ---- @instana/google-cloud-run@latest for Node.js >= 10.x ----
WORKDIR /instana
COPY package.json ./
COPY instana-*.tgz ./
COPY .npmrc ./
COPY setup.sh ./

RUN npm install --only=production

RUN rm -f instana-*.tgz
RUN rm -f package.json
RUN rm -f .npmrc

# ---- Start over from scratch and copy npm modules
FROM scratch

COPY --from=instana-google-cloud-run-build-nodejs /instana /instana

Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,36 @@ FROM node:12.22.7-buster AS instana-google-cloud-run-build-nodejs

ARG package_version

ARG legacy_package_version_range="<2.0.0"

# ---- @instana/[email protected] for Node.js < 10.x ----
WORKDIR /instana/legacy-1x
COPY package.json.npm ./
RUN sed -e s/SELF_VERSION/$package_version/g \
-e s/INSTANA_GOOGLE_CLOUD_RUN_VERSION/$legacy_package_version_range/g \
package.json.npm > package.json
COPY .npmrc ./
RUN npm install --only=production
RUN rm -f instana-*.tgz && \
rm -f package.json && \
rm -f package.json.npm && \
rm -f .npmrc

# ---- @instana/google-cloud-run@latest for Node.js >= 10.x ----
WORKDIR /instana
COPY package.json.npm ./
RUN sed -e s/VERSION/$package_version/g package.json.npm > package.json
RUN sed -e s/SELF_VERSION/$package_version/g \
-e s/INSTANA_GOOGLE_CLOUD_RUN_VERSION/$package_version/g \
package.json.npm > package.json
COPY .npmrc ./
COPY setup.sh ./

RUN npm install --only=production

RUN rm -f instana-*.tgz && \
rm -f package.json && \
rm -f package.json.npm && \
rm -f .npmrc

# ---- Start over from scratch and copy npm modules
FROM scratch

COPY --from=instana-google-cloud-run-build-nodejs /instana /instana

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "instana-google-cloud-run-docker-image",
"version": "VERSION",
"version": "SELF_VERSION",
"description": "Provides @instana/google-cloud-run as an image for multi stage Dockerfiles",
"repository": {
"type": "git",
Expand All @@ -9,6 +9,6 @@
"author": "Bastian Krol <[email protected]>",
"license": "ISC",
"dependencies": {
"@instana/google-cloud-run": "VERSION"
"@instana/google-cloud-run": "INSTANA_GOOGLE_CLOUD_RUN_VERSION"
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
#######################################
# (c) Copyright IBM Corp. 2021
# (c) Copyright Instana Inc. and contributors 2020
#######################################
cd /instana
npm rebuild || echo "Warning: Rebuilding native add-ons for @instana/google-cloud-run failed. Monitoring the Cloud Run service revision instance will work nonetheless, but you will miss some Node.js metrics (GC metrics, event loop metrics, ...). See https://www.ibm.com/docs/de/obi/current?topic=agents-monitoring-google-cloud-run#build-dependencies-and-native-add-ons for details."
2 changes: 1 addition & 1 deletion packages/google-cloud-run/images/test-images/utils
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function normalizeArgs {
elif [[ $nodeJsVersion = 10 ]]; then
NODEJS_VERSION=10.20.1
elif [[ $nodeJsVersion = 8 ]]; then
NODEJS_VERSION=8.6.0
NODEJS_VERSION=8.17.0
else
NODEJS_VERSION=$nodeJsVersion
fi
Expand Down
60 changes: 36 additions & 24 deletions packages/google-cloud-run/src/index.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,44 @@
/*
* (c) Copyright IBM Corp. 2021
* (c) Copyright Instana Inc. and contributors 2020
* (c) Copyright IBM Corp. 2022
*/

'use strict';

const { isNodeJsTooOld, minimumNodeJsVersion } = require('@instana/core/src/util/nodeJsVersionCheck');
// MAINTENANCE NOTE: All code in this file needs to be compatible with all Node.js versions >= 6.0.0.

if (isNodeJsTooOld()) {
try {
const nodeJsVersion = process.version;
if (typeof nodeJsVersion !== 'string') {
return;
}
const matchResult = /v(\d+)\.(\d+)\.(\d+)/.exec(nodeJsVersion);
if (!matchResult || matchResult.length < 4) {
return;
}
const majorVersion = parseInt(matchResult[1], 10);
if (majorVersion == null || isNaN(majorVersion)) {
return;
}
if (majorVersion >= 10) {
module.exports = exports = require('./preactivate.js');
return;
} else if (majorVersion >= 6 && majorVersion < 10) {
// Maintenance note: As long as we do not backport the changes to index.js and the additional preactivate.js file to
// the 1.x branch, this needs to point to:
// /instana/legacy-1x/node_modules/@instana/google-cloud-run/src/index
// Because in 1.x, src/index either delegates to src/activate or src/noop.
//
// If we ever include those changes into the 1.x version (which we should not), we would need to point to:
// /instana/legacy-1x/node_modules/@instana/google-cloud-run/src/preactivate
// Otherwise we would get into an endless require loop.
//
// eslint-disable-next-line instana/no-unsafe-require
module.exports = exports = require('/instana/legacy-1x/node_modules/@instana/google-cloud-run/src/index');
return;
}
// Node.js versions < 6 are unsupported and will not be instrumented at all.
} catch (e) {
// ignore all runtime errors
// eslint-disable-next-line no-console
console.error(
`The package @instana/google-cloud-run requires at least Node.js ${minimumNodeJsVersion} but this process is ` +
`running on Node.js ${process.version}. This Google Cloud Run service will not be monitored by Instana.`
);
return;
}

const { util: coreUtil } = require('@instana/core');
const { environment: environmentUtil } = require('@instana/serverless');

const isExcludedFromInstrumentation = coreUtil.excludedFromInstrumentation && coreUtil.excludedFromInstrumentation();

if (!isExcludedFromInstrumentation) {
environmentUtil.validate();
}

if (!isExcludedFromInstrumentation && environmentUtil.isValid()) {
module.exports = exports = require('./activate');
} else {
module.exports = exports = require('./noop');
console.error(e);
}
32 changes: 32 additions & 0 deletions packages/google-cloud-run/src/preactivate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* (c) Copyright IBM Corp. 2021
* (c) Copyright Instana Inc. and contributors 2020
*/

'use strict';

const { isNodeJsTooOld, minimumNodeJsVersion } = require('@instana/core/src/util/nodeJsVersionCheck');

if (isNodeJsTooOld()) {
// eslint-disable-next-line no-console
console.error(
`The package @instana/google-cloud-run requires at least Node.js ${minimumNodeJsVersion} but this process is ` +
`running on Node.js ${process.version}. This Google Cloud Run service will not be monitored by Instana.`
);
return;
}

const { util: coreUtil } = require('@instana/core');
const { environment: environmentUtil } = require('@instana/serverless');

const isExcludedFromInstrumentation = coreUtil.excludedFromInstrumentation && coreUtil.excludedFromInstrumentation();

if (!isExcludedFromInstrumentation) {
environmentUtil.validate();
}

if (!isExcludedFromInstrumentation && environmentUtil.isValid()) {
module.exports = exports = require('./activate');
} else {
module.exports = exports = require('./noop');
}

0 comments on commit 451ad1f

Please sign in to comment.