Skip to content

Commit

Permalink
Add option --x-trace-all for forcing distribute tracing (#1973)
Browse files Browse the repository at this point in the history
  • Loading branch information
serhiy-storchaka authored Feb 4, 2021
1 parent 026836f commit 039dea7
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.D/1973.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added private option `--x-trace-all` which forces distribute tracing.
8 changes: 8 additions & 0 deletions neuro-cli/src/neuro_cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ def make_context(
network_timeout=kwargs["network_timeout"],
config_path=Path(kwargs["neuromation_config"]),
trace=kwargs["trace"],
force_trace_all=kwargs["x_trace_all"],
trace_hide_token=hide_token_bool,
command_path="",
command_params=[],
Expand Down Expand Up @@ -383,6 +384,12 @@ def print_options(
is_flag=True,
help="Trace sent HTTP requests and received replies to stderr.",
)
@option(
"--x-trace-all",
hidden=True,
is_flag=True,
help="Force distribute tracing in all HTTP requests.",
)
@option(
"--hide-token/--no-hide-token",
is_flag=True,
Expand Down Expand Up @@ -415,6 +422,7 @@ def cli(
disable_pypi_version_check: bool,
network_timeout: float,
trace: bool,
x_trace_all: bool,
hide_token: Optional[bool],
skip_stats: bool,
) -> None:
Expand Down
2 changes: 2 additions & 0 deletions neuro-cli/src/neuro_cli/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class Root:
network_timeout: float
config_path: Path
trace: bool
force_trace_all: bool
verbosity: int
trace_hide_token: bool
command_path: str
Expand Down Expand Up @@ -174,6 +175,7 @@ def factory(self) -> Factory:
path=self.config_path,
trace_configs=trace_configs,
trace_id=gen_trace_id(),
trace_sampled=True if self.force_trace_all else None,
)
return self._factory

Expand Down
1 change: 1 addition & 0 deletions neuro-cli/tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def create_root(config_path: Path) -> Root:
verbosity=0,
trace=False,
trace_hide_token=True,
force_trace_all=False,
command_path="",
command_params=[],
skip_gmp_stats=True,
Expand Down
2 changes: 2 additions & 0 deletions neuro-cli/tests/unit/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def test_prompt_cluster(make_client: Callable[..., Client]) -> None:
verbosity=0,
trace=False,
trace_hide_token=True,
force_trace_all=False,
command_path="",
command_params=[],
skip_gmp_stats=True,
Expand Down Expand Up @@ -112,6 +113,7 @@ def test_prompt_cluster_default(make_client: Callable[..., Client]) -> None:
verbosity=0,
trace=False,
trace_hide_token=True,
force_trace_all=False,
command_path="",
command_params=[],
skip_gmp_stats=True,
Expand Down
1 change: 1 addition & 0 deletions neuro-cli/tests/unit/test_root.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def root_uninitialized() -> Iterator[Root]:
verbosity=0,
trace=False,
trace_hide_token=True,
force_trace_all=False,
command_path="",
command_params=[],
skip_gmp_stats=True,
Expand Down
1 change: 1 addition & 0 deletions neuro-cli/tests/unit/test_storage_progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def make(color: bool, tty: bool, verbose: bool) -> Root:
verbosity=int(verbose),
trace=False,
trace_hide_token=True,
force_trace_all=False,
command_path="",
command_params=[],
skip_gmp_stats=True,
Expand Down
4 changes: 2 additions & 2 deletions neuro-sdk/src/neuro_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ def __init__(
session: aiohttp.ClientSession,
path: Path,
trace_id: Optional[str],
trace_sampled: Optional[bool] = None,
) -> None:
self._closed = False
self._trace_id = trace_id
self._session = session
self._core = _Core(session, trace_id)
self._core = _Core(session, trace_id, trace_sampled)
self._config = Config._create(self._core, path)

# Order does matter, need to check the main config before loading
Expand Down
6 changes: 5 additions & 1 deletion neuro-sdk/src/neuro_sdk/config_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def __init__(
path: Optional[Path] = None,
trace_configs: Optional[List[aiohttp.TraceConfig]] = None,
trace_id: Optional[str] = None,
trace_sampled: Optional[bool] = None,
) -> None:
if path is None:
path = Path(os.environ.get(CONFIG_ENV_NAME, DEFAULT_CONFIG_PATH))
Expand All @@ -64,6 +65,7 @@ def __init__(
if trace_configs:
self._trace_configs += trace_configs
self._trace_id = trace_id
self._trace_sampled = trace_sampled

@property
def path(self) -> Path:
Expand All @@ -78,7 +80,9 @@ async def get(self, *, timeout: aiohttp.ClientTimeout = DEFAULT_TIMEOUT) -> Clie
await self.login_with_passed_config(timeout=timeout)
session = await _make_session(timeout, self._trace_configs)
try:
client = Client._create(session, self._path, self._trace_id)
client = Client._create(
session, self._path, self._trace_id, self._trace_sampled
)
await client.config.check_server()
except (asyncio.CancelledError, Exception):
await session.close()
Expand Down
3 changes: 3 additions & 0 deletions neuro-sdk/src/neuro_sdk/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@ def __init__(
self,
session: aiohttp.ClientSession,
trace_id: Optional[str],
trace_sampled: Optional[bool] = None,
) -> None:
self._session = session
self._trace_id = trace_id
self._trace_sampled = trace_sampled
self._exception_map = {
400: IllegalArgumentError,
401: AuthenticationError,
Expand Down Expand Up @@ -131,6 +133,7 @@ async def request(
if trace_id is None:
trace_id = gen_trace_id()
trace_request_ctx.trace_id = trace_id
trace_request_ctx.trace_sampled = self._trace_sampled
if params:
url = url.with_query(params)
async with self._session.request(
Expand Down
18 changes: 16 additions & 2 deletions neuro-sdk/src/neuro_sdk/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import random
import time
import types
from typing import Optional

import aiohttp
from multidict import CIMultiDict
Expand Down Expand Up @@ -32,10 +33,22 @@ def _gen_span_id() -> str:
return os.urandom(8).hex()


def _update_headers(headers: "CIMultiDict[str]", trace_id: str, span_id: str) -> None:
def _update_headers(
headers: "CIMultiDict[str]",
trace_id: str,
span_id: str,
sampled: Optional[bool] = None,
) -> None:
"""Creates dict with zipkin single header format."""
# b3={TraceId}-{SpanId}-{SamplingState}-{ParentSpanId}
headers["b3"] = f"{trace_id}-{span_id}"
if sampled is True:
sampled_str = "1"
elif sampled is False:
sampled_str = "0"
else:
sampled_str = ""
headers["sentry-trace"] = f"{trace_id}-{span_id}-{sampled_str}"


async def _on_request_start(
Expand All @@ -47,8 +60,9 @@ async def _on_request_start(
trace_id = getattr(trace_ctx, "trace_id", None)
if trace_id is None:
return
sampled = getattr(trace_ctx, "trace_sampled", None)
span_id = _gen_span_id()
_update_headers(params.headers, trace_id, span_id)
_update_headers(params.headers, trace_id, span_id, sampled)


def _make_trace_config() -> aiohttp.TraceConfig:
Expand Down

0 comments on commit 039dea7

Please sign in to comment.