Last active
October 1, 2024 08:27
-
-
Save samuelcolvin/73d6536166236cad2bf04044fd0ee0f1 to your computer and use it in GitHub Desktop.
Logfire vs raw OTel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
To run install: | |
logfire[fastapi] | |
fastapi | |
uvicorn | |
""" | |
import time | |
import logfire | |
import uvicorn | |
from fastapi import FastAPI | |
# this is generally all you need to set up logfire | |
# (the token can also be set via the LOGFIRE_TOKEN environment variable | |
# with using the SDK with `logfire projects use`) | |
logfire.configure(service_name='api') | |
# send a zero-duration span AKA a log | |
logfire.info("hello {name}", name="world") | |
do_some_work = lambda: time.sleep(2) | |
# send a span with a duration | |
with logfire.span("my span"): | |
do_some_work() | |
# instrument a FastAPI app | |
app = FastAPI() | |
logfire.instrument_fastapi(app) | |
@app.get('/') | |
def read_root(): | |
return {'Hello': 'World'} | |
if __name__ == '__main__': | |
uvicorn.run(app, port=8000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
To run install: | |
opentelemetry-api | |
opentelemetry-exporter-otlp-proto-http | |
opentelemetry-instrumentation-fastapi | |
fastapi | |
uvicorn | |
""" | |
import os | |
import time | |
import uvicorn | |
from fastapi import FastAPI | |
from opentelemetry import trace | |
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader | |
from opentelemetry.sdk.resources import SERVICE_NAME | |
from opentelemetry.sdk.resources import Resource | |
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter | |
from opentelemetry.exporter.otlp.proto.http.metric_exporter import OTLPMetricExporter | |
from opentelemetry.sdk.trace import TracerProvider | |
from opentelemetry.sdk.trace.export import BatchSpanProcessor | |
from opentelemetry.sdk.metrics import MeterProvider | |
from opentelemetry import metrics | |
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor | |
# configure OpenTelemetry | |
LOGFIRE_BASE_URL = 'https://logfire-api.pydantic.dev/v1' | |
LOGFIRE_TOKEN = os.environ['LOGFIRE_TOKEN'] | |
headers = { | |
'Authorization': LOGFIRE_TOKEN | |
} | |
resource = Resource(attributes={SERVICE_NAME: 'api'}) | |
provider = TracerProvider() | |
processor = BatchSpanProcessor( | |
OTLPSpanExporter( | |
endpoint=f'{LOGFIRE_BASE_URL}/traces', | |
headers=headers, | |
), | |
schedule_delay_millis=1000, | |
) | |
provider.add_span_processor(processor) | |
trace.set_tracer_provider(provider) | |
tracer = trace.get_tracer(__name__) | |
reader = PeriodicExportingMetricReader( | |
OTLPMetricExporter(endpoint=f'{LOGFIRE_BASE_URL}/metrics', headers=headers) | |
) | |
meterProvider = MeterProvider(resource=resource, metric_readers=[reader]) | |
metrics.set_meter_provider(meterProvider) | |
# NOTE attribute values must be simple types like ints and strings, None is not allowed1 | |
with tracer.start_span("hello world", attributes={'name': 'world'}): | |
pass | |
do_some_work = lambda: time.sleep(2) | |
with tracer.start_span("my span"): | |
do_some_work() | |
app = FastAPI() | |
FastAPIInstrumentor.instrument_app(app) | |
@app.get('/') | |
def read_root(): | |
return {'Hello': 'World'} | |
if __name__ == '__main__': | |
uvicorn.run(app, port=8000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment