metrics_configuration_factory

safir.metrics.metrics_configuration_factory()

Choose an appropriate metrics configuration based on the environment.

This function is intended for use as the argument to the default_factory parameter to pydantic.Field for the application metrics configuration. It selects an appropriate metrics configuration based on which configuration class can be instantiated from the available environment variables. This is not necessary if the application configuration comes from a source such as YAML that specifies settings for the metrics configuration, since in that case Pydantic will correctly instantiate the correct settings model.

Returns:

An appropriate metrics configuration.

Return type:

BaseMetricsConfiguration

Raises:

pydantic.ValidationError – Raised if none of the possible configurations have their required variables set.

Examples

from pydantic_settings import BaseSettings
from safir.metrics import (
    MetricsConfiguration,
    metrics_configuration_factory,
)

class Config(BaseSettings):
    metrics: MetricsConfiguration = Field(
        default_factory=metrics_configuration_factory,
        title="Metrics configuration",
    )


config = Config()