EventManager

class safir.metrics.EventManager(topic, logger=None)

Bases: object

Interface for a client for publishing application metrics events.

This interface is implemented by the fully-functional KafkaEventManager and the disabled NoopEventManager. The latter is used when events publishing is disabled so that the rest of the application code doesn’t need to change.

Parameters:
  • topic (str) – Kafka topic to which events will be published.

  • logger (BoundLogger | None, default: None) – Logger to use. The safir.metrics logger will be used if none is provided.

topic

Kafka topic to which events will be published.

logger

Logger that subclasses should use. This should not be used outside of subclasses of this class.

Methods Summary

aclose()

Shut down any internal state or managed clients.

build_publisher_for_model(model)

Implementation-specific construction of the event publisher.

create_publisher(name, payload_model)

Create an EventPublisher for a type of event.

initialize()

Initialize any internal state or managed clients.

Methods Documentation

async aclose()

Shut down any internal state or managed clients.

Return type:

None

abstract async build_publisher_for_model(model)

Implementation-specific construction of the event publisher.

This class must be overridden by child classes to do the implementation-specific work of constructing an appropriate child instance of EventPublisher.

Parameters:

model (type[TypeVar(P, bound= EventPayload)]) – Enriched and configured model representing the event that will be published.

Returns:

An appropriate event publisher implementation instance.

Return type:

EventPublisher

async create_publisher(name, payload_model)

Create an EventPublisher for a type of event.

The schema is registered with the schema manager when this method is called.

Parameters:
  • name (str) – Name of the event. This will be used as the name of the Avro schema and record and the name of the event in the event storage backend.

  • payload_model (type[TypeVar(P, bound= EventPayload)]) – A type derived from EventPayload. This defines the type of models that will be passed into the publish method of the resulting publisher. The events as published will include the information in this model plus the fields of EventMetadata.

Returns:

A publisher for a type of event matching the payload_model.

Return type:

EventPublisher

Raises:
async initialize()

Initialize any internal state or managed clients.

This method must be called before calling create_publisher.

Return type:

None