Migrating Python 3.10 Project from OpenCensus to OpenTelemetry: Logging Message Exception Details with Application Insights
OpenCensus and OpenTelemetry are popular tools for distributed tracing, monitoring, and analytics. OpenCensus has been deprecated and the community has moved towards using OpenTelemetry. This article covers the process of migrating a Python 3.10 project from OpenCensus to OpenTelemetry and the configuration required for logging message exception details with Application Insights.
Introduction to OpenCensus and OpenTelemetry
OpenCensus is a set of libraries for collecting trace and metric data from applications and was created by Google. OpenTelemetry, on the other hand, is a collection of tools, APIs, and SDKs that can be used to generate, collect, and export telemetry data. OpenTelemetry is an open-source project that is a collaboration between the OpenCensus and OpenTracing communities.
Why Migrate from OpenCensus to OpenTelemetry?
OpenCensus has been deprecated and the community has moved towards using OpenTelemetry. Migrating from OpenCensus to OpenTelemetry allows you to take advantage of the ongoing development and support of OpenTelemetry, as well as the larger community. OpenTelemetry also provides a unified tracing and monitoring solution for both distributed tracing and metrics.
Migrating from OpenCensus to OpenTelemetry in Python 3.10
Migrating from OpenCensus to OpenTelemetry in Python 3.10 involves installing the OpenTelemetry package, modifying the code to use the OpenTelemetry APIs and SDKs, and configuring the appropriate exporters. The following steps will guide you through the process:
- Install the OpenTelemetry package by running
pip install opentelemetry-api opentelemetry-sdk opentelemetry-exporter-otlp opentelemetry-instrumentation-static_instrumentation opentelemetry-contrib-instrumentation-requests opentelemetry-contrib-instrumentation-sqlalchemy. - Modify the code to use the OpenTelemetry APIs and SDKs. This will involve replacing any OpenCensus functions and classes with their OpenTelemetry equivalents.
- Configure the OpenTelemetry exporter for Application Insights. This can be done by creating a configuration file or by using code. The following code shows an example of configuring the exporter using code:
from opentelemetry.exporter.otlp.trace\_exporter import OTLPTraceExporter from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.resources import SERVICE\_NAME, \ SERVICE\_VERSION from opentelemetry.instrumentation.requests import RequestsInstrumentor from opentelemetry.sdk.trace.export import BatchSpanProcessor exporter = OTLPTraceExporter(endpoint="your-application-insights-endpoint") provider = TracerProvider(resource=SERVICE\_NAME("your-service-name"), service\_version="your-service-version") processor = BatchSpanProcessor(exporter) provider.add\_span\_processor(processor) RequestsInstrumentor().instrument(provider) - Run the modified code and verify that the telemetry data is being sent to Application Insights.
Logging Message Exception Details
To log message exception details with Application Insights, you can use the OpenTelemetry Span.set\_attribute() method. This will add additional attributes to the span, which will be included in the telemetry data. The following is an example of using the Span.set\_attribute() method:
from opentelemetry import trace
with trace.start\_as\_current\_span("name-of-span"):
try:
# code that may raise an exception
except Exception as e:
trace.get\_current\_span().set\_attribute("exception.message", str(e))
trace.get\_current\_span().set\_attribute("exception.type", type(e))
Migrating a Python 3.10 project from OpenCensus to OpenTelemetry involves installing the OpenTelemetry package, modifying the code to use the OpenTelemetry APIs and SDKs, and configuring the appropriate exporters. To log message exception details with Application Insights, you can use the Span.set\_attribute() method. By migrating from OpenCensus to OpenTelemetry, you can take advantage of the ongoing development and support of OpenTelemetry, as well as the larger community.
References
- OpenTelemetry: https://opentelemetry.io/
- Application Insights: https://azure.microsoft.com/en-us/services/application-insights/
- OpenTelemetry Python SDK: https://github.com/open-telemetry/opentelemetry-python-sdk
- OpenTelemetry Python Instrumentation: https://github.com/open-telemetry/opentelemetry-python-contrib