Skip to content

Remove time_ns from API #1602

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

Merged
merged 33 commits into from
Mar 10, 2021
Merged

Remove time_ns from API #1602

merged 33 commits into from
Mar 10, 2021

Conversation

ocelotl
Copy link
Contributor

@ocelotl ocelotl commented Feb 12, 2021

This PR depends on #1599, I have marked this PR as a draft until #1599 gets merged to avoid trouble when reviewing.

Description

This removes time_ns from our API. We were providing this only to support Python versions older than 3.7 that did not include time_ns as a function of time. The problem with the current approach is that it provides a function that does not have the resolution that the new time_ns provides, it is misleading for our users to have a function named time_ns that does not provide actual nanosecond resolution. Having this function in our API is problematic, having a function that returns the time in nanoseconds is not part of the spec and makes us have to support it in future versions.

The solution here artificially adds a function named time_ns to time for Python versions older than 3.7. With this approach, our users can always do from time import time_ns and when we decide not to support Python 3.5 (which has reached end of life already) or Python 3.6 (which will reach end of life by the end of this year) then we can simply remove this solution. This will be completely transparent to our users because they will never import any time_ns function from an opentelemetry package.

Fixes #1594

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Test A

Does This PR Require a Contrib Repo Change?

Answer the following question based on these examples of changes that would require a Contrib Repo Change:

  • The OTel specification has changed which prompted this PR to update the method interfaces of opentelemetry-api/ or opentelemetry-sdk/

  • The method interfaces of opentelemetry-instrumentation/ have changed

  • The method interfaces of test/util have changed

  • Scripts in scripts/ that were copied over to the Contrib repo have changed

  • Configuration files that were copied over to the Contrib repo have changed (when consistency between repositories is applicable) such as in

    • pyproject.toml
    • isort.cfg
    • .flake8
  • When a new .github/CODEOWNER is added

  • Major changes to project information, such as in:

    • README.md
    • CONTRIBUTING.md
  • Yes. - Link to PR: Sync with Remove time_ns from API opentelemetry-python-contrib#342

  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@ocelotl ocelotl added the Skip Changelog PRs that do not require a CHANGELOG.md entry label Feb 12, 2021
@ocelotl ocelotl force-pushed the issue_1594 branch 5 times, most recently from 1de57e3 to f3a3278 Compare February 18, 2021 04:41
@ocelotl ocelotl changed the title Issue 1594 Remove time_ns from API Feb 18, 2021
@ocelotl ocelotl marked this pull request as ready for review February 18, 2021 05:35
@ocelotl ocelotl requested review from a team, toumorokoshi and hectorhdzg and removed request for a team February 18, 2021 05:35
@ocelotl
Copy link
Contributor Author

ocelotl commented Feb 18, 2021

Right now we do this:

  1. We provide a time_ns function. This is convenient for our users since they can use it to create timestamps regardless of the Python version they are using.
  2. This time_ns function is part of the API. This is inconvenient for us because it only exists to fulfill the needs of Python<3.7 users, forces us to keep it in the long term and it has nothing to do with OpenTelemetry.
  3. We do not tell our Python<3.6 users that the function they are using does not have the same resolution as the function in Python>3.7. This is inconvenient for our users because they can be mislead into thinking time_ns provides actual nanosecond resolution.

Now, this PR as it is right now "satisfies" the 3 points mentioned above:

  1. It still provides a time_ns function so that our users can have a function that creates timestamps regardless of the Python version that they are using.
  2. The time_ns function we provide in this PR is not part of the public API.
  3. We let our users know that the function they are using does not have the right resolution.

I believe this PR to be a bit controversial since it uses a kind of a "hack" to fix this problem, it uses a sitecustomize.py file to add time_ns to time for Python versions previous than 3.6. I understand that others may prefer a less hackyish solution.

Of the 3 points mentioned before, I care mostly about 2 and 3. In the past SIG it was suggested another possible solution, to not provide a time_ns function at all in the public API, instead make it private and use it in our packages while raising the warning for users of Python<3.7. I am also ok with this solution because while it does not "satisfy" point 1 it "satisfies" points 2 and 3 which I consider the most important and it is not a controversial solution that relies on a "hack".

@lzchen, @codeboten, @aabmass and anyone who may have an opinion, please leave it in the comments below. If more prefer the more traditional approach of just making time_ns private and raise a warning I can reimplement this PR to do so.

@owais
Copy link
Contributor

owais commented Feb 19, 2021

In the past SIG it was suggested another possible solution, to not provide a time_ns function at all in the public API, instead make it private and use it in our packages

This makes a lot of sense to me. We can just mark it as internal so we don't cover it under any API stability guarantees. I think this should be good enough for us to use the function internally. Anyone else deciding to use it would take on an intentional risk. Going out of the way with the sitecustomize trick isn't worth it IMHO.

Copy link
Member

@srikanthccv srikanthccv left a comment

Choose a reason for hiding this comment

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

+1 for making time_ns as private instead of patching the time module.

@lzchen lzchen mentioned this pull request Feb 19, 2021
11 tasks
@lzchen
Copy link
Contributor

lzchen commented Feb 19, 2021

In the past SIG it was suggested another possible solution, to not provide a time_ns function at all in the public API, instead make it private and use it in our packages

+1

Copy link
Member

@toumorokoshi toumorokoshi left a comment

Choose a reason for hiding this comment

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

+1 to the other reviewers concerns about patching in this fashion. Definitely seems easy to make this a private function.

@ocelotl ocelotl self-assigned this Feb 22, 2021
@ocelotl ocelotl force-pushed the issue_1594 branch 3 times, most recently from 2f13261 to 8e0c8ed Compare February 22, 2021 23:20
Copy link
Contributor

@codeboten codeboten left a comment

Choose a reason for hiding this comment

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

Just looking through the change, there's a lot of changes that don't appear to be directly tied to removing time_ns from the API, is it possible to refactor this PR to only that code?

@ocelotl ocelotl requested a review from toumorokoshi February 23, 2021 23:26
Copy link
Member

@toumorokoshi toumorokoshi left a comment

Choose a reason for hiding this comment

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

Thanks the changes look good, but I think the warning could be more succinct.

from logging import getLogger
from sys import version_info

if version_info.minor < 7:
Copy link
Member

Choose a reason for hiding this comment

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

IMO a safer way to do this is:

try:
    from time import time_ns
except ImportError:
   def _time_ns....

This avoids anything funky with backported functions and the like.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, how can this avoid anything funky with backported functions? I prefer the if approach because it is not-exceptional for our users to use 3.6 or 3.5 so it should not be treated as an exception.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, how can this avoid anything funky with backported functions?

In the past, people have backported python features back to older versions of python. It's common for them to use the same module name, or modify an existing module to include the functionality they backported.

There's no guarantee that the version of python doesn't have some version of the function backported. Hence I was saying that it's more common to just check to see if the import fails, and if it doesn't, then import the 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.

Ok, but we are not adding anything to any existing module now, I mean, we no longer try to patch time. _time_ns is always an attribute of opentelemetry.util regardless of the Python version used, only its implementation changes.

Copy link
Member

Choose a reason for hiding this comment

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

I'm more concerned about anyone patching the module. In the examples above, someone else could add time_ns, and if it exists, it would be nice to do so.

But this isn't a blocker comment, practically I don't think this will make much of a difference.

Copy link
Contributor

Choose a reason for hiding this comment

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

@toumorokoshi
Is this resolved?


if version_info.minor < 7:
getLogger(__name__).warning( # pylint: disable=logging-not-lazy
"You are using Python version 3.%s. This version does not include a "
Copy link
Member

Choose a reason for hiding this comment

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

I think this is more verbose that it needs to be. It's a good code comment for maintainers, but for users it suffices to say "in this version of python, opentelemetry only supports millisecond-level granularity".

Copy link
Member

Choose a reason for hiding this comment

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

In addition, why give the context that this private function will be removed once opentelemetry-api doesn't support python 3.7? I feel like that's somewhat obvious, and also unnecessary information to the user since 3.7+ supports NS granularity.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, you are right. I preferred to err on the safe side and be too verbose to make sure that our users never try using _time_ns. Refactoring this.


if version_info.minor < 7:
getLogger(__name__).warning( # pylint: disable=logging-not-lazy
"You are using Python version 3.%s. This version does not include a "
Copy link
Member

Choose a reason for hiding this comment

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

In addition, why give the context that this private function will be removed once opentelemetry-api doesn't support python 3.7? I feel like that's somewhat obvious, and also unnecessary information to the user since 3.7+ supports NS granularity.

@ocelotl ocelotl requested a review from codeboten March 9, 2021 17:00
@codeboten codeboten merged commit 5cc7ec5 into open-telemetry:main Mar 10, 2021
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.

No real nanosecond resolution provided by time_ns
6 participants