python包如下
pip install opentelemetry-exporter-jaeger
pip install opentelemetry-distro
pip install opentelemetry-instrumentation-grpc
代码如下
这里我简单根据 opentelemetry 官网给出的示例写的demo
import time
from random import randint
import requests
from opentelemetry.exporter.jaeger.thrift import JaegerExporter
from opentelemetry.sdk.resources import SERVICE_NAME, Resource
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
resource = Resource(attributes={
SERVICE_NAME: "mxshop"
})
jaeger_exporter = JaegerExporter(
agent_host_name="192.168.16.196",
agent_port=6831,
)
provider = TracerProvider(resource=resource)
processor = BatchSpanProcessor(jaeger_exporter)
provider.add_span_processor(processor)
# Sets the global default tracer provider
trace.set_tracer_provider(provider)
# Creates a tracer from the global tracer provider
tracer = trace.get_tracer(__name__)
def download():
rsp = requests.get("https://www.imooc.com")
return rsp
@tracer.start_as_current_span("parser")
def parser():
time.sleep(randint(1, 9)*0.1)
def insert_to_mysql():
with tracer.start_as_current_span("prepare") as prepare_span:
# 1. 生成sql的时间
time.sleep(randint(1, 9) * 0.1)
with tracer.start_as_current_span("execute") as execute_span:
# 2. 插入数据库的时间
time.sleep(randint(1, 9) * 0.1)
if __name__ == '__main__':
with tracer.start_as_current_span("spider") as spider_span:
# 下载
with tracer.start_as_current_span("get") as get_span:
download()
# 解析
# with tracer.start_as_current_span("parser") as parser_span:
parser() # 运用到 装饰器
# 入库
with tracer.start_as_current_span("insert") as insert_span:
insert_to_mysql()
time.sleep(2)
原先 jaeger_client 弃用 说是 要更加规范 所以要集成到 opentelemetry上
这里有一个问题想问一下 老师 怎么像你视频那样 把log打印出来