Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: exposed installation method in debug mode #1268

Merged
merged 12 commits into from
Aug 28, 2024

Conversation

abhilash-sivan
Copy link
Contributor

@abhilash-sivan abhilash-sivan commented Aug 12, 2024

JIRA card: INSTA-778

  • test in local with --experimental-loader flag: node --experimental-loader ./node_modules/@instana/collector/esm-loader.mjs ./index-esm.mjs
  • test in local with --import flag: node --import ./node_modules/@instana/collector/esm-register.mjs ./index-esm.mjs
  • test in local with --require flag: node --require ./node_modules/@instana/collector/src/immediate ./index.js
  • test in local with no flags: node ./index.js

@kirrg001 kirrg001 changed the title feat: find out the method in which the client application has instrum… feat: exposed installation method in debug mode Aug 13, 2024
@abhilash-sivan
Copy link
Contributor Author

commit messge to main:

feat: exposed installation method in debug mode
refs INSTA-778

@abhilash-sivan
Copy link
Contributor Author

For the normal commonJS instrumentation using require('@instana/collector')(); in the code level currently considered as default and the debug log will print like

The app isnstrumented instana using: no additional flags

@abhilash-sivan abhilash-sivan marked this pull request as ready for review August 16, 2024 07:46
@abhilash-sivan abhilash-sivan requested a review from a team as a code owner August 16, 2024 07:46
@abhilash-sivan abhilash-sivan force-pushed the client-instrumentation-instana branch from 79e82dd to a754486 Compare August 16, 2024 07:46
@aryamohanan
Copy link
Contributor

commit messge to main:

feat: exposed installation method in debug mode
refs INSTA-778

For me, this isn't a feature but rather a fix, which is useful for troubleshooting. It doesn’t add any new functionality for the customer.

@@ -66,3 +66,31 @@ exports.isESMApp = function isESMApp() {

return isESM;
};

exports.tracerInstrumentationInfo = function tracerInstrumentationInfo() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only applicable for the collector package? what about the serverless packages?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now considered only the collector package.
The serverless whole is another scope.

If we need the serverless information from args and env, then it can also be added I guess.

Instead of

includes('@instana/collector')

can check for

esm-register.mjs

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we need the serverless information from args and env, then it can also be added I guess.

Instead of

includes('@instana/collector')

can check for

esm-register.mjs

I don't understand.

The serverless whole is another scope.

Ok, then we can do it later if needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of

includes('@instana/collector')

can check for

esm-register.mjs

this won't work btw

The serverless whole is another scope.

Yeah, I agree.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we need to check the flag for all our packages. Checking @instana/collector would only apply to the collector package. We already have a similar check in place for detecting ESM, as seen here. I'm thinking of either extending or referencing this function . This approach would help us avoid maintaining flags in multiple functions. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be a good idea. Initially, I designed the function like this but then later decided to check for a single package for checking serverless and collector specifically.
Re-using the existing function will be a good idea, even I thought of this. But since the outputs and checks differ in the function isESMApp() and function tracerInstrumentationInfo(), need to decide how can we extend this. I will get back to you after that. Please share if you have any better solutions in mind

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented a new logic!

@kirrg001 kirrg001 removed the WIP label Aug 16, 2024
@@ -66,3 +66,31 @@ exports.isESMApp = function isESMApp() {

return isESM;
};

exports.tracerInstrumentationInfo = function tracerInstrumentationInfo() {
const instrumentationMap = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic should not be here, this file specific to esm.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay will update.

@@ -197,6 +198,10 @@ exports.init = function init(_config, downstreamConnection, _processIdentityProv
automaticTracingEnabled = config.tracing.automaticTracingEnabled;

if (tracingEnabled) {
const loader = tracerInstrumentationInfo();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO: This should be called only in debug mode. Could you explore the options to do that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is an issue reported and we want to check the logs, then IMO it might be helpful to know the instrumentation details.
But for that, we can do console.info instead of console.debug!
Whats your take on that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO this should only be logged if debug mode is enabled.

noFlags: 'no additional flags'
};

const usingExperimentalLoaderFlag =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Qs: Why isn't there a check for @instana/collector included here? I noticed that you added checks for other flags.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching. That was not added in the checklist.
I will update this and add to checklist

@aryamohanan
Copy link
Contributor

I'm done with the review. Added one more comment.

Copy link
Contributor

@aryamohanan aryamohanan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

const method = getPackageInstallation();

// eslint-disable-next-line no-console
console.debug(`The App has instrumented instana using: ${method}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: I am getting

The App has instrumented instana using: no additional flags

When doing

$ INSTANA_DEBUG=true node --require ~/dev/instana/nodejs-2/packages/collector/src/immediate.js app.js

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am getting correctly!! but why🤔
Screenshot 2024-08-22 at 3 30 33 PM

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The path is different. Maybe we only cover specific use cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason is that you are referring to the packages directly.
The logic behind the comparison is checking for the @instana keyword in the arguments (as the clients are using our npm package).
So in a client app perspective it should work if they are including our package in the application start arguments

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could simplify the whole function by printing all values if --require, --import or --experimental-loader is used.

e.g. customer uses node --import X --import X app.js

Advantages:

  • less complicated logic, less checks
  • covers the use case that if a customer uses multiple import values to dig down potential problems -> conflicts
  • this is only debug information
  • covers more use cases (such as the case I showed)

The output would be:

console.debug(`The App is using the following preload flags: ${preloadFlags}`);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah perfect 👍

@@ -179,6 +180,13 @@ exports.preInit = function preInit(preliminaryConfig) {
* @param {CollectorPIDStore} _processIdentityProvider
*/
exports.init = function init(_config, downstreamConnection, _processIdentityProvider) {
if (process.env.INSTANA_DEBUG || process.env.INSTANA_LOG_LEVEL === 'debug') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this check a lot 👍

S: It would be cool if you could raise a second PR to add a utility for that. We use this check in many places e.g.

https://github.com/search?q=repo%3Ainstana%2Fnodejs%20%22if%20(process.env.INSTANA_DEBUG%20%7C%7C%20process.env.INSTANA_LOG_LEVEL%22&type=code

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah sure! 👍

const method = getPackageInstallation();

// eslint-disable-next-line no-console
console.debug(`The App has instrumented instana using: ${method}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could simplify the whole function by printing all values if --require, --import or --experimental-loader is used.

e.g. customer uses node --import X --import X app.js

Advantages:

  • less complicated logic, less checks
  • covers the use case that if a customer uses multiple import values to dig down potential problems -> conflicts
  • this is only debug information
  • covers more use cases (such as the case I showed)

The output would be:

console.debug(`The App is using the following preload flags: ${preloadFlags}`);

@abhilash-sivan abhilash-sivan force-pushed the client-instrumentation-instana branch from 6ea894a to b54a587 Compare August 23, 2024 06:15
@abhilash-sivan
Copy link
Contributor Author

commit message:

fix: exposed preload flags in debug mode
refs INSTA-778

process.env.NODE_OPTIONS?.includes('--require')
|| process.env.NODE_OPTIONS?.includes('--import')
|| process.env.NODE_OPTIONS?.includes('--experimental-loader')) {
return process.env.NODE_OPTIONS;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NODE_OPTIONS can contain multiple information..

Possible update:

const flags = ['--require', '--import', '--experimental-loader'];
  
  if (process.env.NODE_OPTIONS) {
    const relevantFlags = process.env.NODE_OPTIONS.split(' ').filter(option =>
      flags.some(flag => option.includes(flag))
    );
    if (relevantFlags.length > 0) {
      return relevantFlags.join(' ');
    }
  }
  
  if (process.execArgv.length > 0) {
    const result = process.execArgv.find(arg =>
      flags.some(flag => arg.includes(flag))
    );
    return result || 'noFlags';
  } else {
    return 'noFlags';
  }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NODE_OPTIONS can contain multiple information..

Thanks for sharing. I will update the same according to this suggestion 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since Array.prototype.some() function returns a boolean, we might have to make some changes in the returning string if the whole argument list is required in the debug logs.
But as you pointed out, the arguments and NODE_OPTIONS can contain more information that might not be relevant to us in the debug log.
Let me think of other options.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any solution works :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of printing out the whole, now logging only the flags (--import, --require, etc)
Also added a check for @instana keyword which will be sufficient to verify that one/some of instana packages are passed in the argument list or as NODE_OPTIONS

@abhilash-sivan abhilash-sivan changed the title feat: exposed installation method in debug mode fix: exposed installation method in debug mode Aug 27, 2024
@abhilash-sivan
Copy link
Contributor Author

LGTM!

@abhilash-sivan abhilash-sivan force-pushed the client-instrumentation-instana branch from aa82d63 to f523ef0 Compare August 28, 2024 04:16
@abhilash-sivan abhilash-sivan merged commit 775df15 into main Aug 28, 2024
@abhilash-sivan abhilash-sivan deleted the client-instrumentation-instana branch August 28, 2024 04:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants