Skip to content

Commit 0bb2ddf

Browse files
authored
Merge pull request #70 from lyt122/main
feature:add Jaeger to provide distributed tracing
2 parents 6a125d2 + da557ef commit 0bb2ddf

9 files changed

Lines changed: 151 additions & 47 deletions

File tree

app/search_vector/consts/consts.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,7 @@
2121
KAFKA_CONSUMER_VECTOR_INDEX_TOPIC = "search-engine-csv-loader-topic"
2222

2323
VECTOR_RECALL_TOPK = 20
24+
25+
OTEL_ENDPOINT = "127.0.0.1:4317"
26+
27+
SERVICE_NAME = "tangseng-python"

app/search_vector/kafka_operate/consumer.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@
1616
# under the License.
1717

1818
"""store vector index from kafka"""
19+
import inspect
20+
from opentelemetry import trace
1921
from ..kafka_operate.kafka_operate import kafka_helper
2022

2123

2224
def store_data_from_kafka(kafka_topic, milvus_table_name):
2325
"""
2426
store data to mivlus from kakfa for building inverted index
2527
"""
26-
kafka_helper.connect_consumer(kafka_topic)
27-
kafka_helper.consume_messages_store_milvus(milvus_table_name)
28+
tracer = trace.get_tracer(__name__)
29+
with tracer.start_as_current_span(inspect.getframeinfo(inspect.currentframe()).function):
30+
kafka_helper.connect_consumer(kafka_topic)
31+
kafka_helper.consume_messages_store_milvus(milvus_table_name)

app/search_vector/kafka_operate/kafka_operate.py

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@
1616
# under the License.
1717

1818
"""kafka operate"""
19+
import inspect
1920
import json
2021
from kafka import KafkaProducer, KafkaConsumer
2122
from kafka.errors import KafkaError
23+
from opentelemetry import trace
2224
from ..config.config import KAFKA_CLUSTER
2325
from ..milvus import milvus
2426
from ..milvus.operators import do_upload
2527

28+
tracer = trace.get_tracer(__name__)
29+
2630

2731
class KafkaHelper:
2832
"""
@@ -39,25 +43,27 @@ def connect_producer(self):
3943
"""
4044
connect kafka producer
4145
"""
42-
try:
43-
self.producer = KafkaProducer(
44-
bootstrap_servers=self.bootstrap_servers)
45-
print("Connected to Kafka producer successfully.")
46-
except KafkaError as e:
47-
print(f"Failed to connect to Kafka producer: {e}")
46+
with tracer.start_as_current_span(inspect.getframeinfo(inspect.currentframe()).function):
47+
try:
48+
self.producer = KafkaProducer(
49+
bootstrap_servers=self.bootstrap_servers)
50+
print("Connected to Kafka producer successfully.")
51+
except KafkaError as e:
52+
print(f"Failed to connect to Kafka producer: {e}")
4853

4954
def connect_consumer(self, topic):
5055
"""
5156
connect kafka consumer
5257
"""
53-
try:
54-
self.consumer = KafkaConsumer(
55-
topic, bootstrap_servers=self.bootstrap_servers)
56-
print(
57-
f"Connected to Kafka consumer successfully. Listening to topic: {topic}"
58-
)
59-
except KafkaError as e:
60-
print(f"Failed to connect to Kafka consumer: {e}")
58+
with tracer.start_as_current_span(inspect.getframeinfo(inspect.currentframe()).function):
59+
try:
60+
self.consumer = KafkaConsumer(
61+
topic, bootstrap_servers=self.bootstrap_servers)
62+
print(
63+
f"Connected to Kafka consumer successfully. Listening to topic: {topic}"
64+
)
65+
except KafkaError as e:
66+
print(f"Failed to connect to Kafka consumer: {e}")
6167

6268
def send_message(self, topic, msg):
6369
"""
@@ -88,14 +94,15 @@ def consume_messages_store_milvus(self, milvus_table):
8894
"""
8995
consume messages from kafka and store in milvus
9096
"""
91-
if not self.consumer:
92-
print("No Kafka consumer connected.")
93-
return
94-
print("Consuming messages...")
95-
for msg in self.consumer:
96-
data = json.loads(msg.value.decode('utf-8'))
97-
do_upload(milvus_table, int(data["doc_id"]), data["title"],
98-
data["body"], self.milvus_client)
97+
with tracer.start_as_current_span(inspect.getframeinfo(inspect.currentframe()).function):
98+
if not self.consumer:
99+
print("No Kafka consumer connected.")
100+
return
101+
print("Consuming messages...")
102+
for msg in self.consumer:
103+
data = json.loads(msg.value.decode('utf-8'))
104+
do_upload(milvus_table, int(data["doc_id"]), data["title"],
105+
data["body"], self.milvus_client)
99106

100107
def on_send_success(self, record_metadata):
101108
"""

app/search_vector/service/search_vector.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
# under the License.
1717

1818
"""search vector grpc service"""
19+
import inspect
1920
import json
2021
import grpc
2122
import logging
2223
import asyncio
2324

25+
from opentelemetry import trace
2426
from ..consts.consts import VECTOR_RECALL_TOPK
2527
from idl.pb.search_vector import search_vector_pb2
2628
from ..config.config import DEFAULT_MILVUS_TABLE_NAME, VECTOR_ADDR
@@ -29,6 +31,8 @@
2931
from ..milvus.milvus import milvus_client
3032
from idl.pb.search_vector import search_vector_pb2_grpc
3133

34+
tracer = trace.get_tracer(__name__)
35+
3236

3337
class SearchVectorService(search_vector_pb2_grpc.SearchVectorServiceServicer):
3438
"""
@@ -37,23 +41,24 @@ class SearchVectorService(search_vector_pb2_grpc.SearchVectorServiceServicer):
3741

3842
def SearchVector(self, request,
3943
context) -> search_vector_pb2.SearchVectorResponse:
40-
try:
41-
queryies = request.query
42-
doc_ids = []
43-
for query in queryies:
44-
ids, distants = do_search(DEFAULT_MILVUS_TABLE_NAME, query,
45-
VECTOR_RECALL_TOPK, milvus_client)
46-
print("search vector ids", ids)
47-
doc_ids += ids
48-
print("search vector data", doc_ids)
49-
return search_vector_pb2.SearchVectorResponse(code=200,
50-
doc_ids=doc_ids,
51-
msg='ok',
52-
error='')
53-
except Exception as e:
54-
print("search vector error", e)
55-
return search_vector_pb2.SearchVectorResponse(code=500,
56-
error=str(e))
44+
with tracer.start_as_current_span(inspect.getframeinfo(inspect.currentframe()).function):
45+
try:
46+
queryies = request.query
47+
doc_ids = []
48+
for query in queryies:
49+
ids, distants = do_search(DEFAULT_MILVUS_TABLE_NAME, query,
50+
VECTOR_RECALL_TOPK, milvus_client)
51+
print("search vector ids", ids)
52+
doc_ids += ids
53+
print("search vector data", doc_ids)
54+
return search_vector_pb2.SearchVectorResponse(code=200,
55+
doc_ids=doc_ids,
56+
msg='ok',
57+
error='')
58+
except Exception as e:
59+
print("search vector error", e)
60+
return search_vector_pb2.SearchVectorResponse(code=500,
61+
error=str(e))
5762

5863

5964
async def serve() -> None:
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with 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,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with 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,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
from opentelemetry import trace
19+
from opentelemetry.sdk.trace import TracerProvider
20+
from opentelemetry.sdk.resources import SERVICE_NAME
21+
from opentelemetry.sdk.trace.export import BatchSpanProcessor
22+
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
23+
from opentelemetry.sdk.resources import Resource
24+
from opentelemetry.propagate import set_global_textmap
25+
from opentelemetry.propagators.b3 import B3MultiFormat
26+
27+
28+
def init_tracer_provider(url, service_name):
29+
# 创建一个新的 OTLP 导出器
30+
exporter = OTLPSpanExporter(
31+
insecure=True,
32+
endpoint=url,
33+
)
34+
35+
resource = Resource(attributes={
36+
SERVICE_NAME: service_name,
37+
})
38+
39+
# 设置全局tracer
40+
provider = TracerProvider(
41+
resource=resource,
42+
)
43+
provider.add_span_processor(BatchSpanProcessor(exporter))
44+
trace.set_tracer_provider(provider)
45+
46+
# 设置全局Propagator
47+
b3_propagator = B3MultiFormat()
48+
set_global_textmap(b3_propagator)

main.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,17 @@
2828
from PIL import Image
2929
from flask import Flask, request
3030
from torchvision import transforms
31+
from opentelemetry.instrumentation.flask import FlaskInstrumentor
3132
from app.search_vector.service.search_vector import serve
32-
33+
from app.search_vector.consts.consts import OTEL_ENDPOINT
34+
from app.search_vector.consts.consts import SERVICE_NAME
3335
from app.search_vector.config.config import DEFAULT_MILVUS_TABLE_NAME, NETWORK_MODEL_NAME
3436
from app.search_vector.cirtorch.datasets.datahelpers import imresize
3537
from app.search_vector.cirtorch.networks.imageretrievalnet import init_network
3638
from app.search_vector.milvus.milvus import milvus_client
3739
from app.search_vector.milvus.operators import do_upload, do_search
3840
from app.search_vector.utils.logs import LOGGER
41+
from app.search_vector.tracing.tracing import init_tracer_provider
3942

4043
app = Flask(__name__)
4144

@@ -199,6 +202,9 @@ def init_model():
199202
net, lsh, transform = init_model()
200203

201204
if __name__ == "__main__":
205+
init_tracer_provider(url=OTEL_ENDPOINT, service_name=SERVICE_NAME)
206+
# FlaskInstrumentor is to trace http
207+
# FlaskInstrumentor().instrument_app(app)
202208
# app.run(host=WEBSITE_HOST, port=WEBSITE_PORT, debug=True)
203209
# print("start server {}:{}".format(WEBSITE_HOST, WEBSITE_PORT))
204210
asyncio.run(serve())

requirements.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,12 @@ ujson==5.8.0
6464
urllib3==2.0.4
6565
Werkzeug==2.3.7
6666
zipp==3.17.0
67+
opentelemetry-api==1.23.0
68+
opentelemetry-sdk==1.23.0
69+
opentelemetry-exporter-otlp-proto-grpc==1.23.0
70+
opentelemetry-propagator-b3==1.23.0
71+
opentelemetry-instrumentation-flask==0.44b0
72+
73+
74+
75+

vector_index.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
# under the License.
1717

1818
"""the script file is to handle vector index from kafka"""
19+
import inspect
1920
import threading
21+
from opentelemetry import trace
2022
from app.search_vector.consts.consts import KAFKA_CONSUMER_VECTOR_INDEX_TOPIC
2123
from app.search_vector.config.config import DEFAULT_MILVUS_TABLE_NAME
2224
from app.search_vector.kafka_operate.consumer import store_data_from_kafka
@@ -28,11 +30,13 @@ def consume_inverted_index():
2830
"""
2931
topic = KAFKA_CONSUMER_VECTOR_INDEX_TOPIC
3032
table_name = DEFAULT_MILVUS_TABLE_NAME
31-
thread = threading.Thread(target=store_data_from_kafka(
32-
topic, table_name)) # 创建线程对象
33-
thread.start() # 启动线程
34-
print("start consume inverted index")
35-
thread.join() # 等待线程结束
33+
tracer = trace.get_tracer(__name__)
34+
with tracer.start_as_current_span(inspect.getframeinfo(inspect.currentframe()).function):
35+
thread = threading.Thread(target=store_data_from_kafka(
36+
topic, table_name)) # 创建线程对象
37+
thread.start() # 启动线程
38+
print("start consume inverted index")
39+
thread.join() # 等待线程结束
3640

3741

3842
if __name__ == "__main__":

0 commit comments

Comments
 (0)