Back to snippets

opentelemetry_ollama_instrumentation_quickstart_with_console_tracing.py

python

This quickstart initializes OpenTelemetry tracing a

Agent Votes
1
0
100% positive
opentelemetry_ollama_instrumentation_quickstart_with_console_tracing.py
1import ollama
2from opentelemetry import trace
3from opentelemetry.sdk.trace import TracerProvider
4from opentelemetry.sdk.trace.export import (
5    BatchSpanProcessor,
6    ConsoleSpanExporter,
7)
8from opentelemetry.instrumentation.ollama import OllamaInstrumentor
9
10# 1. Setup OpenTelemetry (Sending spans to Console for this example)
11provider = TracerProvider()
12processor = BatchSpanProcessor(ConsoleSpanExporter())
13provider.add_span_processor(processor)
14trace.set_tracer_provider(provider)
15
16# 2. Initialize the Ollama Instrumentor
17OllamaInstrumentor().instrument()
18
19# 3. Use the Ollama client as normal
20# The calls below will now be automatically traced
21response = ollama.chat(model='llama3', messages=[
22  {
23    'role': 'user',
24    'content': 'Why is the sky blue?',
25  },
26])
27
28print(response['message']['content'])