-
Notifications
You must be signed in to change notification settings - Fork 805
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
feat!(instrumentation): remove moduleExports generic type from instrumentation registration #4598
feat!(instrumentation): remove moduleExports generic type from instrumentation registration #4598
Conversation
…mentation registration
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## main #4598 +/- ##
==========================================
+ Coverage 90.77% 92.85% +2.07%
==========================================
Files 90 328 +238
Lines 1930 9499 +7569
Branches 417 2042 +1625
==========================================
+ Hits 1752 8820 +7068
- Misses 178 679 +501
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few comments, this generally this looks good. 👍
I think this plays well into #4586.
FYI @open-telemetry/javascript-approvers, this is a quite substantial change with a large impact on instrumentation authors. Please put your objections on the PR if you have any so that we can discuss further
experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts
Outdated
Show resolved
Hide resolved
experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts
Outdated
Show resolved
Hide resolved
experimental/packages/opentelemetry-instrumentation/tsconfig.esm.json
Outdated
Show resolved
Hide resolved
…rm/node/instrumentation.ts Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>
…rm/node/instrumentation.ts Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>
Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>
Thank you @pichlermarc for the review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for working on this! I left a few suggestions around eslint warnings to add and remove, but those are nits and the general changes here look good.
experimental/packages/opentelemetry-instrumentation/src/instrumentationNodeModuleDefinition.ts
Outdated
Show resolved
Hide resolved
experimental/packages/opentelemetry-instrumentation/src/instrumentationNodeModuleFile.ts
Outdated
Show resolved
Hide resolved
name, | ||
baseDir | ||
); | ||
return this._onRequire<typeof exports>(module, exports, name, baseDir); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This now looks so much cleaner and easier to reason about!
Co-authored-by: Jamie Danielson <jamieedanielson@gmail.com>
…mentationNodeModuleFile.ts Co-authored-by: Jamie Danielson <jamieedanielson@gmail.com>
…mentationNodeModuleDefinition.ts Co-authored-by: Jamie Danielson <jamieedanielson@gmail.com>
Great suggestions, I need to be more aware of the eslint directives. Thank you for adding them |
…mentation registration (open-telemetry#4598) * feat!(instrumentation): remove moudleExports generic type from instrumentation registration * fix: lint * chore: add changelog * fix: core instrumentations * docs: update README with the change * Update experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com> * Update experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com> * Update CHANGELOG.md Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com> * chore: lint * revert: sdk-logs in tsconfig * chore: lint markdown * Apply suggestions from code review Co-authored-by: Jamie Danielson <jamieedanielson@gmail.com> * Update experimental/packages/opentelemetry-instrumentation/src/instrumentationNodeModuleFile.ts Co-authored-by: Jamie Danielson <jamieedanielson@gmail.com> * Update experimental/packages/opentelemetry-instrumentation/src/instrumentationNodeModuleDefinition.ts Co-authored-by: Jamie Danielson <jamieedanielson@gmail.com> * fix: remove unrelevant eslint ignore --------- Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com> Co-authored-by: Jamie Danielson <jamieedanielson@gmail.com>
This PR removes the generic type on
InstrumentationBase
which determines themoduleExports
types for patching and unpatching.Adding types for function parameters, variables and return values is considered idiomatic and good practice in typescript. Thus it was added to the OpenTelemetry
InstrumentationBase
class in the first place - to promote type safety. However, while it has benefits, there are also some downsides which we observed over the last years:Public Package API
the generic parameter can leak private types to the public api.
A typical instrumentation that uses this generic looks like this:
After typescript transpilation, this public init function remains in the
index.d.ts
transitive import, which means that any typescript user of the instrumentation must satisfyimport type { FooType } from 'foo';
and have the types installed as part of the dependencies.This has proven to be problematic as:
foo
in the example) is found in the user application, as it is a valid use case to install instrumentations for packages which are not a dependency (as happens, for example, with the auto-instrumentation package).@types
available (e.g.@types/foo
in our example), the types package is not always well maintained, compatible, or stable.To overcome the above issues, it is common for instrumentations to drop the type checking services for
moduleExports
and useany
as the genric parameter.Few statistics:
any
22 instrumentations - (amqplib
,cucumber
,dataloader
,lru-memoizer
,mongoose
,socket.io
,aws-lambda
,aws-sdk
,cassandra
,fastify
,generic-pool
,graphql
,ioredis
,knex
,mongodb
,mysql2
,nestjs
,pino
,redis
,restify
,router
,winston
)@types/
dependency 10 instrumentations - (fs
,tedious
,bunyan
,connect
,express
,hapi
,koa
,memcached
,mysql
,pg
)dns
,net
).This change will affect 12 existing instrumentations which will need to be updated.
Type Safety
Instrumentation can still cast the parameters in the
InstrumentationModuleDefinition
patch
function to promote type safety.For example:
The patch signature is now:
public patch?: (exports: any, moduleVersion?: string) => any,
and implementations can use the right types ((exports: Https, moduleVersion?: string) Https
) instead ofany
to use the correct type. The downside is that it is not automatically enforced and is more "OPT-IN". an instrumentation implementer would need to explicitly state the types instead of using the generic type.Breaking Change
This change would require a small change in each existing instrumentation - removing the generic parameter and optionally adding type to the
patch
andunpatch
functions ofInstrumentationModuleDefinition
andInstrumentationModuleFile
. Since the package is experimental, it's ok to make such changes and publish them as a minor version bump.Misc
Pros:
Cons:
patch
andunpatch
functions, the user would need to specifically add the type to the function signature.