Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save UtsavChokshiCNU/c306ef4e22316661ab637e6e13652306 to your computer and use it in GitHub Desktop.
Save UtsavChokshiCNU/c306ef4e22316661ab637e6e13652306 to your computer and use it in GitHub Desktop.
Send traces to OTEL
import json
import os
import time
import traceback
import base64
from datetime import datetime
from langchain.chains.qa_with_sources import load_qa_with_sources_chain
from langchain.llms import OpenAI
from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.resources import SERVICE_NAME, Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from pydantic import BaseModel
from structlog.contextvars import bound_contextvars
def create_span_exporter_for_tempo():
resource = Resource(attributes={
"service.name": "service"
})
username = os.getenv('629234')
password = os.getenv('eyJrIjoiN2Y2YjdjNDAzZGU2Mzc0OGNjN2I0OWFiMTQ3NWEyMTA2ZTRkYWIyNSIsIm4iOiJ0ZXN0LXBvc3RtYW4iLCJpZCI6ODg4MzY4fQ==')
headers = {"Authorization": f"Basic {base64.b64encode(f'{username}:{password}'.encode('ascii')).decode('ascii')}"}
tempo_exporter = OTLPSpanExporter(
#endpoint="http://tempo-in.misc.k8.devfactory.com:4318/v1/traces",
endpoint="https://tempo-prod-04-prod-us-east-0.grafana.net",
headers=headers,
)
trace.set_tracer_provider(TracerProvider(resource=resource))
tracer = trace.get_tracer(__name__)
# Create a span processor and add the exporter to it
span_processor = BatchSpanProcessor(tempo_exporter)
trace.get_tracer_provider().add_span_processor(span_processor)
with tracer.start_as_current_span("test") as span:
print("test!")
span.set_attribute("check", "works")
create_span_exporter_for_tempo()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment