Skip to content

Commit

Permalink
Modularize instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
anticorrelator committed Jan 25, 2024
1 parent c7e258b commit 575a3fd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
16 changes: 16 additions & 0 deletions python/examples/llama-index/backend/instrument.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from openinference.instrumentation.llama_index import LlamaIndexInstrumentor
from opentelemetry import trace as trace_api
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk import trace as trace_sdk
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace.export import SimpleSpanProcessor


def instrument():
resource = Resource(attributes={})
tracer_provider = trace_sdk.TracerProvider(resource=resource)
span_exporter = OTLPSpanExporter(endpoint="http://phoenix:6006/v1/traces")
span_processor = SimpleSpanProcessor(span_exporter=span_exporter)
tracer_provider.add_span_processor(span_processor=span_processor)
trace_api.set_tracer_provider(tracer_provider=tracer_provider)
LlamaIndexInstrumentor().instrument()
23 changes: 7 additions & 16 deletions python/examples/llama-index/backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,13 @@
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

from openinference.instrumentation.llama_index import LlamaIndexInstrumentor
from opentelemetry import trace as trace_api
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk import trace as trace_sdk
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace.export import SimpleSpanProcessor

resource = Resource(attributes={})
tracer_provider = trace_sdk.TracerProvider(resource=resource)
span_exporter = OTLPSpanExporter(endpoint="http://127.0.0.1:6006/v1/traces")
span_processor = SimpleSpanProcessor(span_exporter=span_exporter)
tracer_provider.add_span_processor(span_processor=span_processor)
trace_api.set_tracer_provider(tracer_provider=tracer_provider)
app = FastAPI()
from instrument import instrument

do_not_instrument = os.getenv("INSTRUMENT_LLAMA_INDEX", "true") == "false"
if not do_not_instrument:
instrument()

app = FastAPI()
environment = os.getenv("ENVIRONMENT", "dev") # Default to 'development' if not set


Expand All @@ -43,5 +35,4 @@


if __name__ == "__main__":
LlamaIndexInstrumentor().instrument()
uvicorn.run(app="main:app", host="0.0.0.0", reload=True)
uvicorn.run(app="main:app", host="0.0.0.0", port=8000, reload=True)

0 comments on commit 575a3fd

Please sign in to comment.