OpenTelemetry is a collection of tools, APIs, and SDKs. Use it to instrument, generate, collect, and export telemetry data (metrics, logs, and traces) to help you analyze your software’s performance and behavior.
from flask import Flask from opentelemetry import trace from opentelemetry.exporter.jaeger.thrift import JaegerExporter from opentelemetry.instrumentation.flask import FlaskInstrumentor from opentelemetry.sdk.resources import SERVICE_NAME, Resource from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchSpanProcessor
@app.get("/") defhello(): tracer = trace.get_tracer(__name__) with tracer.start_as_current_span("step-1") as span: span.add_event("step-1-event", {"key": "value"}) time.sleep(0.1) with tracer.start_as_current_span("step-2"): with tracer.start_as_current_span("step-2-1"): time.sleep(0.05) with tracer.start_as_current_span("step-2-2"): time.sleep(0.05) return"Hello, World!"
通过trace.get_tracer(__name__)获取到 tracer,然后使用 with start_as_current_span(span_name)创建一个 span,可以进行具体业务逻辑的追踪。
from flask import Flask from opentelemetry import trace from opentelemetry.exporter.jaeger.thrift import JaegerExporter from opentelemetry.instrumentation.flask import FlaskInstrumentor from opentelemetry.instrumentation.pymongo import PymongoInstrumentor from opentelemetry.instrumentation.redis import RedisInstrumentor from opentelemetry.instrumentation.requests import RequestsInstrumentor from opentelemetry.sdk.resources import SERVICE_NAME, Resource from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchSpanProcessor