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

feat: Support ddtrace version 3 #366

Merged
merged 1 commit into from
Feb 28, 2025
Merged
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
158 changes: 64 additions & 94 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ documentation = "https://github.com/kolonialno/troncos"
keywords = ["logs", "traces", "opentelemetry"]

[tool.poetry.dependencies]
ddtrace = ">=2,<3"
ddtrace = ">=2,<4"
opentelemetry-exporter-otlp-proto-grpc = { version = ">=1.19,<2", optional = true }
opentelemetry-exporter-otlp-proto-http = ">=1.19,<2"
opentelemetry-sdk = ">=1.19,<2"
Expand Down
7 changes: 3 additions & 4 deletions tests/tracing/test_writer.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from contextlib import contextmanager
from typing import Any, Generator

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

from troncos.tracing._exporter import Exporter, ExporterType
Expand All @@ -19,7 +18,7 @@ def tracer_test(

assert tracer.current_span() is None

tracer.configure(
tracer._configure(
writer=OTELWriter(
service_name=service_name,
exporter=Exporter(
Expand Down Expand Up @@ -90,7 +89,7 @@ def test_headers(httpserver: HTTPServer) -> None:
httpserver.expect_request("/v1/trace").respond_with_data("OK")
httpserver.expect_request("/v1/trace/custom-header").respond_with_data("OK")

tracer.configure(
tracer._configure(
writer=OTELWriter(
service_name="test_headers",
exporter=Exporter(
Expand Down
27 changes: 21 additions & 6 deletions troncos/tracing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,40 @@
from ._exporter import Exporter, ExporterType
from ._writer import OTELWriter

__all__ = ["Exporter", "ExporterType"]
__all__ = ["Exporter", "ExporterType", "configure_tracer", "create_trace_writer"]


def configure_tracer(
def create_trace_writer(
*,
service_name: str,
exporter: Exporter | None = None,
resource_attributes: dict[str, Any] | None = None,
) -> None:
"""Configure ddtrace to write traces to the otel tracing backend."""
) -> OTELWriter:
"""Create a trace writer that writes traces to the otel tracing backend."""

if exporter is None:
exporter = Exporter()

# Initialize our custom writer used to handle ddtrace spans.
writer = OTELWriter(
return OTELWriter(
service_name=service_name,
exporter=exporter,
resource_attributes=resource_attributes,
)


def configure_tracer(
*,
service_name: str,
exporter: Exporter | None = None,
resource_attributes: dict[str, Any] | None = None,
) -> None:
"""Configure ddtrace to write traces to the otel tracing backend."""

writer = create_trace_writer(
service_name=service_name,
exporter=exporter,
resource_attributes=resource_attributes,
)

tracer.configure(writer=writer)
tracer._configure(writer=writer)
3 changes: 2 additions & 1 deletion troncos/tracing/_span.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Any

from ddtrace import Span as DDSpan, constants, ext
from ddtrace import constants, ext
from ddtrace.trace import Span as DDSpan
from opentelemetry.attributes import BoundedAttributes # type: ignore
from opentelemetry.sdk.resources import SERVICE_NAME, Resource
from opentelemetry.sdk.trace import (
Expand Down
2 changes: 1 addition & 1 deletion troncos/tracing/_writer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Any

from ddtrace import Span
from ddtrace.trace import Span
from ddtrace.internal.writer.writer import TraceWriter
from opentelemetry.sdk.resources import Resource

Expand Down
6 changes: 3 additions & 3 deletions troncos/tracing/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from functools import wraps
from types import FunctionType
from typing import Awaitable, Any, Callable, ParamSpec, Type, TypeVar, cast, overload
import ddtrace
from ddtrace.trace import tracer

from ddtrace.trace import tracer, Span


_TRACE_IGNORE_ATTR = "_trace_ignore"
Expand All @@ -26,7 +26,7 @@ def trace_block(
service: str | None = None,
span_type: str | None = None,
attributes: dict[str | bytes, Any] | None = None,
) -> Generator[ddtrace.Span, None, None]:
) -> Generator[Span, None, None]:
"""
Trace a code block using a with statement. Example:

Expand Down
Loading