Skip to content
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

chore(tracing): ensure tracing tests do not initialize the tracer #11824

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions tests/ci_visibility/test_ci_visibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ def test_civisibilitywriter_evp_proxy_url(self):
), mock.patch(
"ddtrace.internal.agent.get_trace_url", return_value="http://evpproxy.bar:1234"
), mock.patch("ddtrace.settings.config.Config", _get_default_civisibility_ddconfig()), mock.patch(
"ddtrace.tracer", ddtrace.Tracer()
"ddtrace.tracer", ddtrace.tracer
), mock.patch(
"ddtrace.internal.ci_visibility.recorder.CIVisibility._agent_evp_proxy_is_available", return_value=True
), _dummy_noop_git_client(), mock.patch(
Expand All @@ -699,7 +699,7 @@ def test_civisibilitywriter_only_traces(self):
)
), mock.patch(
"ddtrace.internal.agent.get_trace_url", return_value="http://onlytraces:1234"
), mock.patch("ddtrace.tracer", ddtrace.Tracer()), mock.patch(
), mock.patch("ddtrace.tracer", ddtrace.tracer), mock.patch(
"ddtrace.internal.ci_visibility.recorder.CIVisibility._agent_evp_proxy_is_available", return_value=False
), mock.patch(
"ddtrace.internal.ci_visibility.writer.config", ddtrace.settings.Config()
Expand Down Expand Up @@ -1113,7 +1113,7 @@ def test_civisibility_enable_respects_passed_in_tracer():
), _dummy_noop_git_client(), mock.patch(
"ddtrace.internal.ci_visibility.recorder.ddconfig", _get_default_civisibility_ddconfig()
), mock.patch("ddtrace.internal.ci_visibility.writer.config", ddtrace.settings.Config()):
tracer = ddtrace.Tracer()
tracer = ddtrace.tracer
tracer.configure(partial_flush_enabled=False, partial_flush_min_spans=100)
CIVisibility.enable(tracer=tracer)
assert CIVisibility._instance.tracer._partial_flush_enabled is False
Expand Down
2 changes: 1 addition & 1 deletion tests/ci_visibility/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,5 +209,5 @@ def _ci_override_env(
new_vars: t.Optional[t.Dict[str, str]] = None, mock_ci_env=False, replace_os_env=True, full_clear=False
):
env_vars = _get_default_ci_env_vars(new_vars, mock_ci_env, full_clear)
with override_env(env_vars, replace_os_env=replace_os_env), mock.patch("ddtrace.tracer", ddtrace.Tracer()):
with override_env(env_vars, replace_os_env=replace_os_env), mock.patch("ddtrace.tracer", ddtrace.tracer):
yield
4 changes: 2 additions & 2 deletions tests/commands/ddtrace_run_app_name.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from ddtrace.opentracer import Tracer
from ddtrace.opentracer import Tracer as OTTracer


if __name__ == "__main__":
tracer = Tracer()
tracer = OTTracer()
print(tracer._service_name)
7 changes: 3 additions & 4 deletions tests/contrib/aiomysql/test_aiomysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pytest

from ddtrace import Pin
from ddtrace import Tracer
from ddtrace import tracer
from ddtrace.contrib.aiomysql import patch
from ddtrace.contrib.aiomysql import unpatch
from ddtrace.internal.schema import DEFAULT_SPAN_SERVICE_NAME
Expand Down Expand Up @@ -38,12 +38,11 @@ async def patched_conn(tracer):

@pytest.fixture()
async def snapshot_conn():
tracer = Tracer()
conn = await aiomysql.connect(**AIOMYSQL_CONFIG)
Pin.get_from(conn).clone(tracer=tracer).onto(conn)
Pin.get_from(conn).clone().onto(conn)
yield conn
conn.close()
tracer.shutdown()
tracer.flush()


@pytest.mark.asyncio
Expand Down
8 changes: 1 addition & 7 deletions tests/contrib/flask_cache/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from flask import Flask

from ddtrace._trace.tracer import Tracer
from ddtrace import tracer
from ddtrace.contrib.flask_cache import get_traced_cache
from ddtrace.contrib.internal.flask_cache.utils import _extract_client
from ddtrace.contrib.internal.flask_cache.utils import _extract_conn_tags
Expand All @@ -17,7 +17,6 @@ class FlaskCacheUtilsTest(unittest.TestCase):

def test_extract_redis_connection_metadata(self):
# create the TracedCache instance for a Flask app
tracer = Tracer()
Cache = get_traced_cache(tracer, service=self.SERVICE)
app = Flask(__name__)
config = {
Expand All @@ -37,7 +36,6 @@ def test_extract_redis_connection_metadata(self):

def test_extract_memcached_connection_metadata(self):
# create the TracedCache instance for a Flask app
tracer = Tracer()
Cache = get_traced_cache(tracer, service=self.SERVICE)
app = Flask(__name__)
config = {
Expand All @@ -56,7 +54,6 @@ def test_extract_memcached_connection_metadata(self):

def test_extract_memcached_multiple_connection_metadata(self):
# create the TracedCache instance for a Flask app
tracer = Tracer()
Cache = get_traced_cache(tracer, service=self.SERVICE)
app = Flask(__name__)
config = {
Expand All @@ -78,7 +75,6 @@ def test_extract_memcached_multiple_connection_metadata(self):

def test_resource_from_cache_with_prefix(self):
# create the TracedCache instance for a Flask app
tracer = Tracer()
Cache = get_traced_cache(tracer, service=self.SERVICE)
app = Flask(__name__)
config = {
Expand All @@ -94,7 +90,6 @@ def test_resource_from_cache_with_prefix(self):

def test_resource_from_cache_with_empty_prefix(self):
# create the TracedCache instance for a Flask app
tracer = Tracer()
Cache = get_traced_cache(tracer, service=self.SERVICE)
app = Flask(__name__)
config = {
Expand All @@ -110,7 +105,6 @@ def test_resource_from_cache_with_empty_prefix(self):

def test_resource_from_cache_without_prefix(self):
# create the TracedCache instance for a Flask app
tracer = Tracer()
Cache = get_traced_cache(tracer, service=self.SERVICE)
app = Flask(__name__)
traced_cache = Cache(app, config={"CACHE_TYPE": "redis"})
Expand Down
4 changes: 1 addition & 3 deletions tests/contrib/kafka/test_kafka.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import pytest

from ddtrace import Pin
from ddtrace import Tracer
from ddtrace.contrib.kafka.patch import TracedConsumer
from ddtrace.contrib.kafka.patch import patch
from ddtrace.contrib.kafka.patch import unpatch
Expand Down Expand Up @@ -105,7 +104,7 @@ def should_filter_empty_polls():
@pytest.fixture
def tracer(should_filter_empty_polls):
patch()
t = Tracer()
t = DummyTracer()
if should_filter_empty_polls:
t.configure(settings={"FILTERS": [KafkaConsumerPollFilter()]})
# disable backoff because it makes these tests less reliable
Expand All @@ -114,7 +113,6 @@ def tracer(should_filter_empty_polls):
yield t
finally:
t.flush()
t.shutdown()
unpatch()


Expand Down
4 changes: 2 additions & 2 deletions tests/contrib/tornado/test_config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from ddtrace._trace.tracer import Tracer
from ddtrace.filters import TraceFilter
from tests.utils import DummyTracer
from tests.utils import DummyWriter

from .utils import TornadoTestCase
Expand All @@ -20,7 +20,7 @@ class TestTornadoSettings(TornadoTestCase):

def get_app(self):
# Override with a real tracer
self.tracer = Tracer()
self.tracer = DummyTracer()
super(TestTornadoSettings, self).get_app()

def get_settings(self):
Expand Down
24 changes: 12 additions & 12 deletions tests/integration/test_debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def test_standard_tags():


def test_debug_post_configure():
tracer = ddtrace.Tracer()
tracer = ddtrace.tracer
tracer.configure(
hostname="0.0.0.0",
port=1234,
Expand All @@ -131,7 +131,7 @@ def test_debug_post_configure():

# Tracer doesn't support re-configure()-ing with a UDS after an initial
# configure with normal http settings. So we need a new tracer instance.
tracer = ddtrace.Tracer()
tracer = ddtrace.tracer
tracer.configure(uds_path="/file.sock")

f = debug.collect(tracer)
Expand Down Expand Up @@ -187,7 +187,7 @@ def test_trace_agent_url(self):
)
)
def test_tracer_loglevel_info_connection(self):
tracer = ddtrace.Tracer()
tracer = ddtrace.tracer
logging.basicConfig(level=logging.INFO)
with mock.patch.object(logging.Logger, "log") as mock_logger:
# shove an unserializable object into the config log output
Expand All @@ -203,7 +203,7 @@ def test_tracer_loglevel_info_connection(self):
)
)
def test_tracer_loglevel_info_no_connection(self):
tracer = ddtrace.Tracer()
tracer = ddtrace.tracer
logging.basicConfig(level=logging.INFO)
with mock.patch.object(logging.Logger, "log") as mock_logger:
tracer.configure()
Expand All @@ -217,7 +217,7 @@ def test_tracer_loglevel_info_no_connection(self):
)
)
def test_tracer_log_disabled_error(self):
tracer = ddtrace.Tracer()
tracer = ddtrace.tracer
with mock.patch.object(logging.Logger, "log") as mock_logger:
tracer.configure()
assert mock_logger.mock_calls == []
Expand All @@ -229,7 +229,7 @@ def test_tracer_log_disabled_error(self):
)
)
def test_tracer_log_disabled(self):
tracer = ddtrace.Tracer()
tracer = ddtrace.tracer
with mock.patch.object(logging.Logger, "log") as mock_logger:
tracer.configure()
assert mock_logger.mock_calls == []
Expand All @@ -241,7 +241,7 @@ def test_tracer_log_disabled(self):
)
def test_tracer_info_level_log(self):
logging.basicConfig(level=logging.INFO)
tracer = ddtrace.Tracer()
tracer = ddtrace.tracer
with mock.patch.object(logging.Logger, "log") as mock_logger:
tracer.configure()
assert mock_logger.mock_calls == []
Expand Down Expand Up @@ -287,14 +287,14 @@ def test_to_json():

def test_agentless(monkeypatch):
monkeypatch.setenv("AWS_LAMBDA_FUNCTION_NAME", "something")
tracer = ddtrace.Tracer()
tracer = ddtrace.tracer
info = debug.collect(tracer)

assert info.get("agent_url") == "AGENTLESS"


def test_custom_writer():
tracer = ddtrace.Tracer()
tracer = ddtrace.tracer

class CustomWriter(TraceWriter):
def recreate(self) -> TraceWriter:
Expand All @@ -316,15 +316,15 @@ def flush_queue(self) -> None:


def test_different_samplers():
tracer = ddtrace.Tracer()
tracer = ddtrace.tracer
tracer.configure(sampler=ddtrace.sampler.RateSampler())
info = debug.collect(tracer)

assert info.get("sampler_type") == "RateSampler"


def test_startup_logs_sampling_rules():
tracer = ddtrace.Tracer()
tracer = ddtrace.tracer
sampler = ddtrace.sampler.DatadogSampler(rules=[ddtrace.sampler.SamplingRule(sample_rate=1.0)])
tracer.configure(sampler=sampler)
f = debug.collect(tracer)
Expand Down Expand Up @@ -413,7 +413,7 @@ def test_debug_span_log():


def test_partial_flush_log():
tracer = ddtrace.Tracer()
tracer = ddtrace.tracer

tracer.configure(
partial_flush_enabled=True,
Expand Down
14 changes: 5 additions & 9 deletions tests/integration/test_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@
import mock
import pytest

from ddtrace import Tracer
from ddtrace import tracer


AGENT_VERSION = os.environ.get("AGENT_VERSION")


class TestTraceAcceptedByAgent:
def test_simple_trace_accepted_by_agent(self):
tracer = Tracer()
with mock.patch("ddtrace.internal.writer.writer.log") as log:
with tracer.trace("root"):
for _ in range(999):
with tracer.trace("child"):
pass
tracer.shutdown()
tracer.flush()
log.warning.assert_not_called()
log.error.assert_not_called()

Expand All @@ -32,14 +31,13 @@ def test_simple_trace_accepted_by_agent(self):
)
def test_trace_with_meta_accepted_by_agent(self, tags):
"""Meta tags should be text types."""
tracer = Tracer()
with mock.patch("ddtrace.internal.writer.writer.log") as log:
with tracer.trace("root", service="test_encoding", resource="test_resource") as root:
root.set_tags(tags)
for _ in range(999):
with tracer.trace("child") as child:
child.set_tags(tags)
tracer.shutdown()
tracer.flush()
log.warning.assert_not_called()
log.error.assert_not_called()

Expand All @@ -53,14 +51,13 @@ def test_trace_with_meta_accepted_by_agent(self, tags):
)
def test_trace_with_metrics_accepted_by_agent(self, metrics):
"""Metric tags should be numeric types - i.e. int, float, long (py3), and str numbers."""
tracer = Tracer()
with mock.patch("ddtrace.internal.writer.writer.log") as log:
with tracer.trace("root") as root:
root.set_metrics(metrics)
for _ in range(999):
with tracer.trace("child") as child:
child.set_metrics(metrics)
tracer.shutdown()
tracer.flush()
log.warning.assert_not_called()
log.error.assert_not_called()

Expand All @@ -72,13 +69,12 @@ def test_trace_with_metrics_accepted_by_agent(self, metrics):
)
def test_trace_with_links_accepted_by_agent(self, span_links_kwargs):
"""Links should not break things."""
tracer = Tracer()
with mock.patch("ddtrace.internal.writer.writer.log") as log:
with tracer.trace("root", service="test_encoding", resource="test_resource") as root:
root.set_link(**span_links_kwargs)
for _ in range(10):
with tracer.trace("child") as child:
child.set_link(**span_links_kwargs)
tracer.shutdown()
tracer.flush()
log.warning.assert_not_called()
log.error.assert_not_called()
10 changes: 5 additions & 5 deletions tests/integration/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import mock
import pytest

from ddtrace import Tracer
from ddtrace.internal.atexit import register_on_exit_signal
from ddtrace.internal.runtime import container
from ddtrace.internal.writer import AgentWriter
Expand All @@ -17,14 +16,15 @@
from tests.integration.utils import parametrize_with_all_encodings
from tests.integration.utils import send_invalid_payload_and_get_logs
from tests.integration.utils import skip_if_testagent
from tests.utils import DummyTracer
from tests.utils import call_program


FOUR_KB = 1 << 12


def test_configure_keeps_api_hostname_and_port():
tracer = Tracer()
tracer = DummyTracer()
assert tracer._writer.agent_url == "http://localhost:{}".format("9126" if AGENT_VERSION == "testagent" else "8126")
tracer.configure(hostname="127.0.0.1", port=8127)
assert tracer._writer.agent_url == "http://127.0.0.1:8127"
Expand All @@ -38,7 +38,7 @@ def test_configure_keeps_api_hostname_and_port():
@mock.patch("signal.getsignal")
def test_shutdown_on_exit_signal(mock_get_signal, mock_signal):
mock_get_signal.return_value = None
tracer = Tracer()
tracer = DummyTracer()
register_on_exit_signal(tracer._atexit)
assert mock_signal.call_count == 2
assert mock_signal.call_args_list[0][0][0] == signal.SIGTERM
Expand Down Expand Up @@ -507,7 +507,7 @@ def test_validate_headers_in_payload_to_intake_with_nested_spans():


def test_trace_with_invalid_client_endpoint_generates_error_log():
t = Tracer()
t = DummyTracer()
for client in t._writer._clients:
client.ENDPOINT = "/bad"
with mock.patch("ddtrace.internal.writer.writer.log") as log:
Expand Down Expand Up @@ -621,7 +621,7 @@ def test_api_version_downgrade_generates_no_warning_logs():


def test_synchronous_writer_shutdown_raises_no_exception():
tracer = Tracer()
tracer = DummyTracer()
tracer.configure(writer=AgentWriter(tracer._writer.agent_url, sync_mode=True))
tracer.shutdown()

Expand Down
Loading
Loading