Skip to content

Commit

Permalink
tracer: added noop exporter support
Browse files Browse the repository at this point in the history
  • Loading branch information
nosahama committed Jan 7, 2025
1 parent 36cc6c2 commit 38db86c
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 14 deletions.
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
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

0 comments on commit 38db86c

Please sign in to comment.