Skip to content

Commit

Permalink
Fix ddtrace deprecation warnings. There is a new version 3 of ddtrace…
Browse files Browse the repository at this point in the history
…, this is one step closer to be able to use that. (#363)
  • Loading branch information
eirsyl authored Feb 28, 2025
1 parent 11f3054 commit ae3bf2b
Show file tree
Hide file tree
Showing 15 changed files with 276 additions and 389 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
strategy:
fail-fast: false
matrix:
check: [isort, mypy, black, ruff-check, ruff-format]
check: [mypy, ruff-check, ruff-format]

steps:
- name: Checkout
Expand Down
10 changes: 0 additions & 10 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,6 @@ repos:

- repo: local
hooks:
- id: isort
name: isort
entry: .venv/bin/isort
language: system
types: [python]
- id: black
name: black
entry: .venv/bin/black
language: system
types: [python]
- id: ruff
name: ruff
entry: .venv/bin/ruff
Expand Down
35 changes: 7 additions & 28 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,30 @@ $(POETRY): ; $(info $(M) checking POETRY...)
@touch $@

.PHONY: lint
lint: .venv lint-isort lint-black lint-ruff-check lint-ruff-format lint-mypy ## Run all linters

.PHONY: lint-isort
lint-isort: .venv ; $(info $(M) running isort...) @ ## Run isort linter
$Q $(POETRY) run isort -c --diff $(PACKAGE)
$Q $(POETRY) run isort -c --diff $(TESTS)

.PHONY: lint-black
lint-black: .venv ; $(info $(M) running black...) @ ## Run black linter
$Q $(POETRY) run black --check $(PACKAGE)
$Q $(POETRY) run black --check $(TESTS)
lint: .venv lint-ruff-check lint-ruff-format lint-mypy ## Run all linters

.PHONY: lint-mypy
lint-mypy: .venv ; $(info $(M) running mypy...) @ ## Run mypy linter
$Q $(POETRY) run mypy $(PACKAGE)
$Q $(POETRY) run mypy $(TESTS)
$Q $(POETRY) run mypy $(PACKAGE) $(TESTS)

.PHONY: lint-ruff-check
lint-ruff-check: .venv | $(BASE) ; $(info $(M) running ruff check…) @ ## Run ruff check linter
$Q $(POETRY) run ruff check $(PACKAGE)
$Q $(POETRY) run ruff check $(PACKAGE) $(TESTS)

.PHONY: lint-ruff-format
lint-ruff-format: .venv | $(BASE) ; $(info $(M) running ruff format…) @ ## Run ruff format linter
$Q $(POETRY) run ruff format $(PACKAGE) --check
$Q $(POETRY) run ruff format $(PACKAGE) $(TESTS) --check

.PHONY: fix
fix: .venv fix-isort fix-black fix-ruff-check fix-ruff-format ## Run all fixers

.PHONY: fix-isort
fix-isort: .venv ; $(info $(M) running isort...) @ ## Run isort fixer
$Q $(POETRY) run isort $(PACKAGE)
$Q $(POETRY) run isort $(TESTS)

.PHONY: fix-black
fix-black: .venv ; $(info $(M) running black...) @ ## Run black fixer
$Q $(POETRY) run black $(PACKAGE)
$Q $(POETRY) run black $(TESTS)
fix: .venv fix-ruff-check fix-ruff-format ## Run all fixers

.PHONY: fix-ruff-check
fix-ruff-check: .venv | $(BASE) ; $(info $(M) running ruff check…) @ ## Run ruff check fixer
$Q $(POETRY) run ruff check $(PACKAGE) --fix
$Q $(POETRY) run ruff check $(PACKAGE) $(TESTS) --fix

.PHONY: fix-ruff-format
fix-ruff-format: .venv | $(BASE) ; $(info $(M) running ruff format…) @ ## Run ruff format fixer
$Q $(POETRY) run ruff format $(PACKAGE)
$Q $(POETRY) run ruff format $(PACKAGE) $(TESTS)

.PHONY: test
test: .venv ; $(info $(M) running tests...) @ ## Run tests
Expand Down
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ ddtrace.patch_all()

# Configure the ddtrace tracer to send traces to Tempo.
configure_tracer(
enabled=False, # Set to True when TRACE_HOST is configured.
service_name='SERVICE_NAME',
exporter=Exporter(
host = "127.0.0.1", # Usually obtained from env variables.
Expand Down Expand Up @@ -144,7 +143,6 @@ from troncos.tracing import configure_tracer, Exporter


configure_tracer(
enabled=False, # Set to True when TRACE_HOST is configured.
service_name='SERVICE_NAME',
exporter=Exporter(
host = "127.0.0.1", # Usually obtained from env variables.
Expand All @@ -160,7 +158,6 @@ from troncos.tracing import configure_tracer, Exporter


configure_tracer(
enabled=False, # Set to True when TRACE_HOST is configured.
service_name='SERVICE_NAME',
exporter=Exporter(
host = "127.0.0.1", # Usually obtained from env variables.
Expand Down
450 changes: 175 additions & 275 deletions poetry.lock

Large diffs are not rendered by default.

30 changes: 15 additions & 15 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@ grpc = ["opentelemetry-exporter-otlp-proto-grpc"]
sentry = ["structlog-sentry"]

[tool.poetry.group.dev.dependencies]
black = "^24.0.0"
celery = "^5.3.4"
django = "^5.0.0"
django-environ = "^0.11.0"
fastapi = ">=0.111,<0.112"
ipython = "^8.14.0"
isort = "^5.12.0"
mkdocs-techdocs-core = "^1.2.3"
mypy = "^1.5.0"
mypy = "^1.15.0"
pytest = "^8.0.0"
pytest_codeblocks = "^0.17.0"
pytest-asyncio = ">=0.23.1,<0.24.0"
Expand All @@ -48,19 +46,10 @@ requests = "^2.31.0"
snakeviz = "^2.2.0"
structlog = "^24.0.0"
types-requests = "^2.28.11.14"
ruff = "^0.8.0"
ruff = "^0.9.8"
pytest-env = "^1.1.5"


[tool.black]
target-version = ['py311']
line-length = 88
include = '\.pyi?$'

[tool.isort]
profile = "black"
skip_gitignore = true
combine_as_imports = true

[tool.mypy]
python_version = "3.11"
files = ["./troncos", "./tests"]
Expand All @@ -87,11 +76,22 @@ console_output_style = "classic"
empty_parameter_set_mark = "xfail"
log_cli_level = "INFO"
log_level = "INFO"
filterwarnings = ["ignore::DeprecationWarning"]
ddtrace = 0
filterwarnings = [
"error",
"ignore:Type google._upb._message.MessageMapContainer:DeprecationWarning:",
"ignore:Type google._upb._message.ScalarMapContainer:DeprecationWarning:",
"ignore:datetime.datetime.utcfromtimestamp:DeprecationWarning:",
"ignore:pkg_resources is deprecated as an API:DeprecationWarning:",
"ignore:Deprecated call to `pkg_resources:DeprecationWarning:",
]
addopts = [
"--ignore=perf"
]

[tool.pytest_env]
DD_TRACE_ENABLED = "True"

[tool.ruff]
line-length = 88
exclude = [
Expand Down
28 changes: 14 additions & 14 deletions tests/structlog/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,23 @@ def test_processors_with_sentry() -> None:

logger_name_index = shared_processors.index(structlog.stdlib.add_logger_name)
log_level_index = shared_processors.index(structlog.stdlib.add_log_level)
sentry_processor = [
sentry_processor = next(
proc for proc in shared_processors if "structlog_sentry" in repr(proc)
][0]
)
sentry_processor_index = shared_processors.index(sentry_processor)
format_exc_info_index = shared_processors.index(
structlog.processors.format_exc_info
)

assert (
logger_name_index < sentry_processor_index
), "Logger name must come before Sentry processor"
assert (
log_level_index < sentry_processor_index
), "Log level must come before Sentry processor"
assert (
sentry_processor_index < format_exc_info_index
), "Format exc info must come after Sentry processor"
assert logger_name_index < sentry_processor_index, (
"Logger name must come before Sentry processor"
)
assert log_level_index < sentry_processor_index, (
"Log level must come before Sentry processor"
)
assert sentry_processor_index < format_exc_info_index, (
"Format exc info must come after Sentry processor"
)


@patch("builtins.__import__", side_effect=import_without_structlog_sentry)
Expand All @@ -65,9 +65,9 @@ def test_processors_without_sentry(mocked: Any) -> None:
for proc in shared_processors
if "structlog_sentry" in repr(proc)
]
assert (
len(sentry_processors) == 0
), "No Sentry processors should exist in shared processors when not installed"
assert len(sentry_processors) == 0, (
"No Sentry processors should exist in shared processors when not installed"
)

# And make sure nothing in the actual initiation blows up when we don't have
# structlog_sentry installed
Expand Down
6 changes: 3 additions & 3 deletions tests/tracing/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@ def test_trace_class_is_traced(class_attr: Any) -> None:
],
)
def test_trace_class_is_not_traced(class_attr: Any) -> None:
assert (
hasattr(class_attr, "__wrapped__") is False
), f"Expected {class_attr} not to be traced"
assert hasattr(class_attr, "__wrapped__") is False, (
f"Expected {class_attr} not to be traced"
)
45 changes: 30 additions & 15 deletions tests/tracing/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Any, Generator

from ddtrace import Tracer
from ddtrace.trace import tracer
from pytest_httpserver import HTTPServer

from troncos.tracing._exporter import Exporter, ExporterType
Expand All @@ -14,9 +15,10 @@ def tracer_test(
service_name: str,
resource_attributes: dict[str, Any] | None = None,
) -> Generator[Tracer, Any, Any]:
httpserver.expect_oneshot_request("/v1/trace").respond_with_data("OK")
httpserver.expect_request("/v1/trace").respond_with_data("OK")

assert tracer.current_span() is None

tracer = Tracer()
tracer.configure(
writer=OTELWriter(
service_name=service_name,
Expand All @@ -36,15 +38,20 @@ def tracer_test(


def tracer_assert(httpserver: HTTPServer) -> bytes:
assert len(httpserver.log), "We should have gotten 1 request"
req, res = httpserver.log[0]
assert res.status_code == 200, "Response should have been 200"
return req.data
assert len(httpserver.log), "We should have gotten at least 1 request"

data: bytes = b""

for req, res in httpserver.log:
assert res.status_code == 200, "Response should have been 200"
data += req.data

return data


def test_simple_span(httpserver: HTTPServer) -> None:
with tracer_test(httpserver, "test_service") as tracer:
with tracer.trace("test"):
with tracer.trace("test", service="test_service"):
pass

data = tracer_assert(httpserver)
Expand All @@ -57,7 +64,7 @@ def test_attributes(httpserver: HTTPServer) -> None:
"test_attributes",
resource_attributes={"resource_attribute": "working"},
) as tracer:
with tracer.trace("test") as span:
with tracer.trace("test", service="test_attributes") as span:
span.set_tag("span_attribute", "also_working")

data = tracer_assert(httpserver)
Expand All @@ -69,8 +76,8 @@ def test_attributes(httpserver: HTTPServer) -> None:
def test_exceptions(httpserver: HTTPServer) -> None:
with tracer_test(httpserver, "test_exception") as tracer:
try:
with tracer.trace("test"):
assert False, "TestFailure"
with tracer.trace("test", service="test_exception"):
raise AssertionError("TestFailure")
except AssertionError:
pass

Expand All @@ -80,28 +87,36 @@ def test_exceptions(httpserver: HTTPServer) -> None:


def test_headers(httpserver: HTTPServer) -> None:
httpserver.expect_oneshot_request("/v1/trace").respond_with_data("OK")
httpserver.expect_request("/v1/trace").respond_with_data("OK")
httpserver.expect_request("/v1/trace/custom-header").respond_with_data("OK")

tracer = Tracer()
tracer.configure(
writer=OTELWriter(
service_name="test_headers",
exporter=Exporter(
host=httpserver.host,
port=f"{httpserver.port}",
path="/v1/trace",
path="/v1/trace/custom-header",
exporter_type=ExporterType.HTTP,
headers={"test-header": "works"},
),
resource_attributes={},
)
)

assert tracer.current_span() is None

with tracer.trace("test"):
pass

tracer.flush() # type: ignore[no-untyped-call]

assert len(httpserver.log), "We should have gotten 1 request"
req, _ = httpserver.log[0]
relevant_requests = [
entry for entry in httpserver.log if entry[0].path == "/v1/trace/custom-header"
]

assert len(relevant_requests) == 1, "We should have gotten 1 request"

req, _ = relevant_requests[0]

assert req.headers.get("test-header") == "works"
6 changes: 4 additions & 2 deletions troncos/contrib/asgi/logging/middleware.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import time
from typing import Any, Awaitable, Callable, Iterator, Mapping, MutableMapping, cast

import ddtrace

from ddtrace.trace import tracer

from python_ipware.python_ipware import IpWare

try:
Expand Down Expand Up @@ -147,7 +149,7 @@ async def wrapped_send(message: MutableMapping[str, Any]) -> None:
# To ensure that the trace information is always logged, we simply inject that
# information in here, and by doing so we do not have to care about the
# internals of the ASGI TraceMiddleware in ddtrace.
if dd_context := ddtrace.tracer.current_trace_context():
if dd_context := tracer.current_trace_context():
extra["trace_id"] = f"{dd_context.trace_id:x}"
extra["span_id"] = f"{dd_context.span_id:x}"

Expand Down
3 changes: 2 additions & 1 deletion troncos/contrib/structlog/processors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from ddtrace import tracer
from ddtrace.trace import tracer

from structlog.processors import LogfmtRenderer as LogFmt
from structlog.types import EventDict, WrappedLogger

Expand Down
6 changes: 4 additions & 2 deletions troncos/profiling/pyroscope.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import pyroscope
from ddtrace import config, tracer
from ddtrace import config
from ddtrace.trace import tracer

from ddtrace.internal.hostname import get_hostname


Expand Down Expand Up @@ -30,7 +32,7 @@ def start_py_spy_profiler(
profiler_tags.update(tags)

pyroscope.configure(
app_name=app_name,
application_name=app_name,
tags=profiler_tags,
server_address=server_address,
sample_rate=100,
Expand Down
Loading

0 comments on commit ae3bf2b

Please sign in to comment.