|
| 1 | +import contextlib |
1 | 2 | import os |
2 | 3 | import socket |
| 4 | +import sys |
3 | 5 | import tempfile |
4 | 6 | import threading |
5 | 7 | import time |
|
20 | 22 | from ddtrace.internal.writer import LogWriter |
21 | 23 | from ddtrace.internal.writer import Response |
22 | 24 | from ddtrace.internal.writer import _human_size |
| 25 | +from ddtrace.sampler import RateByServiceSampler |
23 | 26 | from ddtrace.span import Span |
24 | 27 | from tests.utils import AnyInt |
25 | 28 | from tests.utils import BaseTestCase |
26 | 29 | from tests.utils import override_env |
27 | 30 |
|
28 | 31 |
|
| 32 | +@contextlib.contextmanager |
| 33 | +def mock_sys_platform(new_value): |
| 34 | + old_value = sys.platform |
| 35 | + try: |
| 36 | + sys.platform = new_value |
| 37 | + yield |
| 38 | + finally: |
| 39 | + sys.platform = old_value |
| 40 | + |
| 41 | + |
29 | 42 | class DummyOutput: |
30 | 43 | def __init__(self): |
31 | 44 | self.entries = [] |
@@ -552,6 +565,86 @@ def test_writer_recreate_api_version(init_api_version, api_version, endpoint, en |
552 | 565 | assert isinstance(writer._encoder, encoder_cls) |
553 | 566 |
|
554 | 567 |
|
| 568 | +@pytest.mark.parametrize( |
| 569 | + "sys_platform, api_version, ddtrace_api_version, priority_sampler, raises_error, expected", |
| 570 | + [ |
| 571 | + # -- win32 |
| 572 | + # Defaults on windows |
| 573 | + ("win32", None, None, None, False, "v0.3"), |
| 574 | + # Default with priority sampler |
| 575 | + ("win32", None, None, RateByServiceSampler(), False, "v0.4"), |
| 576 | + # Explicitly passed in API version is always used |
| 577 | + ("win32", "v0.3", None, RateByServiceSampler(), False, "v0.3"), |
| 578 | + ("win32", "v0.3", "v0.4", None, False, "v0.3"), |
| 579 | + ("win32", "v0.3", "v0.4", RateByServiceSampler(), False, "v0.3"), |
| 580 | + # Env variable is used if explicit value is not given |
| 581 | + ("win32", None, "v0.4", None, False, "v0.4"), |
| 582 | + ("win32", None, "v0.4", RateByServiceSampler(), False, "v0.4"), |
| 583 | + # v0.5 is not supported on windows |
| 584 | + ("win32", "v0.5", None, None, True, None), |
| 585 | + ("win32", "v0.5", None, RateByServiceSampler(), True, None), |
| 586 | + ("win32", "v0.5", "v0.4", RateByServiceSampler(), True, None), |
| 587 | + ("win32", None, "v0.5", RateByServiceSampler(), True, None), |
| 588 | + # -- cygwin |
| 589 | + # Defaults on windows |
| 590 | + ("cygwin", None, None, None, False, "v0.3"), |
| 591 | + # Default with priority sampler |
| 592 | + ("cygwin", None, None, RateByServiceSampler(), False, "v0.4"), |
| 593 | + # Explicitly passed in API version is always used |
| 594 | + ("cygwin", "v0.3", None, RateByServiceSampler(), False, "v0.3"), |
| 595 | + ("cygwin", "v0.3", "v0.4", None, False, "v0.3"), |
| 596 | + ("cygwin", "v0.3", "v0.4", RateByServiceSampler(), False, "v0.3"), |
| 597 | + # Env variable is used if explicit value is not given |
| 598 | + ("cygwin", None, "v0.4", None, False, "v0.4"), |
| 599 | + ("cygwin", None, "v0.4", RateByServiceSampler(), False, "v0.4"), |
| 600 | + # v0.5 is not supported on windows |
| 601 | + ("cygwin", "v0.5", None, None, True, None), |
| 602 | + ("cygwin", "v0.5", None, RateByServiceSampler(), True, None), |
| 603 | + ("cygwin", "v0.5", "v0.4", RateByServiceSampler(), True, None), |
| 604 | + ("cygwin", None, "v0.5", RateByServiceSampler(), True, None), |
| 605 | + # -- Non-windows |
| 606 | + # defaults |
| 607 | + ("darwin", None, None, None, False, "v0.3"), |
| 608 | + # Default with priority sample |
| 609 | + ("darwin", None, None, RateByServiceSampler(), False, "v0.5"), |
| 610 | + # Explicitly setting api version |
| 611 | + ("darwin", "v0.4", None, RateByServiceSampler(), False, "v0.4"), |
| 612 | + # Explicitly set version takes precedence |
| 613 | + ("darwin", "v0.4", "v0.5", RateByServiceSampler(), False, "v0.4"), |
| 614 | + # Via env variable |
| 615 | + ("darwin", None, "v0.4", RateByServiceSampler(), False, "v0.4"), |
| 616 | + ("darwin", None, "v0.5", RateByServiceSampler(), False, "v0.5"), |
| 617 | + ], |
| 618 | +) |
| 619 | +def test_writer_api_version_selection( |
| 620 | + sys_platform, api_version, ddtrace_api_version, priority_sampler, raises_error, expected, monkeypatch |
| 621 | +): |
| 622 | + """test to verify that we are unable to select v0.5 api version when on a windows machine. |
| 623 | +
|
| 624 | + https://docs.python.org/3/library/sys.html#sys.platform |
| 625 | +
|
| 626 | + The possible ``sys.platform`` values when on windows are ``win32`` or ``cygwin``. |
| 627 | + """ |
| 628 | + |
| 629 | + # Mock the value of `sys.platform` to be a specific value |
| 630 | + with mock_sys_platform(sys_platform): |
| 631 | + |
| 632 | + # If desired, set the DD_TRACE_API_VERSION env variable |
| 633 | + if ddtrace_api_version is not None: |
| 634 | + monkeypatch.setenv("DD_TRACE_API_VERSION", ddtrace_api_version) |
| 635 | + |
| 636 | + try: |
| 637 | + # Create a new writer |
| 638 | + writer = AgentWriter( |
| 639 | + agent_url="http://dne:1234", api_version=api_version, priority_sampler=priority_sampler |
| 640 | + ) |
| 641 | + assert writer._api_version == expected |
| 642 | + except RuntimeError: |
| 643 | + # If we were not expecting a RuntimeError, then cause the test to fail |
| 644 | + if not raises_error: |
| 645 | + pytest.fail("Raised RuntimeError when it was not expected") |
| 646 | + |
| 647 | + |
555 | 648 | def test_writer_reuse_connections_envvar(monkeypatch): |
556 | 649 | monkeypatch.setenv("DD_TRACE_WRITER_REUSE_CONNECTIONS", "false") |
557 | 650 | writer = AgentWriter(agent_url="http://localhost:9126") |
|
0 commit comments