Skip to content

feat/Add in fastapi otel middleware #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 28, 2024
Merged

feat/Add in fastapi otel middleware #20

merged 4 commits into from
Aug 28, 2024

Conversation

rbiseck3
Copy link
Collaborator

@rbiseck3 rbiseck3 commented Aug 27, 2024

Description

Support otel data being emitted assuming env vars are set.

Testing

pip install .
OTEL_TRACES_EXPORTER=console PYTHONPATH=. etl-uvicorn test.assets.empty_input_and_response:SampleClass --method-name do_nothing

Then in another window:

curl http://127.0.0.1:8000/schema

You will now see the traces being emitted to the console:

{
    "name": "GET /schema http send",
    "context": {
        "trace_id": "0x12897471a088f9c5b78d7f3923ad4a64",
        "span_id": "0x655107449032a999",
        "trace_state": "[]"
    },
    "kind": "SpanKind.INTERNAL",
    "parent_id": "0x983094d67ffe7323",
    "start_time": "2024-08-27T20:00:31.586969Z",
    "end_time": "2024-08-27T20:00:31.587908Z",
    "status": {
        "status_code": "UNSET"
    },
    "attributes": {
        "asgi.event.type": "http.response.start",
        "http.status_code": 200
    },
    "events": [],
    "links": [],
    "resource": {
        "attributes": {
            "service.name": "unknown_service"
        },
        "schema_url": ""
    }
}
{
    "name": "GET /schema http send",
    "context": {
        "trace_id": "0x12897471a088f9c5b78d7f3923ad4a64",
        "span_id": "0x1cef6596192fc80b",
        "trace_state": "[]"
    },
    "kind": "SpanKind.INTERNAL",
    "parent_id": "0x983094d67ffe7323",
    "start_time": "2024-08-27T20:00:31.588393Z",
    "end_time": "2024-08-27T20:00:31.588439Z",
    "status": {
        "status_code": "UNSET"
    },
    "attributes": {
        "asgi.event.type": "http.response.body"
    },
    "events": [],
    "links": [],
    "resource": {
        "attributes": {
            "service.name": "unknown_service"
        },
        "schema_url": ""
    }
}
{
    "name": "GET /schema",
    "context": {
        "trace_id": "0x12897471a088f9c5b78d7f3923ad4a64",
        "span_id": "0x983094d67ffe7323",
        "trace_state": "[]"
    },
    "kind": "SpanKind.SERVER",
    "parent_id": null,
    "start_time": "2024-08-27T20:00:31.586157Z",
    "end_time": "2024-08-27T20:00:31.588597Z",
    "status": {
        "status_code": "UNSET"
    },
    "attributes": {
        "http.scheme": "http",
        "http.host": "127.0.0.1:8000",
        "net.host.port": 8000,
        "http.flavor": "1.1",
        "http.target": "/schema",
        "http.url": "http://127.0.0.1:8000/schema",
        "http.method": "GET",
        "http.server_name": "127.0.0.1:8000",
        "http.user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36",
        "net.peer.ip": "127.0.0.1",
        "net.peer.port": 54874,
        "http.route": "/schema",
        "http.status_code": 200
    },
    "events": [],
    "links": [],
    "resource": {
        "attributes": {
            "service.name": "unknown_service"
        },
        "schema_url": ""
    }
}

Can also run against an otel collector:
Set up a local collector using docker compose:

services:
  jaeger:
    image: jaegertracing/all-in-one:latest
    container_name: jaeger
    restart: always
    ports:
      - "16686:16686"
      - "14268"
      - "14250"


  otel-collector:
    image: otel/opentelemetry-collector
    container_name: otel-collector
    restart: always
    command: [ "--config=/etc/otel-collector-config.yaml", "${OTELCOL_ARGS}" ]
    volumes:
      - ./otel_assets/otel-collector-config.yaml:/etc/otel-collector-config.yaml
    ports:
      - "1888:1888"   # pprof extension
      - "13133:13133" # health_check extension
      - "4317:4317"   # OTLP gRPC receiver
      - "4318:4318"   # OTLP http receiver
      - "55679:55679" # zpages extension
    depends_on:
      - jaeger

Run the api:

OTEL_SERVICE_NAME=test_service OTEL_TRACES_EXPORTER=otlp OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=http://localhost:4317 PYTHONPATH=. etl-uvicorn test.assets.empty_input_and_response:SampleClass --method-name do_nothing

After invoking the api, view the emitted traces in Jaegar at http://localhost:16686/

Copy link
Contributor

@ctrahey ctrahey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

@rbiseck3 rbiseck3 merged commit 5e7dd40 into main Aug 28, 2024
7 checks passed
@rbiseck3 rbiseck3 deleted the roman/otel-support branch August 28, 2024 13:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants