def __init__(self, arguments): super().__init__(arguments) # auth configuration connection_string = self.get_from_env( "APPLICATIONINSIGHTS_CONNECTION_STRING") # Create clients self.exporter = AzureMonitorTraceExporter.from_connection_string( connection_string) trace.set_tracer_provider(TracerProvider()) tracer = trace.get_tracer(__name__) self.spans_list = [] for _ in range(self.args.num_spans): with tracer.start_as_current_span(name="name") as span: self.spans_list.append(span)
import requests from opentelemetry import trace from opentelemetry.instrumentation.flask import FlaskInstrumentor from opentelemetry.instrumentation.requests import RequestsInstrumentor from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchExportSpanProcessor import flask from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter # The preferred tracer implementation must be set, as the opentelemetry-api # defines the interface with a no-op implementation. trace.set_tracer_provider(TracerProvider()) tracer = trace.get_tracer(__name__) exporter = AzureMonitorTraceExporter.from_connection_string( os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"]) # SpanExporter receives the spans and send them to the target location. span_processor = BatchExportSpanProcessor(exporter) trace.get_tracer_provider().add_span_processor(span_processor) # Integrations are the glue that binds the OpenTelemetry API and the # frameworks and libraries that are used together, automatically creating # Spans and propagating context as appropriate. # Enable instrumentation in the requests library. RequestsInstrumentor().instrument() app = flask.Flask(__name__) # Enable instrumentation in the flask library. FlaskInstrumentor().instrument_app(app)
from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter app = Flask(__name__) app.config.from_object(Config) db = SQLAlchemy(app) # Set global TracerProvider before instrumenting trace.set_tracer_provider( TracerProvider(resource=Resource.create({SERVICE_NAME: "flask-sample"}))) # Enable tracing for sqlalchemy library SQLAlchemyInstrumentor().instrument() # Enable tracing for Flask library FlaskInstrumentor().instrument_app(app) # Enable tracing for requests library RequestsInstrumentor().instrument() trace_exporter = AzureMonitorTraceExporter( connection_string=Config.CONNECTION_STRING) trace.get_tracer_provider().add_span_processor( BatchSpanProcessor(trace_exporter)) # Import here to avoid circular imports from app import routes # noqa isort:skip app.run(host='localhost', port=5000)
# Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. """ An example to show an application instrumented with the Opentelemetry requests instrumentations. Calls made with the requests library will be automatically tracked and telemetry is exported to application insights with the AzureMonitorTraceExporter. """ import os import requests from opentelemetry import trace from opentelemetry.instrumentation.requests import RequestsInstrumentor from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchExportSpanProcessor from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter trace.set_tracer_provider(TracerProvider()) tracer = trace.get_tracer(__name__) RequestsInstrumentor().instrument() span_processor = BatchExportSpanProcessor( AzureMonitorTraceExporter( connection_string=os.environ["APPLICATIONINSIGHTS_CONNECTION_STRING"])) trace.get_tracer_provider().add_span_processor(span_processor) with tracer.start_as_current_span("parent"): response = requests.get("https://azure.microsoft.com/", timeout=5)
import psycopg2 from opentelemetry import trace from opentelemetry.instrumentation.psycopg2 import Psycopg2Instrumentor from opentelemetry.sdk.resources import SERVICE_NAME, Resource from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchSpanProcessor from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter # Set global TracerProvider before instrumenting trace.set_tracer_provider( TracerProvider(resource=Resource.create({SERVICE_NAME: "db-sample"}))) # Enable tracing for psycopg2 library Psycopg2Instrumentor().instrument() trace_exporter = AzureMonitorTraceExporter() trace.get_tracer_provider().add_span_processor( BatchSpanProcessor(trace_exporter)) cnx = psycopg2.connect(database='test', user='******', password='******') cursor = cnx.cursor() cursor.execute("CREATE TABLE test (testField integer);") cursor.execute("INSERT INTO test (testField) VALUES (123)") cursor.close() cnx.close()
) ) ) logging.basicConfig( format="%(asctime)s %(levelname)s: %(message)s", level=logging.INFO, datefmt="%d-%b-%y %H:%M:%S", ) logger = logging.getLogger(__name__) logger.setLevel(logging.INFO) logger.addHandler(AzureLogHandler(connection_string="InstrumentationKey=<your instrumentation key")) exporter = AzureMonitorTraceExporter.from_connection_string( "InstrumentationKey=<your instrumentation key>" ) trace.set_tracer_provider(TracerProvider()) tracer = trace.get_tracer(__name__) span_processor = BatchSpanProcessor(exporter) trace.get_tracer_provider().add_span_processor(span_processor) @ dataclass class RunJob: @ staticmethod def animation(): anime = "|/-\\" for i in range(10):