Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Deduplicate config #20

Merged
merged 1 commit into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ Unreleased

*

[0.3.0] - 2022-08-10
~~~~~~~~~~~~~~~~~~~~

Updated
_______

* Moved configuration onto separate file.
* Updated configuration settings to have EVENT_BUS_KAFKA prefix.

[0.2.0] - 2022-08-09
~~~~~~~~~~~~~~~~~~~~

Expand Down
2 changes: 1 addition & 1 deletion edx_event_bus_kafka/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
Kafka implementation for Open edX event bus.
"""

__version__ = '0.2.0'
__version__ = '0.3.0'
54 changes: 54 additions & 0 deletions edx_event_bus_kafka/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""
Configuration loading and validation.
"""

import warnings
from typing import Optional

from confluent_kafka.schema_registry import SchemaRegistryClient
from django.conf import settings


def create_schema_registry_client() -> Optional[SchemaRegistryClient]:
"""
Create a schema registry client from common settings.
"""
url = getattr(settings, 'EVENT_BUS_KAFKA_SCHEMA_REGISTRY_URL', None)
if url is None:
warnings.warn("Cannot configure event-bus-kafka: Missing setting EVENT_BUS_KAFKA_SCHEMA_REGISTRY_URL")
return None

key = getattr(settings, 'EVENT_BUS_KAFKA_SCHEMA_REGISTRY_API_KEY', '')
secret = getattr(settings, 'EVENT_BUS_KAFKA_SCHEMA_REGISTRY_API_SECRET', '')

return SchemaRegistryClient({
'url': url,
'basic.auth.user.info': f"{key}:{secret}",
})


def load_common_settings() -> Optional[dict]:
"""
Load common settings, a base for either producer or consumer configuration.
"""
bootstrap_servers = getattr(settings, 'EVENT_BUS_KAFKA_BOOTSTRAP_SERVERS', None)
if bootstrap_servers is None:
warnings.warn("Cannot configure event-bus-kafka: Missing setting EVENT_BUS_KAFKA_BOOTSTRAP_SERVERS")
return None

base_settings = {
'bootstrap.servers': bootstrap_servers,
}

key = getattr(base_settings, 'EVENT_BUS_KAFKA_API_KEY', None)
secret = getattr(base_settings, 'EVENT_BUS_KAFKA_API_SECRET', None)

if key and secret:
base_settings.update({
'sasl.mechanism': 'PLAIN',
'security.protocol': 'SASL_SSL',
'sasl.username': key,
'sasl.password': secret,
})

return base_settings
32 changes: 6 additions & 26 deletions edx_event_bus_kafka/publishing/event_producer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@

import json
import logging
import warnings
from functools import lru_cache
from typing import Any, List, Optional

from confluent_kafka import SerializingProducer
from confluent_kafka.schema_registry import SchemaRegistryClient
from confluent_kafka.schema_registry.avro import AvroSerializer
from django.conf import settings
from openedx_events.event_bus.avro.serializer import AvroSignalSerializer
from openedx_events.tooling import OpenEdxPublicSignal

from edx_event_bus_kafka.config import create_schema_registry_client, load_common_settings

logger = logging.getLogger(__name__)

# CloudEvent standard name for the event type header, see
Expand Down Expand Up @@ -136,33 +135,14 @@ def get_producer_for_signal(signal: OpenEdxPublicSignal, event_key_field: str) -
remote-config (and in particular does not result in mixed cache/uncached configuration).
This complexity is being deferred until this becomes a performance issue.
"""
if schema_registry_url := getattr(settings, 'SCHEMA_REGISTRY_URL', None):
schema_registry_config = {
'url': schema_registry_url,
'basic.auth.user.info': f"{getattr(settings, 'SCHEMA_REGISTRY_API_KEY', '')}"
f":{getattr(settings, 'SCHEMA_REGISTRY_API_SECRET', '')}",
}
else:
warnings.warn("Cannot configure event-bus-kafka: Missing setting SCHEMA_REGISTRY_URL")
schema_registry_client = create_schema_registry_client()
if schema_registry_client is None:
return None

if bootstrap_servers := getattr(settings, 'KAFKA_BOOTSTRAP_SERVERS', None):
producer_settings = {
'bootstrap.servers': bootstrap_servers,
}
else:
warnings.warn("Cannot configure event-bus-kafka: Missing setting KAFKA_BOOTSTRAP_SERVERS")
producer_settings = load_common_settings()
if producer_settings is None:
return None

if getattr(settings, 'KAFKA_API_KEY', None) and getattr(settings, 'KAFKA_API_SECRET', None):
producer_settings.update({
'sasl.mechanism': 'PLAIN',
'security.protocol': 'SASL_SSL',
'sasl.username': settings.KAFKA_API_KEY,
'sasl.password': settings.KAFKA_API_SECRET,
})

schema_registry_client = SchemaRegistryClient(schema_registry_config)
signal_serializer = get_serializer(signal)

timmc-edx marked this conversation as resolved.
Show resolved Hide resolved
def inner_to_dict(event_data, ctx=None): # pylint: disable=unused-argument
Expand Down
8 changes: 4 additions & 4 deletions edx_event_bus_kafka/publishing/test_event_producer.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ def test_get_producer_for_signal_configured(self):
"""Creation succeeds when all settings are present."""
signal = openedx_events.learning.signals.SESSION_LOGIN_COMPLETED
with override_settings(
SCHEMA_REGISTRY_URL='http://localhost:12345',
SCHEMA_REGISTRY_API_KEY='some_key',
SCHEMA_REGISTRY_API_SECRET='some_secret',
KAFKA_BOOTSTRAP_SERVERS='http://localhost:54321',
EVENT_BUS_KAFKA_SCHEMA_REGISTRY_URL='http://localhost:12345',
EVENT_BUS_KAFKA_SCHEMA_REGISTRY_API_KEY='some_key',
EVENT_BUS_KAFKA_SCHEMA_REGISTRY_API_SECRET='some_secret',
EVENT_BUS_KAFKA_BOOTSTRAP_SERVERS='http://localhost:54321',
# include these just to maximize code coverage
KAFKA_API_KEY='some_other_key',
KAFKA_API_SECRET='some_other_secret',
Expand Down