|
3 | 3 | ref. https://github.com/tadata-org/fastapi_mcp/blob/v0.3.4/examples/shared/apps/items.py
|
4 | 4 | """
|
5 | 5 |
|
| 6 | +import random |
| 7 | +import uuid |
| 8 | +from os import getenv |
| 9 | + |
| 10 | +from azure.monitor.opentelemetry import configure_azure_monitor |
6 | 11 | from fastapi import FastAPI, HTTPException, Query
|
| 12 | +from opentelemetry import trace |
| 13 | +from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor |
| 14 | +from opentelemetry.sdk.trace import TracerProvider |
| 15 | +from opentelemetry.sdk.trace.export import ( |
| 16 | + BatchSpanProcessor, |
| 17 | + ConsoleSpanExporter, |
| 18 | +) |
| 19 | +from opentelemetry.trace import Span |
7 | 20 | from pydantic import BaseModel
|
8 | 21 |
|
| 22 | +trace.set_tracer_provider(TracerProvider()) |
| 23 | +trace.get_tracer_provider().add_span_processor(BatchSpanProcessor(ConsoleSpanExporter())) |
| 24 | +tracer = trace.get_tracer(__name__) |
| 25 | + |
9 | 26 | app = FastAPI()
|
10 | 27 |
|
| 28 | +# If APPLICATIONINSIGHTS_CONNECTION_STRING exists, configure Azure Monitor |
| 29 | +AZURE_CONNECTION_STRING = getenv("APPLICATIONINSIGHTS_CONNECTION_STRING") |
| 30 | +if AZURE_CONNECTION_STRING: |
| 31 | + |
| 32 | + def server_request_hook(span: Span, scope: dict): |
| 33 | + if span and span.is_recording(): |
| 34 | + try: |
| 35 | + # Application Insights に送るデータにユーザ ID を追加する |
| 36 | + user_id = uuid.uuid4().hex # Replace with actual user ID retrieval logic |
| 37 | + span.set_attribute("enduser.id", user_id) |
| 38 | + except KeyError: |
| 39 | + pass |
| 40 | + |
| 41 | + configure_azure_monitor( |
| 42 | + connection_string=AZURE_CONNECTION_STRING, |
| 43 | + server_request_hook=server_request_hook, |
| 44 | + ) |
| 45 | + FastAPIInstrumentor.instrument_app(app) |
| 46 | + |
11 | 47 |
|
12 | 48 | class Item(BaseModel):
|
13 | 49 | id: int
|
@@ -124,3 +160,64 @@ async def search_items(
|
124 | 160 | ]
|
125 | 161 | for item in sample_items:
|
126 | 162 | items_db[item.id] = item
|
| 163 | + |
| 164 | + |
| 165 | +# Add flaky API which receives percentage of failure |
| 166 | +@app.get("/flaky/{failure_rate}", tags=["flaky"], operation_id="flaky") |
| 167 | +async def flaky(failure_rate: int): |
| 168 | + """ |
| 169 | + A flaky endpoint that simulates a failure based on the provided failure rate. |
| 170 | +
|
| 171 | + The failure rate is a percentage (0-100) that determines the likelihood of failure. |
| 172 | + """ |
| 173 | + if not (0 <= failure_rate <= 100): |
| 174 | + raise HTTPException( |
| 175 | + status_code=400, |
| 176 | + detail="Failure rate must be between 0 and 100", |
| 177 | + ) |
| 178 | + |
| 179 | + if random.randint(0, 100) < failure_rate: |
| 180 | + raise HTTPException( |
| 181 | + status_code=500, |
| 182 | + detail="Simulated failure", |
| 183 | + ) |
| 184 | + |
| 185 | + return { |
| 186 | + "message": "Request succeeded", |
| 187 | + } |
| 188 | + |
| 189 | + |
| 190 | +# Add flaky API which raises an exception |
| 191 | +@app.get("/flaky/exception", tags=["flaky"], operation_id="flaky_exception") |
| 192 | +async def flaky_exception(): |
| 193 | + """ |
| 194 | + A flaky endpoint that always raises an exception. |
| 195 | + """ |
| 196 | + raise HTTPException( |
| 197 | + status_code=500, |
| 198 | + detail="Simulated exception", |
| 199 | + ) |
| 200 | + |
| 201 | + |
| 202 | +# Add a heavy synchronous endpoint which receives milliseconds to sleep |
| 203 | +@app.get("/heavy_sync/{sleep_ms}", tags=["heavy"], operation_id="heavy_sync_with_sleep") |
| 204 | +async def heavy_sync_with_sleep(sleep_ms: int): |
| 205 | + """ |
| 206 | + A heavy synchronous endpoint that sleeps for the specified number of milliseconds. |
| 207 | +
|
| 208 | + This simulates a long-running synchronous operation. |
| 209 | + """ |
| 210 | + if sleep_ms < 0: |
| 211 | + raise HTTPException( |
| 212 | + status_code=400, |
| 213 | + detail="Sleep time must be a non-negative integer", |
| 214 | + ) |
| 215 | + |
| 216 | + import time |
| 217 | + |
| 218 | + with tracer.start_as_current_span("foo"): |
| 219 | + print(f"Sleeping for {sleep_ms} milliseconds") |
| 220 | + time.sleep(sleep_ms / 1000.0) |
| 221 | + return { |
| 222 | + "message": f"Slept for {sleep_ms} milliseconds", |
| 223 | + } |
0 commit comments