Skip to content

Commit

Permalink
Merge pull request #314 from lsst-sqre/tickets/DM-47011
Browse files Browse the repository at this point in the history
DM-47011: Allow extra values to disabled metrics config
  • Loading branch information
rra authored Oct 21, 2024
2 parents b4f803c + 94c7547 commit afecc3e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
3 changes: 3 additions & 0 deletions changelog.d/20241021_091054_rra_DM_47011.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Bug fixes

- Allow and ignore extra attributes to `MetricsConfiguration` when metrics are disabled. This allows passing a partial metrics configuration even when they are disabled, which simplifies the structure of Phalanx configurations based on dumping the Helm values into a YAML configuraiton file.
14 changes: 8 additions & 6 deletions safir/src/safir/metrics/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,6 @@ class BaseMetricsConfiguration(BaseSettings, ABC):
title="Events configuration",
)

model_config = SettingsConfigDict(
alias_generator=to_camel,
extra="forbid",
populate_by_name=True,
)

@abstractmethod
def make_manager(self, logger: BoundLogger | None = None) -> EventManager:
"""Construct an EventManager.
Expand Down Expand Up @@ -125,6 +119,8 @@ class DisabledMetricsConfiguration(BaseMetricsConfiguration):
validation_alias=AliasChoices("enabled", "METRICS_ENABLED"),
)

model_config = SettingsConfigDict(extra="ignore", populate_by_name=True)

def make_manager(
self, logger: BoundLogger | None = None
) -> NoopEventManager:
Expand Down Expand Up @@ -160,6 +156,12 @@ class KafkaMetricsConfiguration(BaseMetricsConfiguration):
title="Kafka schema manager settings",
)

model_config = SettingsConfigDict(
alias_generator=to_camel,
extra="forbid",
populate_by_name=True,
)

def make_manager(
self, logger: BoundLogger | None = None
) -> KafkaEventManager:
Expand Down
17 changes: 17 additions & 0 deletions safir/tests/metrics/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,23 @@ def test_disabled(monkeypatch: pytest.MonkeyPatch) -> None:
assert isinstance(manager, NoopEventManager)


def test_disabled_extra() -> None:
config = Config.model_validate(
{
"metrics": {
"enabled": False,
"application": "example",
"events": {"topicPrefix": "lsst.square.metrics.events"},
"schemaManager": {
"registryUrl": "https://example.com",
"suffix": "",
},
}
}
)
assert isinstance(config.metrics, DisabledMetricsConfiguration)


@pytest.mark.asyncio
async def test_kafka(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("METRICS_APPLICATION", "test")
Expand Down

0 comments on commit afecc3e

Please sign in to comment.