def configure_opentelemetry(flask_app: flask.Flask): """Configure a flask application to use OpenTelemetry. This activates the specific components: * sets tracer to the SDK's Tracer * enables requests integration on the Tracer * uses a WSGI middleware to enable configuration TODO: * processors? * exporters? """ # Start by configuring all objects required to ensure # a complete end to end workflow. # the preferred implementation of these objects must be set, # as the opentelemetry-api defines the interface with a no-op # implementation. trace.set_preferred_tracer_implementation(lambda _: Tracer()) # Next, we need to configure how the values that are used by # traces and metrics are propagated (such as what specific headers # carry this value). # TBD: can remove once default TraceContext propagators are installed. propagators.set_global_httptextformat(B3Format()) # 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. opentelemetry.ext.http_requests.enable(trace.tracer()) instrument_app(flask_app)
def setUpClass(cls): cls._tracer_source = TracerSource() cls._tracer = Tracer(cls._tracer_source, None) cls._span_exporter = InMemorySpanExporter() cls._span_processor = SimpleExportSpanProcessor(cls._span_exporter) cls._tracer_source.add_span_processor(cls._span_processor) trace_integration(cls._tracer) client = MongoClient(MONGODB_HOST, MONGODB_PORT, serverSelectionTimeoutMS=2000) db = client[MONGODB_DB_NAME] cls._collection = db[MONGODB_COLLECTION_NAME]
def setUpClass(cls): """Set preferred tracer implementation only once rather than before every test method. """ trace.set_preferred_tracer_implementation(lambda T: Tracer()) # Save current propagator to be restored on teardown. cls._previous_propagator = propagators.get_global_httptextformat() # Set mock propagator for testing. propagators.set_global_httptextformat(MockHTTPTextFormat)
def setUpClass(cls): cls._connection = None cls._cursor = None cls._tracer_provider = TracerProvider() cls._tracer = Tracer(cls._tracer_provider, None) cls._span_exporter = InMemorySpanExporter() cls._span_processor = SimpleExportSpanProcessor(cls._span_exporter) cls._tracer_provider.add_span_processor(cls._span_processor) trace_integration(cls._tracer) cls._connection = mysql.connector.connect( user=MYSQL_USER, password=MYSQL_PASSWORD, host=MYSQL_HOST, port=MYSQL_PORT, database=MYSQL_DB_NAME, ) cls._cursor = cls._connection.cursor()
def setUpClass(cls): cls._connection = None cls._cursor = None cls._tracer_provider = TracerProvider() cls._tracer = Tracer(cls._tracer_provider, None) cls._span_exporter = InMemorySpanExporter() cls._span_processor = SimpleExportSpanProcessor(cls._span_exporter) cls._tracer_provider.add_span_processor(cls._span_processor) trace_integration(cls._tracer) cls._connection = psycopg2.connect( dbname=POSTGRES_DB_NAME, user=POSTGRES_USER, password=POSTGRES_PASSWORD, host=POSTGRES_HOST, port=POSTGRES_PORT, ) cls._connection.set_session(autocommit=True) cls._cursor = cls._connection.cursor()
from flask import Flask, request, render_template from opentelemetry import trace, propagators from opentelemetry.sdk.trace import Tracer from opentelemetry.sdk.context.propagation.b3_format import B3Format from opentelemetry.ext.http_requests import enable from opentelemetry.ext.wsgi import OpenTelemetryMiddleware from opentelemetry.sdk.trace.export import ConsoleSpanExporter from opentelemetry.sdk.trace.export import SimpleExportSpanProcessor from kitchen_service import KitchenService from kitchen_consumer import KitchenConsumer from donut import Donut from status import NEW_ORDER trace.set_preferred_tracer_implementation(lambda T: Tracer()) propagators.set_global_httptextformat(B3Format()) tracer = trace.tracer() enable(tracer) tracer.add_span_processor(SimpleExportSpanProcessor(ConsoleSpanExporter())) app = Flask(__name__) app.static_folder = 'static' app.wsgi_app = OpenTelemetryMiddleware(app.wsgi_app) kitchen_service = KitchenService() kitchen_consumer = KitchenConsumer()
def tracer(): trace.set_preferred_tracer_implementation(lambda T: Tracer()) return trace.tracer()
def setUpClass(cls): """Set preferred tracer implementation only once rather than before every test method. """ trace.set_preferred_tracer_implementation(lambda T: Tracer())