Skip to content

Instantly share code, notes, and snippets.

@pburkholder
Created March 19, 2024 22:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pburkholder/7e58e43cbdb23bb18d887e9ca81636f6 to your computer and use it in GitHub Desktop.
Save pburkholder/7e58e43cbdb23bb18d887e9ca81636f6 to your computer and use it in GitHub Desktop.
011y python example
import os
from opentelemetry import (
trace
)
from opentelemetry.instrumentation.requests import RequestsInstrumentor
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import (
BatchSpanProcessor,
ConsoleSpanExporter,
SimpleSpanProcessor
)
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import (
OTLPSpanExporter,
)
from opentelemetry.semconv.resource import ResourceAttributes
from grpc import ssl_channel_credentials
from dotenv import load_dotenv
load_dotenv() # take environment variables from .env
# Set up tracing
service_name = os.getenv("OTEL_SERVICE_NAME", "peterb-sequence-of-numbers")
resource = Resource(attributes={
ResourceAttributes.SERVICE_NAME: service_name
})
trace.set_tracer_provider(TracerProvider(resource=resource))
#apikey = os.environ.get("HONEYCOMB_API_KEY", "missing API key")
print("Sending traces to Jaeger with apikey")
# Send the traces to Honeycomb
#hnyExporter = OTLPSpanExporter(
# endpoint="api.honeycomb.io:443",
# insecure=False,
# credentials=ssl_channel_credentials(),
# headers=(
# ("x-honeycomb-team", apikey),
# )
#)
#trace.get_tracer_provider().add_span_processor(BatchSpanProcessor(hnyExporter))
jaeger_endpoint = os.environ.get("JAEGER_ENDPOINT")
jaegerExporter = OTLPSpanExporter(
endpoint=jaeger_endpoint
)
trace.get_tracer_provider().add_span_processor(BatchSpanProcessor(jaegerExporter))
# To see spans in the log, uncomment this:
trace.get_tracer_provider().add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter()))
# auto-instrument outgoing requests
RequestsInstrumentor().instrument(tracer_provider=trace.get_tracer_provider())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment