Skip to content

Commit

Permalink
add logger
Browse files Browse the repository at this point in the history
  • Loading branch information
songcser committed Oct 29, 2018
1 parent da6960d commit 146a8f0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
4 changes: 2 additions & 2 deletions sanicms/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
async def before_server_start(app, loop):
queue = asyncio.Queue()
app.queue = queue
loop.create_task(consume(queue, app.config['ZIPKIN_SERVER']))
loop.create_task(consume(queue, app))
loop.create_task(service_watcher(app, loop))
reporter = AioReporter(queue=queue)
tracer = BasicTracer(recorder=reporter)
Expand All @@ -57,7 +57,7 @@ async def after_server_start(app, loop):
service = ServiceManager(app.name, loop=loop, host=app.config['CONSUL_AGENT_HOST'])
await service.register_service(app.config['PORT'])
app.service = service


@app.listener('before_server_stop')
async def before_server_stop(app, loop):
Expand Down
8 changes: 8 additions & 0 deletions sanicms/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

from collections import defaultdict

logger = logging.getLogger('sanic')


class ServiceInfo(object):

Expand Down Expand Up @@ -56,10 +58,13 @@ async def register_service(self, host=None, port=None):
check = consul.Check.http(url, '10s')
await service.register(self.name, service_id=self.service_id,
address=address, port=port, check=check)
logger.info('register service: name: {}, service_id: {}, address: {}, port:{}'
.format(self.name, self.service_id, address, port))

async def deregister(self):
service = self.consul.agent.service
await service.deregister(self.service_id)
logger.info('deregister service: {}'.format(self.service_id))

async def discovery_service(self, service_name):
catalog = self.consul.catalog
Expand Down Expand Up @@ -94,10 +99,13 @@ async def check_service(self, service_name):

async def service_watcher(app, loop):
service = ServiceManager(loop=loop, host=app.config['CONSUL_AGENT_HOST'])
logger.info('service watcher...')
app.services = defaultdict(list)
while True:
services = await service.discovery_services()
for name in services[1].keys():
if 'consul' == name:
continue
result = await service.discovery_service(name)
checks = await service.check_service(name)
for res in result:
Expand Down
6 changes: 4 additions & 2 deletions sanicms/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import json
import logging
import asyncio
import aiohttp
Expand Down Expand Up @@ -36,7 +37,8 @@ def id_to_hex(id):
return None
return '{0:x}'.format(id)

async def consume(q, zs):
async def consume(q, app):
zs = app.config['ZIPKIN_SERVER']
async with aiohttp.ClientSession() as session:
while True:
# wait for an item from the producer
Expand Down Expand Up @@ -112,7 +114,7 @@ def default(self, request, exception):
span.set_tag('http.status_code', str(exception.status_code))
span.set_tag('error.kind', exception.__class__.__name__)
span.set_tag('error.msg', exception.message)
return json(data, status=exception.status_code)
return json.dumps(data, status=exception.status_code)
return super().default(request, exception)

def before_request(request):
Expand Down

0 comments on commit 146a8f0

Please sign in to comment.