florist.api.monitoring.metrics module

Classes for the instrumentation of metrics reporting from clients and servers.

class DateTimeEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]

Bases: JSONEncoder

Converts a datetime object to string in order to make json encoding easier.

default(o)[source]

Return string of datetime if datetime object is passed else return result of the default encoder method.

Parameters:

o (Any) – Object to encode.

Return type:

Any

class RedisMetricsReporter(host, port, run_id=None)[source]

Bases: BaseReporter

Save the metrics to a Redis instance while it records them.

Lazily instantiates a Redis connection when the first metrics are recorded.

__init__(host, port, run_id=None)[source]

Init an instance of RedisMetricsReporter.

Parameters:
  • host (str) – (str) The host address where the Redis instance is running.

  • port (str) – (str) The port where the Redis instance is running on the host.

  • run_id (Optional[str]) – (Optional[str]) the identifier for the run which these metrics are from. It will be used as the name of the object in Redis. Optional, default is a random UUID.

dump()[source]

Dump the current metrics to Redis under the run_id name.

Will instantiate a Redis connection if it’s the first time it runs for this instance.

Return type:

None

initialize(**kwargs)[source]

Initialize RedisMetricReporter with run_id and set initialized to True.

Parameters:

kwargs (Any) – (Any) The keyword arguments required to initialize the Reporter.

Return type:

None

report(data, round=None, epoch=None, step=None)[source]

Send data to the reporter.

The report method is called by the client/server at frequent intervals (ie step, epoch, round) and sometimes outside of a FL round (for high level summary data). The json reporter is hardcoded to report at the ‘round’ level and therefore ignores calls to the report method made every epoch or every step.

Return type:

None

Args:

data (dict): The data to maybe report from the server or client. round (int | None, optional): The current FL round. If None, this indicates that the method was called

outside of a round (e.g. for summary information). Defaults to None.

epoch (int | None, optional): The current epoch. If None then this method was not called within the scope

of an epoch. Defaults to None.

step (int | None, optional): The current step (total). If None then this method was called outside the

scope of a training or evaluation step (eg. at the end of an epoch or round) Defaults to None.

get_from_redis(name, redis_host, redis_port)[source]

Get the contents of what’s saved on Redis under the name.

Parameters:
  • name (str) – (str) the name to look into Redis.

  • redis_host (str) – (str) the hostname of the redis instance.

  • redis_port (str) – (str) the port of the redis instance.

Return type:

Optional[Dict[str, Any]]

Returns:

(Optional[Dict[str, Any]]) the contents under the name.

get_subscriber(channel, redis_host, redis_port)[source]

Return a PubSub instance with a subscription to the given channel.

Parameters:
  • channel (str) – (str) The name of the channel to add a subscriber to.

  • redis_host (str) – (str) the hostname of the redis instance.

  • redis_port (str) – (str) the port of the redis instance.

Return type:

PubSub

Returns:

(redis.client.PubSub) The PubSub instance subscribed to the given channel.

wait_for_metric(uuid, metric, redis_host, redis_port, logger, max_retries=20, seconds_to_sleep_between_retries=1)[source]

Check metrics on Redis under the given UUID and wait until it appears.

If the metrics are not there yet, it will retry up to max_retries times, sleeping an amount of seconds_to_sleep_between_retries between them.

Parameters:
  • uuid (str) – (str) The UUID to pull the metrics from Redis.

  • metric (str) – (str) The metric to look for.

  • redis_host (str) – (str) The hostname of the Redis instance the metrics are being reported to.

  • redis_port (str) – (str) The port of the Redis instance the metrics are being reported to.

  • logger (Logger) – (logging.Logger) A logger instance to write logs to.

  • max_retries (int) – (int) The maximum number of retries. Optional, default is 20.

  • seconds_to_sleep_between_retries (int) – (int) The amount of seconds to sleep between retries. Optional, default is 1.

Raises:

Exception – If it retries max_retries times and the right metrics have not been found.

Return type:

None