def test_int_service_integration(int_config): pin = Pin() tracer = Tracer() assert trace_utils.int_service(pin, int_config.myint) is None with override_global_config(dict(service="global-svc")): assert trace_utils.int_service(pin, int_config.myint) is None with tracer.trace("something", service=trace_utils.int_service(pin, int_config.myint)) as s: assert s.service == "global-svc"
def test_int_service(int_config, pin, config_val, default, global_service, expected): if config_val: int_config.myint.service = config_val if global_service: int_config.service = global_service assert trace_utils.int_service(pin, int_config.myint, default) == expected
def pytest_configure(config): config.addinivalue_line("markers", "dd_tags(**kwargs): add tags to current span") if is_enabled(config): ci_tags = ci.tags() if ci_tags.get(ci.git.REPOSITORY_URL, None) and int_service(None, ddtrace.config.pytest) == "pytest": repository_name = _extract_repository_name(ci_tags[ci.git.REPOSITORY_URL]) ddtrace.config.pytest["service"] = repository_name Pin(tags=ci_tags, _config=ddtrace.config.pytest).onto(config)
def pytest_runtest_protocol(item, nextitem): pin = Pin.get_from(item.config) if pin is None: yield return with pin.tracer.trace( ddtrace.config.pytest.operation_name, service=int_service(pin, ddtrace.config.pytest), resource=item.nodeid, span_type=SpanTypes.TEST.value, ) as span: span.context.dd_origin = ci.CI_APP_TEST_ORIGIN span.context.sampling_priority = AUTO_KEEP span.set_tags(pin.tags) span.set_tag(SPAN_KIND, KIND) span.set_tag(test.FRAMEWORK, FRAMEWORK) span.set_tag(test.NAME, item.name) if hasattr(item, "module"): span.set_tag(test.SUITE, item.module.__name__) elif hasattr(item, "dtest") and isinstance(item.dtest, DocTest): span.set_tag(test.SUITE, item.dtest.globs["__name__"]) span.set_tag(test.TYPE, SpanTypes.TEST.value) span.set_tag(test.FRAMEWORK_VERSION, pytest.__version__) # We preemptively set FAIL as a status, because if pytest_runtest_makereport is not called # (where the actual test status is set), it means there was a pytest error span.set_tag(test.STATUS, test.Status.FAIL.value) # Parameterized test cases will have a `callspec` attribute attached to the pytest Item object. # Pytest docs: https://docs.pytest.org/en/6.2.x/reference.html#pytest.Function if getattr(item, "callspec", None): parameters = { "arguments": {}, "metadata": {} } # type: Dict[str, Dict[str, str]] for param_name, param_val in item.callspec.params.items(): try: parameters["arguments"][param_name] = repr(param_val) except Exception: parameters["arguments"][param_name] = "Could not encode" log.warning("Failed to encode %r", param_name, exc_info=True) span.set_tag(test.PARAMETERS, json.dumps(parameters)) markers = [ marker.kwargs for marker in item.iter_markers(name="dd_tags") ] for tags in markers: span.set_tags(tags) _store_span(item, span) yield
def traced_get_socket(wrapped, instance, args, kwargs): pin = Pin._find(wrapped, instance) if not pin or not pin.enabled(): with wrapped(*args, **kwargs) as sock_info: yield sock_info return with pin.tracer.trace("pymongo.get_socket", service=trace_utils.int_service(pin, config.pymongo), span_type=SpanTypes.MONGODB) as span: with wrapped(*args, **kwargs) as sock_info: set_address_tags(span, sock_info.address) span.set_tag(SPAN_MEASURED_KEY) yield sock_info
def pytest_runtest_protocol(item, nextitem): pin = Pin.get_from(item.config) if pin is None: yield return with pin.tracer.trace( ddtrace.config.pytest.operation_name, service=int_service(pin, ddtrace.config.pytest), resource=item.nodeid, span_type=SpanTypes.TEST.value, ) as span: span.context.dd_origin = ci.CI_APP_TEST_ORIGIN span.set_tags(pin.tags) span.set_tag(SPAN_KIND, KIND) span.set_tag(test.FRAMEWORK, FRAMEWORK) span.set_tag(test.NAME, item.name) span.set_tag(test.SUITE, item.module.__name__) span.set_tag(test.TYPE, SpanTypes.TEST.value) # Parameterized test cases will have a `callspec` attribute attached to the pytest Item object. # Pytest docs: https://docs.pytest.org/en/6.2.x/reference.html#pytest.Function if getattr(item, "callspec", None): parameters = {"arguments": {}, "metadata": {}} # type: Dict[str, Dict[str, str]] for param_name, param_val in item.callspec.params.items(): try: parameters["arguments"][param_name] = repr(param_val) except Exception: parameters["arguments"][param_name] = "Could not encode" log.warning("Failed to encode %r", param_name, exc_info=True) span.set_tag(test.PARAMETERS, json.dumps(parameters)) markers = [marker.kwargs for marker in item.iter_markers(name="dd_tags")] for tags in markers: span.set_tags(tags) _store_span(item, span) yield