Skip to content

Commit

Permalink
Commit Ex 4
Browse files Browse the repository at this point in the history
  • Loading branch information
vedantprof committed Jul 6, 2023
1 parent 0a77d30 commit e41a940
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
12 changes: 12 additions & 0 deletions .projen/deps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion .projenrc.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
'fastapi',
'google-cloud-storage',
'redis',
'uvicorn[standard]'
'uvicorn[standard]',
'opentelemetry-api',
'opentelemetry-sdk',
'opentelemetry-instrumentation-fastapi'
],
dev_deps=[
'attrs',
Expand Down
35 changes: 34 additions & 1 deletion taskman/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@
from starlette.responses import RedirectResponse
from .backends import Backend, RedisBackend, MemoryBackend, GCSBackend
from .model import Task, TaskRequest
from pydantic import BaseModel
from starlette.responses import RedirectResponse
from redis import Redis

import fastapi
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import (
BatchSpanProcessor,
ConsoleSpanExporter,
)
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
from opentelemetry.semconv.trace import SpanAttributes
from opentelemetry.trace import get_current_span
from opentelemetry.trace.status import StatusCode

app = FastAPI()

Expand Down Expand Up @@ -35,16 +50,22 @@ def redirect_to_tasks() -> None:
@app.get('/tasks')
def get_tasks(backend: Annotated[Backend, Depends(get_backend)]) -> List[Task]:
keys = backend.keys()

tasks = []
for key in keys:
tasks.append(backend.get(key))

return tasks


@app.get('/tasks/{task_id}')
def get_task(task_id: str,
backend: Annotated[Backend, Depends(get_backend)]) -> Task:

current_span = trace.get_current_span()
if current_span:
current_span.set_attribute('task.id', task_id)
current_span.set_attribute('task.name', "This is Span - VRock")
current_span.set_attribute(SpanAttributes.HTTP_METHOD, "GET")
return backend.get(task_id)


Expand All @@ -61,3 +82,15 @@ def create_task(request: TaskRequest,
task_id = str(uuid4())
backend.set(task_id, request)
return task_id

provider = TracerProvider()
processor = BatchSpanProcessor(ConsoleSpanExporter())
provider.add_span_processor(processor)

# Sets the global default tracer provider
trace.set_tracer_provider(provider)

# Creates a tracer from the global tracer provider
tracer = trace.get_tracer("my.tracer.name")

FastAPIInstrumentor.instrument_app(app)

0 comments on commit e41a940

Please sign in to comment.