Skip to content

Commit

Permalink
Merge pull request #40 from DMS-SMS/develop
Browse files Browse the repository at this point in the history
기본적인 Opentracing 작성
  • Loading branch information
mallycrip authored Oct 7, 2020
2 parents 6878111 + 155864f commit 49730f3
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 30 deletions.
33 changes: 21 additions & 12 deletions application/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,31 @@
from concurrent import futures

from infrastructure.extension import Base, engine
from infrastructure.config.grpc_config import gRPCAppConfig
from application.servicers import register_outing_servicers

def register_db():
Base.metadata.create_all(engine)

def register_servicers(app):
register_outing_servicers(app)
class gRPCApplication:
def __init__(self, config, consul):
self._config = config
self._app = grpc.server(futures.ThreadPoolExecutor(max_workers=config.max_workers))
self._consul = consul

self._app.add_insecure_port(config.address)
self.register_db()
self.register_servicers()

def create_app():
config = gRPCAppConfig()
app = grpc.server(futures.ThreadPoolExecutor(max_workers=config.max_workers))
app.add_insecure_port(config.address)
def register_db(self):
Base.metadata.create_all(engine)

register_db()
register_servicers(app)
def register_servicers(self):
register_outing_servicers(self._app)

return app
def serve(self):
try:
self._app.start()
self._consul.register_consul()
print("* gRPC Application is served")
self._app.wait_for_termination()
except:
self._consul.deregister_consul()
print("* gRPC Application is down")
27 changes: 27 additions & 0 deletions application/decorator/metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from functools import wraps
from infrastructure.extension import open_tracing
from infrastructure.open_tracing import openTracing



def jagger_enable(fn):
@wraps(fn)
def wrapper(*args, **kwargs):
flag = 0
context = args[2]
parents_span = str()
x_request_id = str()
for k, v in context.invocation_metadata():
if flag == 2: break
if k == "x-request-id":
x_request_id = v
flag += 1
if k == "span-context":
parents_span = v
flag += 1
else: raise Exception

open_tracing_service = openTracing(open_tracing.tracer, parents_span, x_request_id)
return open_tracing_service.start_active_span(fn, *args, **kwargs)

return wrapper
3 changes: 3 additions & 0 deletions application/servicers/outing_parents_servicer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from infrastructure.repository import OutingRepositoryImpl

from proto.python.outing import outing_parents_pb2_grpc
from application.decorator.metadata import jagger_enable


class ParentsOutingServicer(outing_parents_pb2_grpc.OutingParentsServicer):
Expand All @@ -18,8 +19,10 @@ def __init__(self):
)
)

@jagger_enable
def ApproveOutingByOCode(self, request, context):
return self.service.approve_outing(request)

@jagger_enable
def RejectOutingByOCode(self, request, context):
return self.service.reject_outing(request)
8 changes: 8 additions & 0 deletions application/servicers/outing_student_servicer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from application.service.student_outing_service import StudentOutingService
from application.decorator.metadata import jagger_enable

from domain.usecase.create_outing_usecase import CreateOutingUseCase
from domain.usecase.finish_go_out_usecase import FinishGoOutUseCase
from domain.usecase.get_card_usecase import GetCardUseCase
Expand Down Expand Up @@ -47,20 +49,26 @@ def __init__(self):
)
)

@jagger_enable
def CreateOuting(self, request, context):
return self.service.create_outing(request)

@jagger_enable
def GetCardAboutOuting(self, request, context):
return self.service.get_card_about_outing(request)

@jagger_enable
def GetOutingInform(self, request, context):
return self.service.get_outing_inform(request)

@jagger_enable
def GetStudentOutings(self, request, context):
return self.service.get_student_outings(request)

@jagger_enable
def GoOut(self, request, context):
return self.service.go_out(request)

@jagger_enable
def FinishGoOut(self, request, context):
return self.service.finish_go_out(request)
5 changes: 5 additions & 0 deletions application/servicers/outing_teacher_servicer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from proto.python.outing import outing_teacher_pb2_grpc

from application.service.teacher_outing_service import TeacherOutingService
from application.decorator.metadata import jagger_enable

from infrastructure.repository import OutingRepositoryImpl
from infrastructure.service.outing_domain_service_impl import OutingDomainServiceImpl
Expand All @@ -31,14 +32,18 @@ def __init__(self):
)
)

@jagger_enable
def GetOutingWithFilter(self, request, context):
return self.service.get_outings_with_filter(request)

@jagger_enable
def ApproveOuting(self, request, context):
return self.service.approve_outing(request)

@jagger_enable
def RejectOuting(self, request, context):
return self.service.reject_outing(request)

@jagger_enable
def CertifyOuting(self, request, context):
return self.service.certify_outing(request)
5 changes: 4 additions & 1 deletion infrastructure/extension/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@
db_session, Base, engine = register_db()

from infrastructure.extension.redis_register import register_redis
redis = register_redis()
redis = register_redis()

from infrastructure.open_tracing import reigsterOpenTracing
open_tracing = reigsterOpenTracing()
44 changes: 44 additions & 0 deletions infrastructure/open_tracing/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from functools import wraps
from jaeger_client import Config
from jaeger_client.span_context import SpanContext


class reigsterOpenTracing:
def __init__(self):
self._config = Config(
config={ # usually read from some yaml config
'logging': True,
},
service_name='outing-service',
validate=True,
)
self._tracer = self._config.initialize_tracer()

@property
def tracer(self):
return self._tracer


class openTracing:
def __init__(self, tracer, span_context, x_request_id):
self._split_span_context = span_context.split(":")
self._trace_id = int(self._split_span_context[0], 16)
self._span_id =int(self._split_span_context[1], 16)
self._span_parent_id = int(self._split_span_context[2], 16)
self._span_flag = int(self._split_span_context[3], 16)
self._span_context = SpanContext(
self._trace_id,
self._span_id,
self._span_parent_id,
self._span_flag
)
self._tracer = tracer
self._x_request_id = x_request_id


def start_active_span(self, fn, *args, **kwargs):
with self._tracer.start_active_span("OutingService", child_of=self._span_context) as span:
self._tracer.active_span.set_tag("X-Request-Id", self._x_request_id)
return_value = fn(args[0], args[1], args[2])
span.close()
return return_value
11 changes: 8 additions & 3 deletions infrastructure/service/auth_service.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import grpc

from infrastructure.extension import consul, open_tracing
from proto.python.auth import auth_student_pb2, auth_student_pb2_grpc


class AuthService:
def __init__(self):
self.address = "127.0.0.1:10059"
# TODO
self.address = "127.0.0.1:10071"
# self.address = consul.get_address("DMS.SMS.v1.service.auth")
self.channel = grpc.insecure_channel(self.address)
self.stub = auth_student_pb2_grpc.AuthStudentStub(self.channel)
self.metadata = (("x-request-id", "f9ed4675f1c53513c61a3b3b4e25b4c0"),
("span-context", "a:a:0:0"))
self.metadata: str

def get_student_inform(self, uuid, student_uuid):
self.metadata = (("x-request-id", "f9ed4675f1c53513c61a3b3b4e25b4c0"),
("span-context", str(open_tracing.tracer.active_span).split()[0]))

return self.stub.GetStudentInformWithUUID(auth_student_pb2.GetStudentInformWithUUIDRequest(
UUID=uuid,
StudentUUID=student_uuid
Expand Down
19 changes: 5 additions & 14 deletions run.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
from application import create_app
from infrastructure.extension import consul


def serve(app):
try:
app.start()
print("* Presentation is served")
consul.register_consul()
app.wait_for_termination()
except:
consul.deregister_consul()
from application import gRPCApplication

from infrastructure.config.grpc_config import gRPCAppConfig
from infrastructure.extension import consul

if __name__ == "__main__":
app = create_app()
serve(app)
app = gRPCApplication(gRPCAppConfig(), consul)
app.serve()

0 comments on commit 49730f3

Please sign in to comment.