|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: UTF-8 -*- |
| 3 | + |
| 4 | +# ------------------------------------------------------------------------------ |
| 5 | +# Copyright 2020. NAVER Corp. - |
| 6 | +# - |
| 7 | +# Licensed under the Apache License, Version 2.0 (the "License"); - |
| 8 | +# you may not use this file except in compliance with the License. - |
| 9 | +# You may obtain a copy of the License at - |
| 10 | +# - |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 - |
| 12 | +# - |
| 13 | +# Unless required by applicable law or agreed to in writing, software - |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, - |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - |
| 16 | +# See the License for the specific language governing permissions and - |
| 17 | +# limitations under the License. - |
| 18 | +# ------------------------------------------------------------------------------ |
| 19 | + |
| 20 | +# Created by eeliu at 11/13/2024 |
| 21 | + |
| 22 | +from pinpointPy import Common, pinpoint, Defines |
| 23 | +import kombu |
| 24 | + |
| 25 | + |
| 26 | +class KombuPlugin(Common.PinTrace): |
| 27 | + |
| 28 | + def __init__(self, name): |
| 29 | + super().__init__(name) |
| 30 | + |
| 31 | + def get_host(self, *args, **kwargs): |
| 32 | + message = args[0] |
| 33 | + from kombu.messaging import Producer |
| 34 | + if isinstance(message, Producer): |
| 35 | + return message.connection.host |
| 36 | + else: |
| 37 | + return "localhost" |
| 38 | + |
| 39 | + def get_routing_exchange(self, *args, **kwargs): |
| 40 | + if kwargs: |
| 41 | + # return "" if 'routing_key' not in kwargs else kwargs['routing_key'], "" if 'exchange' not in kwargs else kwargs['exchange'].name |
| 42 | + routing_key = "" if 'routing_key' not in kwargs else kwargs['routing_key'] |
| 43 | + exchange = "" if 'exchange' not in kwargs else kwargs['exchange'] |
| 44 | + |
| 45 | + if isinstance(exchange, kombu.entity.Exchange): |
| 46 | + exchange = exchange.name |
| 47 | + return routing_key, exchange |
| 48 | + else: |
| 49 | + return "", "" |
| 50 | + |
| 51 | + def onBefore(self, parentId, *args, **kwargs): |
| 52 | + trace_id, args, kwargs = super().onBefore(parentId, *args, **kwargs) |
| 53 | + pinpoint.add_trace_header( |
| 54 | + Defines.PP_INTERCEPTOR_NAME, self.getUniqueName(), trace_id) |
| 55 | + # fixme treat all type as rabbitmq |
| 56 | + pinpoint.add_trace_header( |
| 57 | + Defines.PP_SERVER_TYPE, Defines.PP_RABBITMQ_CLIENT, trace_id) |
| 58 | + |
| 59 | + pinpoint.add_trace_header( |
| 60 | + Defines.PP_DESTINATION, self.get_host(*args, **kwargs), trace_id) |
| 61 | + |
| 62 | + routing, ex = self.get_routing_exchange(*args, **kwargs) |
| 63 | + pinpoint.add_trace_header_v2( |
| 64 | + Defines.PP_RABBITMQ_ROUTINGKEY, routing, trace_id) |
| 65 | + pinpoint.add_trace_header_v2( |
| 66 | + Defines.PP_RABBITMQ_EXCHANGEKEY, ex, trace_id) |
| 67 | + |
| 68 | + return trace_id, args, kwargs |
| 69 | + |
| 70 | + def onEnd(self, traceId, ret): |
| 71 | + super().onEnd(traceId, ret) |
| 72 | + return ret |
| 73 | + |
| 74 | + def onException(self, traceId, e): |
| 75 | + pinpoint.add_exception(str(e), traceId) |
0 commit comments