Skip to content

Commit a898328

Browse files
author
ks6088ts
committed
add metrics
1 parent 6d3f044 commit a898328

File tree

3 files changed

+45
-10
lines changed

3 files changed

+45
-10
lines changed

docs/index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@
2525
- [FastAPI のテレメトリデータを Azure Application Insights に送る](https://qiita.com/hoto17296/items/2f366dfabdbe3d1d4e97)
2626
- [【Azure Functions】 - Application Insights のログが表示されない問題](https://zenn.dev/headwaters/articles/ff19f7e1b99b44)
2727
- [opentelemetry-instrumentation-fastapi (python) から OpenTelemetry に入門する](https://zenn.dev/taxin/articles/opentelemetry-fast-api-instrumentation-basics)
28+
29+
## OpenTelemetry
30+
31+
- [Docs > Language APIs & SDKs > Python > Getting Started](https://opentelemetry.io/docs/languages/python/getting-started/)

template_fastapi/app.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,14 @@
99

1010
from azure.monitor.opentelemetry import configure_azure_monitor
1111
from fastapi import FastAPI, HTTPException, Query
12-
from opentelemetry import trace
1312
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-
)
1913
from opentelemetry.trace import Span
2014
from pydantic import BaseModel
2115

22-
trace.set_tracer_provider(TracerProvider())
23-
trace.get_tracer_provider().add_span_processor(BatchSpanProcessor(ConsoleSpanExporter()))
24-
tracer = trace.get_tracer(__name__)
16+
from template_fastapi.opentelemetry import get_meter, get_tracer
2517

18+
tracer = get_tracer(__name__)
19+
meter = get_meter(__name__)
2620
app = FastAPI()
2721

2822
# If APPLICATIONINSIGHTS_CONNECTION_STRING exists, configure Azure Monitor
@@ -189,6 +183,7 @@ async def flaky(failure_rate: int):
189183

190184

191185
# Add flaky API which raises an exception
186+
@tracer.start_as_current_span("flaky_exception")
192187
@app.get("/flaky/exception", tags=["flaky"], operation_id="flaky_exception")
193188
async def flaky_exception():
194189
"""
@@ -216,9 +211,29 @@ async def heavy_sync_with_sleep(sleep_ms: int):
216211

217212
import time
218213

219-
with tracer.start_as_current_span("foo"):
214+
with tracer.start_as_current_span("parent"):
220215
print(f"Sleeping for {sleep_ms} milliseconds")
221216
time.sleep(sleep_ms / 1000.0)
217+
with tracer.start_as_current_span("child"):
218+
print("Child span")
222219
return {
223220
"message": f"Slept for {sleep_ms} milliseconds",
224221
}
222+
223+
224+
# Add counter for dice rolls
225+
roll_counter = meter.create_counter(
226+
"dice.rolls",
227+
description="The number of rolls by roll value",
228+
)
229+
230+
231+
@app.get("/roll_dice", operation_id="roll_dice")
232+
async def roll_dice():
233+
"""
234+
Simulate rolling a dice and record the roll in the meter.
235+
"""
236+
with tracer.start_as_current_span("roll_dice"):
237+
roll = random.randint(1, 6)
238+
roll_counter.add(1, {"roll.value": str(roll)})
239+
return roll

template_fastapi/opentelemetry.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from opentelemetry import metrics, trace
2+
from opentelemetry.sdk.trace import TracerProvider
3+
from opentelemetry.sdk.trace.export import (
4+
BatchSpanProcessor,
5+
ConsoleSpanExporter,
6+
)
7+
8+
9+
def get_tracer(name: str):
10+
trace.set_tracer_provider(TracerProvider())
11+
trace.get_tracer_provider().add_span_processor(BatchSpanProcessor(ConsoleSpanExporter()))
12+
return trace.get_tracer(name)
13+
14+
15+
def get_meter(name: str):
16+
return metrics.get_meter(name)

0 commit comments

Comments
 (0)