Skip to content

Commit

Permalink
Update docs to include flush/shutdown behavior, add fastapi example (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
lzchen authored Jul 25, 2023
1 parent 336569b commit 71998fb
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 10 deletions.
19 changes: 18 additions & 1 deletion sdk/monitor/azure-monitor-opentelemetry-exporter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ logging.getLogger().setLevel(logging.NOTSET)
logger = logging.getLogger(__name__)

logger.warning("Hello World!")

# Telemetry records are flushed automatically upon application exit
# If you would like to flush records manually yourself, you can call force_flush()
logger_provider.force_flush()
```

#### Export Correlated Log
Expand Down Expand Up @@ -397,6 +401,10 @@ histogram.record(99.9)
# Async Gauge
gauge = meter.create_observable_gauge("gauge", [observable_gauge_func])

# Upon application exit, one last collection is made and telemetry records are
# flushed automatically. # If you would like to flush records manually yourself,
# you can call force_flush()
meter_provider.force_flush()
```

#### Metric custom views
Expand Down Expand Up @@ -521,7 +529,8 @@ from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter

trace.set_tracer_provider(TracerProvider())
tracer_provider = TracerProvider()
trace.set_tracer_provider(tracer_provider)
tracer = trace.get_tracer(__name__)
# This is the exporter that sends data to Application Insights
exporter = AzureMonitorTraceExporter(
Expand All @@ -532,6 +541,10 @@ trace.get_tracer_provider().add_span_processor(span_processor)

with tracer.start_as_current_span("hello"):
print("Hello, World!")

# Telemetry records are flushed automatically upon application exit
# If you would like to flush records manually yourself, you can call force_flush()
tracer_provider.force_flush()
```

#### Instrumentation with requests library
Expand Down Expand Up @@ -611,6 +624,10 @@ for i in range(100):
print("Hello, World!")
```

## Flush/shutdown behavior

For all applications set up with OpenTelemetry SDK and Azure Monitor exporters, telemetry is flushed automatically upon application exit. Note that this does not include when application ends abruptly or crashes due to uncaught exception.

## Troubleshooting

The exporter raises exceptions defined in [Azure Core](https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/core/azure-core/README.md#azure-core-library-exceptions).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@

from azure.monitor.opentelemetry.exporter import AzureMonitorLogExporter

set_logger_provider(LoggerProvider())
logger_provider = LoggerProvider()
set_logger_provider(logger_provider)
exporter = AzureMonitorLogExporter.from_connection_string(
os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]
)
get_logger_provider().add_log_record_processor(BatchLogRecordProcessor(exporter))
get_logger_provider().add_log_record_processor(BatchLogRecordProcessor(exporter, schedule_delay_millis=60000))

# Attach LoggingHandler to namespaced logger
handler = LoggingHandler()
Expand All @@ -33,4 +34,6 @@

logger.info("Hello World!")

input()
# Telemetry records are flushed automatically upon application exit
# If you would like to flush records manually yourself, you can call force_flush()
logger_provider.force_flush()
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]
)
# Metrics are reported every 1 minute
reader = PeriodicExportingMetricReader(exporter)
metrics.set_meter_provider(MeterProvider(metric_readers=[reader]))
reader = PeriodicExportingMetricReader(exporter,export_interval_millis=60000)
meter_provider = MeterProvider(metric_readers=[reader])
metrics.set_meter_provider(meter_provider)

# Create a namespaced meter
meter = metrics.get_meter_provider().get_meter("sample")
Expand Down Expand Up @@ -66,4 +67,9 @@ def observable_gauge_func(options: CallbackOptions) -> Iterable[Observation]:
# Async Gauge
gauge = meter.create_observable_gauge("gauge", [observable_gauge_func])

# cSpell:disable
# Upon application exit, one last collection is made and telemetry records are
# flushed automatically. # If you would like to flush records manually yourself,
# you can call force_flush()
meter_provider.force_flush()

# cSpell:disable
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
"""
An example to show an application instrumented with the OpenTelemetry flask instrumentation.
Calls made with the flask library will be automatically tracked and telemetry is exported to
application insights with the AzureMonitorTraceExporter.
See more info on the flask instrumentation here:
https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-flask
"""
from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter
from fastapi import Depends, FastAPI
from httpx import AsyncClient
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor

app = FastAPI()

tracer = trace.get_tracer(__name__)

async def get_client():
async with AsyncClient() as client:
yield client

@app.get("/")
async def read_root(client=Depends(get_client)):
with tracer.start_as_current_span("read_root"):
response = await client.get("https://httpbin.org/get")
return response.json()

if __name__ == "__main__":
# cSpell:disable
import uvicorn
trace.set_tracer_provider(TracerProvider())
exporter = AzureMonitorTraceExporter()
span_processor = BatchSpanProcessor(exporter)
trace.get_tracer_provider().add_span_processor(span_processor)
uvicorn.run("sample_fastapi:app", port=8008, reload=True)
# cSpell:disable

Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@

from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter


exporter = AzureMonitorTraceExporter.from_connection_string(
os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]
)

trace.set_tracer_provider(TracerProvider())
tracer_provider = TracerProvider()
trace.set_tracer_provider(tracer_provider)
tracer = trace.get_tracer(__name__)
span_processor = BatchSpanProcessor(exporter)
span_processor = BatchSpanProcessor(exporter, schedule_delay_millis=60000)
trace.get_tracer_provider().add_span_processor(span_processor)

with tracer.start_as_current_span("hello"):
print("Hello, World!")

# Telemetry records are flushed automatically upon application exit
# If you would like to flush records manually yourself, you can call force_flush()
tracer_provider.force_flush()

0 comments on commit 71998fb

Please sign in to comment.