|
| 1 | +# |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +# contributor license agreements. See the NOTICE file distributed with |
| 4 | +# this work for additional information regarding copyright ownership. |
| 5 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +# (the "License"); you may not use this file except in compliance with |
| 7 | +# the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | +from skywalking import Layer, Component |
| 18 | +from skywalking.trace.carrier import Carrier |
| 19 | +from skywalking.trace.context import get_context |
| 20 | +from skywalking.trace.tags import TagMqTopic, TagMqBroker |
| 21 | + |
| 22 | +link_vector = ['https://github.com/apache/pulsar-client-python'] |
| 23 | +support_matrix = { |
| 24 | + 'pulsar-client': { |
| 25 | + '>=3.8': ['3.3.0'] |
| 26 | + } |
| 27 | +} |
| 28 | +note = """""" |
| 29 | + |
| 30 | + |
| 31 | +def install(): |
| 32 | + from pulsar import Producer |
| 33 | + from pulsar import Consumer |
| 34 | + from pulsar import Client |
| 35 | + |
| 36 | + __init = Client.__init__ |
| 37 | + _send = Producer.send |
| 38 | + _receive = Consumer.receive |
| 39 | + _peer = '' |
| 40 | + |
| 41 | + def get_peer(): |
| 42 | + return _peer |
| 43 | + |
| 44 | + def set_peer(value): |
| 45 | + nonlocal _peer |
| 46 | + _peer = value |
| 47 | + |
| 48 | + def _sw_init(self, service_url): |
| 49 | + __init(self, service_url) |
| 50 | + set_peer(service_url) |
| 51 | + |
| 52 | + def _sw_send_func(_send): |
| 53 | + def _sw_send(this, content, |
| 54 | + properties=None, |
| 55 | + partition_key=None, |
| 56 | + sequence_id=None, |
| 57 | + replication_clusters=None, |
| 58 | + disable_replication=False, |
| 59 | + event_timestamp=None, |
| 60 | + deliver_at=None, |
| 61 | + deliver_after=None, |
| 62 | + ): |
| 63 | + topic = this._producer.topic().split('/')[-1] |
| 64 | + with get_context().new_exit_span(op=f'Pulsar/Topic/{topic}/Producer', peer=get_peer(), |
| 65 | + component=Component.PulsarProducer) as span: |
| 66 | + span.tag(TagMqTopic(topic)) |
| 67 | + span.tag(TagMqBroker(get_peer())) |
| 68 | + span.layer = Layer.MQ |
| 69 | + |
| 70 | + carrier = span.inject() |
| 71 | + if properties is None: |
| 72 | + properties = {} |
| 73 | + for item in carrier: |
| 74 | + properties[item.key] = item.val |
| 75 | + |
| 76 | + return _send(this, content, properties=properties, partition_key=partition_key, |
| 77 | + sequence_id=sequence_id, replication_clusters=replication_clusters, |
| 78 | + disable_replication=disable_replication, event_timestamp=event_timestamp, |
| 79 | + deliver_at=deliver_at, deliver_after=deliver_after) |
| 80 | + |
| 81 | + return _sw_send |
| 82 | + |
| 83 | + def _sw_receive_func(_receive): |
| 84 | + def _sw_receive(this, timeout_millis=None): |
| 85 | + res = _receive(this, timeout_millis=timeout_millis) |
| 86 | + if res: |
| 87 | + topic = res.topic_name().split('/')[-1] |
| 88 | + properties = res.properties() |
| 89 | + carrier = Carrier() |
| 90 | + for item in carrier: |
| 91 | + if item.key in properties.keys(): |
| 92 | + val = res.properties().get(item.key) |
| 93 | + if val is not None: |
| 94 | + item.val = val |
| 95 | + |
| 96 | + with get_context().new_entry_span(op=f'Pulsar/Topic/{topic}/Consumer', carrier=carrier) as span: |
| 97 | + span.tag(TagMqTopic(topic)) |
| 98 | + span.tag(TagMqBroker(get_peer())) |
| 99 | + span.layer = Layer.MQ |
| 100 | + span.component = Component.PulsarConsumer |
| 101 | + return res |
| 102 | + |
| 103 | + return _sw_receive |
| 104 | + |
| 105 | + Client.__init__ = _sw_init |
| 106 | + Producer.send = _sw_send_func(_send) |
| 107 | + Consumer.receive = _sw_receive_func(_receive) |
0 commit comments