-
Notifications
You must be signed in to change notification settings - Fork 656
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
Add convenience methods to access common config options in Configuration class #1426
Changes from 9 commits
04e1f98
c2e60b5
6ff672d
da0bcef
be19c3a
6870b01
d500384
dfcefee
64e5188
ebd8443
3918bd8
c958585
ce6449a
aaff2f4
bee4452
2a0671a
6f607cb
f5ccb50
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,14 +93,26 @@ | |
to override this value instead of changing it. | ||
""" | ||
|
||
import re | ||
from os import environ | ||
from re import fullmatch | ||
from typing import ClassVar, Dict, Optional, TypeVar, Union | ||
from typing import ClassVar, Dict, List, Optional, Sequence, TypeVar, Union | ||
|
||
ConfigValue = Union[str, bool, int, float] | ||
_T = TypeVar("_T", ConfigValue, Optional[ConfigValue]) | ||
|
||
|
||
class ExcludeList: | ||
"""Class to exclude certain paths (given as a list of regexes) from tracing requests""" | ||
|
||
def __init__(self, excluded_urls: Sequence[str]): | ||
self._non_empty = len(excluded_urls) > 0 | ||
if self._non_empty: | ||
self._regex = re.compile("|".join(excluded_urls)) | ||
|
||
def url_disabled(self, url: str) -> bool: | ||
return bool(self._non_empty and re.search(self._regex, url)) | ||
|
||
|
||
class Configuration: | ||
_instance = None # type: ClassVar[Optional[Configuration]] | ||
_config_map = {} # type: ClassVar[Dict[str, ConfigValue]] | ||
|
@@ -113,7 +125,7 @@ def __new__(cls) -> "Configuration": | |
instance = super().__new__(cls) | ||
for key, value_str in environ.items(): | ||
|
||
match = fullmatch(r"OTEL_(PYTHON_)?([A-Za-z_][\w_]*)", key) | ||
match = re.fullmatch(r"OTEL_(PYTHON_)?([A-Za-z_][\w_]*)", key) | ||
|
||
if match is not None: | ||
|
||
|
@@ -167,3 +179,20 @@ def _reset(cls) -> None: | |
if cls._instance: | ||
cls._instance._config_map.clear() # pylint: disable=protected-access | ||
cls._instance = None | ||
|
||
def traced_request_attrs(self, instrumentation: str) -> List[str]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Can you make these protected? I believe we are only using these internally. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah sure. I was thinking we would let users use this if they need. |
||
"""Returns list of traced request attributes for instrumentation.""" | ||
key = "{}_TRACED_REQUEST_ATTRS".format(instrumentation.upper()) | ||
value = self._config_map.get(key, "") | ||
|
||
request_attrs = ( | ||
[attr.strip() for attr in str.split(value, ",")] if value else [] # type: ignore | ||
srikanthccv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
return request_attrs | ||
|
||
def excluded_urls(self, instrumentation: str) -> ExcludeList: | ||
key = "{}_EXCLUDED_URLS".format(instrumentation.upper()) | ||
value = self._config_map.get(key, "") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if commas are allowed in URLs, but they can appear meaningfully in a regex like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comma is reserved character and is not typically used in URLs. It is normally used in query component for name value pairs. |
||
|
||
urls = str.split(value, ",") if value else [] # type: ignore | ||
return ExcludeList(urls) |
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.
Updated to show that tests pass, will revert once approved.
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.
As soon as your Contrib repo PR gets merged, you can't revert this. It either has to be
master
or this commit. I think leaving it as this commit is fine so that you don't have to change anything!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.
I thought the flow was supposed to be the reverse i.e, core repo gets merged first and then contrib.