pip install opentelemetry-sdk \
opentelemetry-exporter-otlp-proto-grpc \
opentelemetry-instrumentation-flask \
opentelemetry-instrumentation-requests \
opentelemetry-instrumentation-sqlalchemy# Install the auto-instrumentation agent
pip install opentelemetry-distro
opentelemetry-bootstrap -a install
# Run your app
OTEL_SERVICE_NAME=your-service-name \
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:9090 \
OTEL_EXPORTER_OTLP_PROTOCOL=grpc \
OTEL_TRACES_EXPORTER=otlp \
OTEL_METRICS_EXPORTER=none \
OTEL_LOGS_EXPORTER=none \
opentelemetry-instrument python app.pyfrom opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.resources import Resource
resource = Resource(attributes={"service.name": "your-service-name"})
provider = TracerProvider(resource=resource)
provider.add_span_processor(
BatchSpanProcessor(
OTLPSpanExporter(
endpoint="http://localhost:9090",
insecure=True
)
)
)
trace.set_tracer_provider(provider)from opentelemetry.instrumentation.flask import FlaskInstrumentor
from flask import Flask
app = Flask(__name__)
FlaskInstrumentor().instrument_app(app)
@app.route('/health')
def health():
return {'status': 'ok'}Every request to /health automatically creates a span in Lumen.
tracer = trace.get_tracer(__name__)
def process_payment(amount):
with tracer.start_as_current_span("process-payment") as span:
span.set_attribute("payment.amount", amount)
# your logic here
return charge_card(amount)curl http://localhost:8080/api/services
# Should show your-service-name