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

tracer: use noop exporter #1018

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
1 change: 0 additions & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ jobs:

- uses: actions/setup-python@v5
with:
cache: pip
python-version: '3.12'

- name: Resolve Karapace version
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ jobs:
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
cache: pip
python-version: ${{ matrix.python-version }}

- name: Set up Go
Expand Down
1 change: 1 addition & 0 deletions container/compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ services:
KARAPACE_KAFKA_RETRIABLE_ERRORS_SILENCED: true
KARAPACE_TAGS__APP: karapace-schema-registry
KARAPACE_TELEMETRY__OTEL_ENDPOINT_URL: http://opentelemetry-collector:4317
KARAPACE_TELEMETRY__OTEL_EXPORTER: OTLP
KARAPACE_TELEMETRY__RESOURCE_SERVICE_NAME: karapace-schema-registry
KARAPACE_TELEMETRY__RESOURCE_SERVICE_INSTANCE_ID: sr1
KARAPACE_TELEMETRY__RESOURCE_TELEMETRY_SDK_NAME: opentelemetry
Expand Down
8 changes: 8 additions & 0 deletions src/karapace/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from pydantic import BaseModel, ImportString
from pydantic_settings import BaseSettings, SettingsConfigDict

import enum
import logging
import os
import socket
Expand All @@ -27,8 +28,15 @@ class KarapaceTags(BaseModel):
app: str = "Karapace"


class KarapaceTelemetryOTelExporter(str, enum.Enum):
OTLP = "OTLP"
CONSOLE = "CONSOLE"
NOOP = "NOOP"


class KarapaceTelemetry(BaseModel):
otel_endpoint_url: str | None = None
otel_exporter: KarapaceTelemetryOTelExporter = KarapaceTelemetryOTelExporter.NOOP
resource_service_name: str = "karapace"
resource_service_instance_id: str = "karapace"
resource_telemetry_sdk_name: str = "opentelemetry"
Expand Down
5 changes: 1 addition & 4 deletions src/schema_registry/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,10 +380,7 @@ def highest_offset(self) -> int:
return max(self._highest_offset, self._offset_watcher.greatest_offset())

def ready(self) -> bool:
with self._tracer.get_tracer().start_as_current_span(
self._tracer.get_name_from_caller_with_class(self, self.ready)
) as span:
span.add_event("Acquiring ready lock")
with self._tracer.get_tracer().start_as_current_span(self._tracer.get_name_from_caller_with_class(self, self.ready)):
with self._ready_lock:
return self._ready

Expand Down
7 changes: 0 additions & 7 deletions src/schema_registry/routers/health.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class HealthCheck(BaseModel):


def set_health_status_tracing_attributes(health_check_span: Span, health_status: HealthStatus) -> None:
health_check_span.add_event("Setting health status tracing attributes")
health_check_span.set_attribute("schema_registry_ready", health_status.schema_registry_ready)
health_check_span.set_attribute("schema_registry_startup_time_sec", health_status.schema_registry_startup_time_sec)
health_check_span.set_attribute(
Expand All @@ -59,15 +58,11 @@ async def health(
with tracer.get_tracer().start_as_current_span("APIRouter: health_check") as health_check_span:
starttime = 0.0

health_check_span.add_event("Checking schema-reader is ready")
schema_reader_is_ready = schema_registry.schema_reader.ready()
if schema_reader_is_ready:
starttime = schema_registry.schema_reader.last_check - schema_registry.schema_reader.start_time

health_check_span.add_event("Getting schema-registry master-coordinator status")
cs = schema_registry.mc.get_coordinator_status()

health_check_span.add_event("Building health status response model")
health_status = HealthStatus(
schema_registry_ready=schema_reader_is_ready,
schema_registry_startup_time_sec=starttime,
Expand All @@ -85,9 +80,7 @@ async def health(
# resp["schema_registry_authfile_timestamp"] = self._auth.authfile_last_modified

if not await schema_registry.schema_reader.is_healthy():
health_check_span.add_event("Erroring because schema-reader is not healthy")
health_check_span.set_status(status=StatusCode.ERROR, description="Schema reader is not healthy")
raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE)

health_check_span.add_event("Returning health check response")
return HealthCheck(status=health_status, healthy=True)
44 changes: 37 additions & 7 deletions src/schema_registry/telemetry/tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@
See LICENSE for details
"""

from collections.abc import Callable
from collections.abc import Callable, Sequence
from dependency_injector.wiring import inject, Provide
from fastapi import Request, Response
from karapace.config import Config
from karapace.config import Config, KarapaceTelemetryOTelExporter
from karapace.container import KarapaceContainer
from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter, SimpleSpanProcessor, SpanProcessor
from opentelemetry.sdk.trace import ReadableSpan
from opentelemetry.sdk.trace.export import (
BatchSpanProcessor,
ConsoleSpanExporter,
SimpleSpanProcessor,
SpanExporter,
SpanExportResult,
SpanProcessor,
)
from opentelemetry.semconv.attributes import (
client_attributes as C,
http_attributes as H,
Expand All @@ -22,19 +30,41 @@
import inspect


class NOOPSpanExporter(SpanExporter):
"""Implementation of :class:`SpanExporter` that does nothing.

This class is intended to be used when tracing exporting to an OTel backend is disabled
and the ConsoleExporter is too verbose to be used.
"""

def export(self, _: Sequence[ReadableSpan]) -> SpanExportResult:
return SpanExportResult.SUCCESS

def force_flush(self, _: int = 0) -> bool:
return False


class Tracer:
@staticmethod
@inject
def get_tracer(config: Config = Provide[KarapaceContainer.config]) -> trace.Tracer:
return trace.get_tracer(f"{config.tags.app}.tracer")

@staticmethod
def get_span_exporter(config: Config) -> SpanExporter:
match config.telemetry.otel_exporter:
case KarapaceTelemetryOTelExporter.NOOP:
return NOOPSpanExporter()
case KarapaceTelemetryOTelExporter.CONSOLE:
return ConsoleSpanExporter()
case KarapaceTelemetryOTelExporter.OTLP:
return OTLPSpanExporter(endpoint=config.telemetry.otel_endpoint_url)

@staticmethod
@inject
def get_span_processor(config: Config = Provide[KarapaceContainer.config]) -> SpanProcessor:
if config.telemetry.otel_endpoint_url:
otlp_span_exporter = OTLPSpanExporter(endpoint=config.telemetry.otel_endpoint_url)
return BatchSpanProcessor(otlp_span_exporter)
return SimpleSpanProcessor(ConsoleSpanExporter())
processor = BatchSpanProcessor if config.telemetry.otel_endpoint_url else SimpleSpanProcessor
return processor(Tracer.get_span_exporter(config=config))

@staticmethod
def get_name_from_caller() -> str:
Expand Down
56 changes: 51 additions & 5 deletions tests/unit/schema_registry/telemetry/test_tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
from fastapi import Request, Response
from karapace.config import KarapaceTelemetry
from karapace.container import KarapaceContainer
from opentelemetry.sdk.trace.export import SpanProcessor
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SpanExporter, SpanProcessor
from opentelemetry.trace.span import Span
from schema_registry.telemetry.tracer import Tracer
from schema_registry.telemetry.tracer import NOOPSpanExporter, Tracer
from unittest.mock import call, MagicMock, patch


Expand All @@ -35,9 +35,55 @@ def test_function(self):
assert Test().test_function() == "Test.test_function()"


def test_get_span_exporter_noop(karapace_container: KarapaceContainer) -> None:
config = karapace_container.config().set_config_defaults(
new_config={
"telemetry": KarapaceTelemetry(
otel_endpoint_url="http://otel:4317",
otel_exporter="NOOP",
)
}
)
exporter: SpanExporter = Tracer.get_span_exporter(config=config)
assert isinstance(exporter, NOOPSpanExporter)


def test_get_span_exporter_console(karapace_container: KarapaceContainer) -> None:
config = karapace_container.config().set_config_defaults(
new_config={
"telemetry": KarapaceTelemetry(
otel_endpoint_url="http://otel:4317",
otel_exporter="CONSOLE",
)
}
)
exporter: SpanExporter = Tracer.get_span_exporter(config=config)
assert isinstance(exporter, ConsoleSpanExporter)


def test_get_span_exporter_otlp(karapace_container: KarapaceContainer) -> None:
config = karapace_container.config().set_config_defaults(
new_config={
"telemetry": KarapaceTelemetry(
otel_endpoint_url="http://otel:4317",
otel_exporter="OTLP",
)
}
)
with patch("schema_registry.telemetry.tracer.OTLPSpanExporter") as mock_otlp_exporter:
exporter: SpanExporter = Tracer.get_span_exporter(config=config)
mock_otlp_exporter.assert_called_once_with(endpoint="http://otel:4317")
assert exporter is mock_otlp_exporter.return_value


def test_get_span_processor_with_otel_endpoint(karapace_container: KarapaceContainer) -> None:
config = karapace_container.config().set_config_defaults(
new_config={"telemetry": KarapaceTelemetry(otel_endpoint_url="http://otel:4317")}
new_config={
"telemetry": KarapaceTelemetry(
otel_endpoint_url="http://otel:4317",
otel_exporter="OTLP",
)
}
)
with (
patch("schema_registry.telemetry.tracer.OTLPSpanExporter") as mock_otlp_exporter,
Expand All @@ -54,11 +100,11 @@ def test_get_span_processor_without_otel_endpoint(karapace_container: KarapaceCo
new_config={"telemetry": KarapaceTelemetry(otel_endpoint_url=None)}
)
with (
patch("schema_registry.telemetry.tracer.ConsoleSpanExporter") as mock_console_exporter,
patch("schema_registry.telemetry.tracer.SimpleSpanProcessor") as mock_simple_span_processor,
patch("schema_registry.telemetry.tracer.NOOPSpanExporter") as mock_noop_exporter,
):
processor: SpanProcessor = Tracer.get_span_processor(config=config)
mock_simple_span_processor.assert_called_once_with(mock_console_exporter.return_value)
mock_simple_span_processor.assert_called_once_with(mock_noop_exporter.return_value)
assert processor is mock_simple_span_processor.return_value


Expand Down
Loading