-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
91 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
""" | ||
Copyright (c) 2024 Aiven Ltd | ||
See LICENSE for details | ||
""" | ||
|
||
from dependency_injector.wiring import inject, Provide | ||
from karapace.config import Config | ||
from karapace.container import KarapaceContainer | ||
from opentelemetry import metrics | ||
from opentelemetry.exporter.otlp.proto.grpc.metric_exporter import OTLPMetricExporter | ||
from opentelemetry.sdk.metrics.export import ConsoleMetricExporter, MetricReader, PeriodicExportingMetricReader | ||
from typing import Final | ||
|
||
|
||
class Meter: | ||
START_TIME_KEY: Final = "start_time" | ||
|
||
@staticmethod | ||
@inject | ||
def get_meter(config: Config = Provide[KarapaceContainer.config]) -> metrics.Meter: | ||
return metrics.get_meter_provider().get_meter(f"{config.tags.app}.meter") | ||
|
||
@staticmethod | ||
@inject | ||
def get_metric_reader(config: Config = Provide[KarapaceContainer.config]) -> MetricReader: | ||
exporter = ConsoleMetricExporter() | ||
if config.telemetry.otel_endpoint_url: | ||
exporter = OTLPMetricExporter(endpoint=config.telemetry.otel_endpoint_url) | ||
return PeriodicExportingMetricReader( | ||
exporter=exporter, | ||
export_interval_millis=config.telemetry.metrics_export_interval_milliseconds, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
""" | ||
schema_registry - telemetry meter tests | ||
Copyright (c) 2024 Aiven Ltd | ||
See LICENSE for details | ||
""" | ||
|
||
from karapace.container import KarapaceContainer | ||
from schema_registry.telemetry.meter import Meter | ||
from unittest.mock import patch | ||
|
||
|
||
def test_meter(karapace_container: KarapaceContainer): | ||
with patch("schema_registry.telemetry.meter.metrics") as mock_metrics: | ||
Meter.get_meter(config=karapace_container.config()) | ||
mock_metrics.get_meter_provider.return_value.get_meter.assert_called_once_with("Karapace.meter") | ||
|
||
|
||
def test_get_metric_reader_with_otel_endpoint(karapace_container: KarapaceContainer) -> None: | ||
with ( | ||
patch("schema_registry.telemetry.meter.OTLPMetricExporter") as mock_otlp_exporter, | ||
patch("schema_registry.telemetry.meter.PeriodicExportingMetricReader") as mock_periodic_exporting_metric_reader, | ||
): | ||
karapace_container.config().telemetry.otel_endpoint_url = "http://otel:4317" | ||
reader = Meter.get_metric_reader(config=karapace_container.config()) | ||
mock_otlp_exporter.assert_called_once_with(endpoint="http://otel:4317") | ||
mock_periodic_exporting_metric_reader.assert_called_once_with( | ||
exporter=mock_otlp_exporter.return_value, | ||
export_interval_millis=10000, | ||
) | ||
assert reader is mock_periodic_exporting_metric_reader.return_value | ||
|
||
|
||
def test_get_metric_reader_without_otel_endpoint(karapace_container: KarapaceContainer) -> None: | ||
with ( | ||
patch("schema_registry.telemetry.meter.ConsoleMetricExporter") as mock_console_exporter, | ||
patch("schema_registry.telemetry.meter.PeriodicExportingMetricReader") as mock_periodic_exporting_metric_reader, | ||
): | ||
reader = Meter.get_metric_reader(config=karapace_container.config()) | ||
mock_console_exporter.assert_called_once() | ||
mock_periodic_exporting_metric_reader.assert_called_once_with( | ||
exporter=mock_console_exporter.return_value, | ||
export_interval_millis=10000, | ||
) | ||
assert reader is mock_periodic_exporting_metric_reader.return_value |