chore: 添加虚拟环境到仓库
- 添加 backend_service/venv 虚拟环境 - 包含所有Python依赖包 - 注意:虚拟环境约393MB,包含12655个文件
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,85 @@
|
||||
# Copyright The OpenTelemetry Authors
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import asyncio
|
||||
import contextlib
|
||||
import functools
|
||||
from typing import TYPE_CHECKING, Callable, Generic, Iterator, TypeVar
|
||||
|
||||
V = TypeVar("V")
|
||||
R = TypeVar("R") # Return type
|
||||
Pargs = TypeVar("Pargs") # Generic type for arguments
|
||||
Pkwargs = TypeVar("Pkwargs") # Generic type for arguments
|
||||
|
||||
# We don't actually depend on typing_extensions but we can use it in CI with this conditional
|
||||
# import. ParamSpec can be imported directly from typing after python 3.9 is dropped
|
||||
# https://peps.python.org/pep-0612/.
|
||||
if TYPE_CHECKING:
|
||||
from typing_extensions import ParamSpec
|
||||
|
||||
P = ParamSpec("P") # Generic type for all arguments
|
||||
|
||||
|
||||
class _AgnosticContextManager(
|
||||
contextlib._GeneratorContextManager[R],
|
||||
Generic[R],
|
||||
): # pylint: disable=protected-access
|
||||
"""Context manager that can decorate both async and sync functions.
|
||||
|
||||
This is an overridden version of the contextlib._GeneratorContextManager
|
||||
class that will decorate async functions with an async context manager
|
||||
to end the span AFTER the entire async function coroutine finishes.
|
||||
|
||||
Else it will report near zero spans durations for async functions.
|
||||
|
||||
We are overriding the contextlib._GeneratorContextManager class as
|
||||
reimplementing it is a lot of code to maintain and this class (even if it's
|
||||
marked as protected) doesn't seems like to be evolving a lot.
|
||||
|
||||
For more information, see:
|
||||
https://github.com/open-telemetry/opentelemetry-python/pull/3633
|
||||
"""
|
||||
|
||||
def __enter__(self) -> R:
|
||||
"""Reimplementing __enter__ to avoid the type error.
|
||||
|
||||
The original __enter__ method returns Any type, but we want to return R.
|
||||
"""
|
||||
del self.args, self.kwds, self.func # type: ignore
|
||||
try:
|
||||
return next(self.gen) # type: ignore
|
||||
except StopIteration:
|
||||
raise RuntimeError("generator didn't yield") from None
|
||||
|
||||
def __call__(self, func: V) -> V: # pyright: ignore [reportIncompatibleMethodOverride]
|
||||
if asyncio.iscoroutinefunction(func):
|
||||
|
||||
@functools.wraps(func) # type: ignore
|
||||
async def async_wrapper(*args: Pargs, **kwargs: Pkwargs) -> R: # pyright: ignore [reportInvalidTypeVarUse]
|
||||
with self._recreate_cm(): # type: ignore
|
||||
return await func(*args, **kwargs) # type: ignore
|
||||
|
||||
return async_wrapper # type: ignore
|
||||
return super().__call__(func) # type: ignore
|
||||
|
||||
|
||||
def _agnosticcontextmanager(
|
||||
func: "Callable[P, Iterator[R]]",
|
||||
) -> "Callable[P, _AgnosticContextManager[R]]":
|
||||
@functools.wraps(func)
|
||||
def helper(*args: Pargs, **kwargs: Pkwargs) -> _AgnosticContextManager[R]: # pyright: ignore [reportInvalidTypeVarUse]
|
||||
return _AgnosticContextManager(func, args, kwargs) # pyright: ignore [reportArgumentType]
|
||||
|
||||
# Ignoring the type to keep the original signature of the function
|
||||
return helper # type: ignore[return-value]
|
||||
@@ -0,0 +1,55 @@
|
||||
# Copyright The OpenTelemetry Authors
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from functools import cache
|
||||
|
||||
# FIXME: Use importlib.metadata (not importlib_metadata)
|
||||
# when support for 3.11 is dropped if the rest of
|
||||
# the supported versions at that time have the same API.
|
||||
from importlib_metadata import ( # type: ignore
|
||||
Distribution,
|
||||
EntryPoint,
|
||||
EntryPoints,
|
||||
PackageNotFoundError,
|
||||
distributions,
|
||||
requires,
|
||||
version,
|
||||
)
|
||||
from importlib_metadata import (
|
||||
entry_points as original_entry_points,
|
||||
)
|
||||
|
||||
|
||||
@cache
|
||||
def _original_entry_points_cached():
|
||||
return original_entry_points()
|
||||
|
||||
|
||||
def entry_points(**params):
|
||||
"""Replacement for importlib_metadata.entry_points that caches getting all the entry points.
|
||||
|
||||
That part can be very slow, and OTel uses this function many times."""
|
||||
return _original_entry_points_cached().select(**params)
|
||||
|
||||
|
||||
__all__ = [
|
||||
"entry_points",
|
||||
"version",
|
||||
"EntryPoint",
|
||||
"EntryPoints",
|
||||
"requires",
|
||||
"Distribution",
|
||||
"distributions",
|
||||
"PackageNotFoundError",
|
||||
]
|
||||
@@ -0,0 +1,47 @@
|
||||
# Copyright The OpenTelemetry Authors
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from threading import Lock
|
||||
from typing import Callable
|
||||
|
||||
|
||||
class Once:
|
||||
"""Execute a function exactly once and block all callers until the function returns
|
||||
|
||||
Same as golang's `sync.Once <https://pkg.go.dev/sync#Once>`_
|
||||
"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
self._lock = Lock()
|
||||
self._done = False
|
||||
|
||||
def do_once(self, func: Callable[[], None]) -> bool:
|
||||
"""Execute ``func`` if it hasn't been executed or return.
|
||||
|
||||
Will block until ``func`` has been called by one thread.
|
||||
|
||||
Returns:
|
||||
Whether or not ``func`` was executed in this call
|
||||
"""
|
||||
|
||||
# fast path, try to avoid locking
|
||||
if self._done:
|
||||
return False
|
||||
|
||||
with self._lock:
|
||||
if not self._done:
|
||||
func()
|
||||
self._done = True
|
||||
return True
|
||||
return False
|
||||
@@ -0,0 +1,52 @@
|
||||
# Copyright The OpenTelemetry Authors
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from logging import getLogger
|
||||
from os import environ
|
||||
from typing import TYPE_CHECKING, TypeVar, cast
|
||||
|
||||
from opentelemetry.util._importlib_metadata import entry_points
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from opentelemetry.metrics import MeterProvider
|
||||
from opentelemetry.trace import TracerProvider
|
||||
|
||||
Provider = TypeVar("Provider", "TracerProvider", "MeterProvider")
|
||||
|
||||
logger = getLogger(__name__)
|
||||
|
||||
|
||||
def _load_provider(
|
||||
provider_environment_variable: str, provider: str
|
||||
) -> Provider: # type: ignore[type-var]
|
||||
try:
|
||||
provider_name = cast(
|
||||
str,
|
||||
environ.get(provider_environment_variable, f"default_{provider}"),
|
||||
)
|
||||
|
||||
return cast(
|
||||
Provider,
|
||||
next( # type: ignore
|
||||
iter( # type: ignore
|
||||
entry_points( # type: ignore
|
||||
group=f"opentelemetry_{provider}",
|
||||
name=provider_name,
|
||||
)
|
||||
)
|
||||
).load()(),
|
||||
)
|
||||
except Exception: # pylint: disable=broad-exception-caught
|
||||
logger.exception("Failed to load configured provider %s", provider)
|
||||
raise
|
||||
@@ -0,0 +1,116 @@
|
||||
# Copyright The OpenTelemetry Authors
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from logging import getLogger
|
||||
from re import compile, split
|
||||
from typing import Dict, List, Mapping
|
||||
from urllib.parse import unquote
|
||||
|
||||
from typing_extensions import deprecated
|
||||
|
||||
_logger = getLogger(__name__)
|
||||
|
||||
# The following regexes reference this spec: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md#specifying-headers-via-environment-variables
|
||||
|
||||
# Optional whitespace
|
||||
_OWS = r"[ \t]*"
|
||||
# A key contains printable US-ASCII characters except: SP and "(),/:;<=>?@[\]{}
|
||||
_KEY_FORMAT = (
|
||||
r"[\x21\x23-\x27\x2a\x2b\x2d\x2e\x30-\x39\x41-\x5a\x5e-\x7a\x7c\x7e]+"
|
||||
)
|
||||
# A value contains a URL-encoded UTF-8 string. The encoded form can contain any
|
||||
# printable US-ASCII characters (0x20-0x7f) other than SP, DEL, and ",;/
|
||||
_VALUE_FORMAT = r"[\x21\x23-\x2b\x2d-\x3a\x3c-\x5b\x5d-\x7e]*"
|
||||
# Like above with SP included
|
||||
_LIBERAL_VALUE_FORMAT = r"[\x20\x21\x23-\x2b\x2d-\x3a\x3c-\x5b\x5d-\x7e]*"
|
||||
# A key-value is key=value, with optional whitespace surrounding key and value
|
||||
_KEY_VALUE_FORMAT = rf"{_OWS}{_KEY_FORMAT}{_OWS}={_OWS}{_VALUE_FORMAT}{_OWS}"
|
||||
|
||||
_HEADER_PATTERN = compile(_KEY_VALUE_FORMAT)
|
||||
_LIBERAL_HEADER_PATTERN = compile(
|
||||
rf"{_OWS}{_KEY_FORMAT}{_OWS}={_OWS}{_LIBERAL_VALUE_FORMAT}{_OWS}"
|
||||
)
|
||||
_DELIMITER_PATTERN = compile(r"[ \t]*,[ \t]*")
|
||||
|
||||
_BAGGAGE_PROPERTY_FORMAT = rf"{_KEY_VALUE_FORMAT}|{_OWS}{_KEY_FORMAT}{_OWS}"
|
||||
|
||||
_INVALID_HEADER_ERROR_MESSAGE_STRICT_TEMPLATE = (
|
||||
"Header format invalid! Header values in environment variables must be "
|
||||
"URL encoded per the OpenTelemetry Protocol Exporter specification: %s"
|
||||
)
|
||||
|
||||
_INVALID_HEADER_ERROR_MESSAGE_LIBERAL_TEMPLATE = (
|
||||
"Header format invalid! Header values in environment variables must be "
|
||||
"URL encoded per the OpenTelemetry Protocol Exporter specification or "
|
||||
"a comma separated list of name=value occurrences: %s"
|
||||
)
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
|
||||
|
||||
@deprecated(
|
||||
"You should use parse_env_headers. Deprecated since version 1.15.0."
|
||||
)
|
||||
def parse_headers(s: str) -> Mapping[str, str]:
|
||||
return parse_env_headers(s)
|
||||
|
||||
|
||||
def parse_env_headers(s: str, liberal: bool = False) -> Mapping[str, str]:
|
||||
"""
|
||||
Parse ``s``, which is a ``str`` instance containing HTTP headers encoded
|
||||
for use in ENV variables per the W3C Baggage HTTP header format at
|
||||
https://www.w3.org/TR/baggage/#baggage-http-header-format, except that
|
||||
additional semi-colon delimited metadata is not supported.
|
||||
If ``liberal`` is True we try to parse ``s`` anyway to be more compatible
|
||||
with other languages SDKs that accept non URL-encoded headers by default.
|
||||
"""
|
||||
headers: Dict[str, str] = {}
|
||||
headers_list: List[str] = split(_DELIMITER_PATTERN, s)
|
||||
for header in headers_list:
|
||||
if not header: # empty string
|
||||
continue
|
||||
header_match = _HEADER_PATTERN.fullmatch(header.strip())
|
||||
if not header_match and not liberal:
|
||||
_logger.warning(
|
||||
_INVALID_HEADER_ERROR_MESSAGE_STRICT_TEMPLATE, header
|
||||
)
|
||||
continue
|
||||
|
||||
if header_match:
|
||||
match_string: str = header_match.string
|
||||
# value may contain any number of `=`
|
||||
name, value = match_string.split("=", 1)
|
||||
name = unquote(name).strip().lower()
|
||||
value = unquote(value).strip()
|
||||
headers[name] = value
|
||||
else:
|
||||
# this is not url-encoded and does not match the spec but we decided to be
|
||||
# liberal in what we accept to match other languages SDKs behaviour
|
||||
liberal_header_match = _LIBERAL_HEADER_PATTERN.fullmatch(
|
||||
header.strip()
|
||||
)
|
||||
if not liberal_header_match:
|
||||
_logger.warning(
|
||||
_INVALID_HEADER_ERROR_MESSAGE_LIBERAL_TEMPLATE, header
|
||||
)
|
||||
continue
|
||||
|
||||
liberal_match_string: str = liberal_header_match.string
|
||||
# value may contain any number of `=`
|
||||
name, value = liberal_match_string.split("=", 1)
|
||||
name = name.strip().lower()
|
||||
value = value.strip()
|
||||
headers[name] = value
|
||||
|
||||
return headers
|
||||
@@ -0,0 +1,59 @@
|
||||
# Copyright The OpenTelemetry Authors
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from typing import Mapping, Optional, Sequence, Tuple, Union
|
||||
|
||||
# This is the implementation of the "Any" type as specified by the specifications of OpenTelemetry data model for logs.
|
||||
# For more details, refer to the OTel specification:
|
||||
# https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/data-model.md#type-any
|
||||
AnyValue = Union[
|
||||
str,
|
||||
bool,
|
||||
int,
|
||||
float,
|
||||
bytes,
|
||||
Sequence["AnyValue"],
|
||||
Mapping[str, "AnyValue"],
|
||||
None,
|
||||
]
|
||||
|
||||
AttributeValue = Union[
|
||||
str,
|
||||
bool,
|
||||
int,
|
||||
float,
|
||||
Sequence[str],
|
||||
Sequence[bool],
|
||||
Sequence[int],
|
||||
Sequence[float],
|
||||
]
|
||||
Attributes = Optional[Mapping[str, AttributeValue]]
|
||||
AttributesAsKey = Tuple[
|
||||
Tuple[
|
||||
str,
|
||||
Union[
|
||||
str,
|
||||
bool,
|
||||
int,
|
||||
float,
|
||||
Tuple[Optional[str], ...],
|
||||
Tuple[Optional[bool], ...],
|
||||
Tuple[Optional[int], ...],
|
||||
Tuple[Optional[float], ...],
|
||||
],
|
||||
],
|
||||
...,
|
||||
]
|
||||
|
||||
_ExtendedAttributes = Mapping[str, "AnyValue"]
|
||||
Reference in New Issue
Block a user