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

chore: Fix lint warnings in instrumentation package #2404

Merged

Conversation

alisabzevari
Copy link
Contributor

@alisabzevari alisabzevari commented Aug 8, 2021

related to #1093,

Which problem is this PR solving?

  • This PR fixes lint warnings in instrumentation package.

Short description of the changes

  • Fix lint warnings
  • Note all warnings are resolved. The following warnings are still unresolved since they need better understanding about the code and its usage. I hope I can fix them later when I gain more knowledge about the project.
/home/asabzevari/projects/opentelemetry/opentelemetry-js/packages/opentelemetry-instrumentation/src/instrumentation.ts
  32:51  warning  Unexpected any. Specify a different type  @typescript-eslint/no-explicit-any

/home/asabzevari/projects/opentelemetry/opentelemetry-js/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts
  28:47  warning  Unexpected any. Specify a different type  @typescript-eslint/no-explicit-any

/home/asabzevari/projects/opentelemetry/opentelemetry-js/packages/opentelemetry-instrumentation/src/platform/node/instrumentationNodeModuleDefinition.ts
  30:39  warning  Unexpected any. Specify a different type  @typescript-eslint/no-explicit-any

@alisabzevari alisabzevari requested a review from a team as a code owner August 8, 2021 17:31
@codecov
Copy link

codecov bot commented Aug 8, 2021

Codecov Report

Merging #2404 (bddeafb) into main (e8e787d) will increase coverage by 0.01%.
The diff coverage is n/a.

@@            Coverage Diff             @@
##             main    #2404      +/-   ##
==========================================
+ Coverage   93.03%   93.04%   +0.01%     
==========================================
  Files         137      138       +1     
  Lines        5069     5093      +24     
  Branches     1089     1096       +7     
==========================================
+ Hits         4716     4739      +23     
- Misses        353      354       +1     
Impacted Files Coverage Δ
...entelemetry-instrumentation/src/autoLoaderUtils.ts 92.00% <0.00%> (ø)
...telemetry-sdk-trace-node/src/NodeTracerProvider.ts 95.83% <0.00%> (ø)

if (Array.isArray(option)) {
const results = parseInstrumentationOptions(option);
instrumentations = instrumentations.concat(results.instrumentations);
} else if (typeof option === 'function') {
} else if (isInstrumentationClass(option)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: this and line 39 seem like overkill, especially as isInstrumentationClass is just checking if option is a function and not specifically that is a class / constructor. Especially this this really just causes more code to be generated than necessary (coming from a code / browser support minification perspective)

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, and don't appear to be related to the listed linting issues.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Replacing this function with typeof option === 'function' results in the following compile error:

Cannot create an instance of an abstract class.ts(2511)

The nice thing about this type guard function is that it implicitly casts the option to the desired type as well.

The isInstrumentation function can be removed though. I just added it to improve the readability of the code (by my personal standards!).

Copy link
Contributor

Choose a reason for hiding this comment

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

That would be a side effect that the function is declaring that the object is of type option is new () => T (which could be added inline) as this gets compiled away.

So simplistically, you have a function that is kludging this. If you want a function I would suggest that we create this as a helper and call it something like isConstructor although that would still just check that it's a function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

... declaring that the object is of type option is new () => T (which could be added inline) ...

That's actually a good idea. Can you tell me how to do it? I cannot figure out the syntax to do it in the if condition.

Copy link
Member

Choose a reason for hiding this comment

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

It would require a change, but it could be done. The meta package could easily return a list of already new'd instrumentations. The constructor itself doesn't do any patching or have side effects.

Copy link
Contributor

Choose a reason for hiding this comment

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

it's not pretty but something like this should work, hence my suggestion of a generic isConstructor helper

} else if (typeof option === 'function') {
  instrumentations.push(new (option as unknown as (new () => InstrumentationOption))());
}

Copy link
Member

Choose a reason for hiding this comment

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

I prefer disabling the eslint warning instead of cast to unknown then to something else which only pretends to be typesafe. If the call to new fails, then the user will get an error during process start. We can't guard against every possible case.

Copy link
Member

Choose a reason for hiding this comment

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

What is the resolution here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems like the current solution is not preferrable. I will change it back to use any and disable the eslint for that line.

@alisabzevari alisabzevari changed the title Fix lint warnings in instrumentation package chore: Fix lint warnings in instrumentation package Aug 10, 2021
@@ -29,20 +30,28 @@ export function parseInstrumentationOptions(
): AutoLoaderResult {
let instrumentations: Instrumentation[] = [];
for (let i = 0, j = options.length; i < j; i++) {
const option = options[i] as any;
Copy link
Member

Choose a reason for hiding this comment

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

actually any is valid case here. This will be coming from a user so it can be written in ts or pure js, I would not change 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 any is valid here, then the InstrumentationOption type should be changed?
Or the function parameter type should be any[] or unknown[].

Copy link
Member

Choose a reason for hiding this comment

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

He is simply saying it is possible for the function to be called with bad inputs and saying you should guard against that. Unlike a real staticly typed language, we can always receive bad input. IMO failing during setup if bad inputs are passed is fine, but if something is easy to guard against I see no reason not to.

if (Array.isArray(option)) {
const results = parseInstrumentationOptions(option);
instrumentations = instrumentations.concat(results.instrumentations);
} else if (typeof option === 'function') {
} else if (isInstrumentationClass(option)) {
Copy link
Member

Choose a reason for hiding this comment

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

I still think we should only allow Instrumentation[] but that would need to be handled in a separate PR since it is breaking and quite a substantial change.

not really, the meta package will return array of instrumentations, and you still want to be able to do that,

[ getNodeAutoInstrumentations(), new MyOwnPackage()]

@dyladan dyladan self-requested a review August 10, 2021 20:19
@vmarchaud
Copy link
Member

cc @alisabzevari could you rebase your PR ? there are conflicts :/

@alisabzevari
Copy link
Contributor Author

Done @vmarchaud, please have a look.

@dyladan dyladan merged commit fdab642 into open-telemetry:main Nov 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants