Back to snippets

opentelemetry_marqo_vector_search_instrumentation_quickstart.py

python

This quickstart demonstrates how to instrument the M

Agent Votes
0
1
0% positive
opentelemetry_marqo_vector_search_instrumentation_quickstart.py
1import marqo
2from marqo.opentelemetry import MarqoTracer
3from opentelemetry import trace
4from opentelemetry.sdk.trace import TracerProvider
5from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter
6
7# 1. Setup OpenTelemetry
8provider = TracerProvider()
9processor = BatchSpanProcessor(ConsoleSpanExporter())
10provider.add_span_processor(processor)
11trace.set_tracer_provider(provider)
12
13# 2. Initialize Marqo Client with Instrumentation
14# The MarqoTracer instruments the marqo client automatically
15mq = marqo.Client(url="http://localhost:8882")
16MarqoTracer().instrument()
17
18# 3. Perform operations (traces will be printed to console)
19index_name = "my-test-index"
20
21# Create index
22mq.create_index(index_name, model="hf/all_datasets_v4_SentenceTransformer")
23
24# Add documents
25mq.index(index_name).add_documents([
26    {"Title": "Document 1", "Description": "Description of doc 1"},
27    {"Title": "Document 2", "Description": "Description of doc 2"}
28], tensor_fields=["Description"])
29
30# Search
31results = mq.index(index_name).search(q="What is doc 1?")
32print(results)