...answered my own question - looks like dependencies are injected into
services, but not each other. Naively trying to use Publisher() in my
Logger DP leads to:
`TypeError: bind() missing 1 required positional argument: 'attr_name'`
Right, DependencyProviders are specifically about injecting dependencies
into Nameko services -- you can't put a DP inside another DP.
So would I be better off importing ampqlib or somesuch and using it
directly in the DP?
Actually if you look at the nameko.messaging.Publisher DP you'll see that
(apart from inheriting from DependencyProvider and implementing
get_dependency()) it doesn't do very much except create an instance of
nameko.amqp.publish.Publisher -- this is a utility class that makes it
easier to publish messages to RabbitMQ. I would use this, rather than
amqplib or kombu directly.
When I directed you to nameko-tracer I forgot that the critical part of
actually publishing messages to AMQP was spun into another (currently
closed-source) library. If you choose to use the tracer, you'll want to
plug the AMQP publishing part in as a Python logging handler.
Here's a skeleton implementation:
import logging
from nameko.amqp.publish import Publisher
logger = logging.getLogger(__name__)
class PublisherHandler(logging.Handler):
""" Handler for publishing trace messages to RabbitMQ
"""
def __init__(
self, amqp_uri, exchange_name, routing_key,
serializer=None, content_type=None
):
self.publisher = Publisher(
amqp_uri,
exchange=exchange_name,
routing_key=routing_key,
serializer=serializer,
content_type=content_type
)
super(PublisherHandler, self).__init__()
def emit(self, log_record):
try:
self.publisher.publish(self.format(log_record))
except Exception:
self.handleError(log_record)
Use by registering it as a handler using the normal logging configuration:
LOGGING:
formatters:
tracer:
(): yourib.logging.formatters.GELFFormatter
handlers:
tracer:
formatter: tracer
(): yourlib.logging.handlers.PublisherHandler
amqp_uri: <GELF_AMQP_URI>
exchange_name: <GELF_EXCHANGE_NAME>
routing_key: <GELF_ROUTING_KEY>
serializer: json
content_type: application/json
loggers:
nameko_tracer:
level: INFO
handlers:
- tracer
I would personally do it this way, implementing the GELF format as a
standard logging formatter too. If there's anything required by Graylog
which is not already collected by the tracer, I'm sure a PR to
nameko-tracer would be gratefully received.
···
On Saturday, 13 January 2018 22:27:39 UTC, Chris Platts wrote: