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

Update typing for tracer.trace #2693

Closed
wants to merge 1 commit into from
Closed

Conversation

janeklb
Copy link
Contributor

@janeklb janeklb commented Jan 15, 2023

What does this PR do?

Corrects the typings of tracer.trace: span (and the done callback, if provided) will never be undefined if no options argument is passed.

Motivation

If options are not passed to trace (and therefore if options.orphanable !== false), then it is guaranteed that span (and done, if provided) will not be undefined.

@janeklb janeklb requested a review from a team as a code owner January 15, 2023 23:29
Comment on lines -84 to 88
trace<T> (name: string, fn: (span?: Span, fn?: (error?: Error) => any) => T): T;
trace<T> (name: string, fn: (span: Span) => T): T;
trace<T> (name: string, fn: (span: Span, done: (error?: Error) => string) => T): T;
trace<T> (name: string, options: TraceOptions & SpanOptions, fn: (span?: Span, done?: (error?: Error) => string) => T): T;
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 return type of the done callback is/was listed as any (with the no-options signature) and string (with the other signature), but looking at the code it seems to be a void -- should that be corrected too?

Copy link
Member

Choose a reason for hiding this comment

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

It's any since the return value can be whatever, and the return value of tracer.trace will be the same as what's returned by fn when provided.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

tracer.trace has a generic parameter <T> which matches the return type of fn; however, i'm referring to the done callback (which was - confusingly - also called fn in the current version of the code, though i have proposed to rename it).

Arrows pointing to the old and new callbacks
image

Copy link
Contributor Author

@janeklb janeklb Jan 16, 2023

Choose a reason for hiding this comment

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

note the return type of fn remains as T (as does the return type of trace)
image

@@ -81,7 +81,8 @@ export declare interface Tracer extends opentracing.Tracer {
* If the `orphanable` option is set to false, the function will not be traced
* unless there is already an active span or `childOf` option.
*/
trace<T> (name: string, fn: (span?: Span, fn?: (error?: Error) => any) => T): T;
trace<T> (name: string, fn: (span: Span) => T): T;
trace<T> (name: string, fn: (span: Span, done: (error?: Error) => string) => T): T;
Copy link
Member

Choose a reason for hiding this comment

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

It's a bit unclear what use case is improved by this change. Can you add a test case in docs/test.ts?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Interestingly enough, docs/test.ts already covers it here.. and typescript has errors in master

image

(whereas typescript doesn't complain on my patch-1 branch)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

im using VSCode with a bunch of extensions.. but i think the only relevant one here is TypeScript Hero -- can't see anything else that would contribute

Copy link
Contributor Author

@janeklb janeklb Jan 16, 2023

Choose a reason for hiding this comment

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

fwiw docs/test.ts has lots of other typescript errors -- at least according to my environment (TypeScript 4.9.4)

image

Copy link
Member

Choose a reason for hiding this comment

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

I see these errors as well in VS Code but not when running yarn type:test 🤔 Do you know what we're doing wrong that these aren't captured in the test? Ideally it would be fixed so that we don't end up with regressions for these cases.

Copy link
Contributor

Choose a reason for hiding this comment

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

Probably vscode isn't using the same base tsconfig or typescript version.

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 can confirm that if I switch VSCode's TS to the version defined in docs/package.json it still shows errors (in VSCode)

  • open a new VSCode window from dd-trace-js/docs
  • run yarn from that directory (ie. the VSCode terminal)
  • open test.ts - warnings still appear

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you know what we're doing wrong that these aren't captured in the test?

Unfortunately I do not know

@tlhunter
Copy link
Member

@janeklb is this a PR you're still interested in getting merged? Looks like there was still some open queries. If not I can close it out for now.

@janeklb
Copy link
Contributor Author

janeklb commented Dec 20, 2023

@janeklb is this a PR you're still interested in getting merged? Looks like there was still some open queries. If not I can close it out for now.

Yes, I would still like to have this merged. My understanding of the discussion threads was/is that they're open with datadog folks. Is there something I can do to help?

If options are not specified (and therefore if `options.orphanable !== false`) then `span` and the `done` callback are guaranteed to be set
Copy link

codecov bot commented Jan 2, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (e84c8e4) 84.70% compared to head (d9edad8) 84.55%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2693      +/-   ##
==========================================
- Coverage   84.70%   84.55%   -0.15%     
==========================================
  Files         237      235       -2     
  Lines       10147     9972     -175     
  Branches       33       33              
==========================================
- Hits         8595     8432     -163     
+ Misses       1552     1540      -12     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Qard Qard mentioned this pull request Jan 2, 2024
2 tasks
@Qard Qard closed this in #3912 Jan 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants