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_source_implementation(lambda _: TracerSource()) # 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). # 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_source()) instrument_app(flask_app)
def tracer(): exporter = ConsoleSpanExporter() span_processor = SimpleExportSpanProcessor(exporter) trace.set_preferred_tracer_source_implementation(lambda T: TracerSource()) span_processor = SimpleExportSpanProcessor(exporter) trace.tracer_source().add_span_processor(span_processor) return trace.get_tracer(__name__)
def setUpClass(cls): global _MEMORY_EXPORTER # pylint:disable=global-statement trace_api.set_preferred_tracer_source_implementation( lambda T: TracerSource()) tracer_source = trace_api.tracer_source() _MEMORY_EXPORTER = InMemorySpanExporter() span_processor = export.SimpleExportSpanProcessor(_MEMORY_EXPORTER) tracer_source.add_span_processor(span_processor)
def _grpc(): """Start discount gRPC server.""" logging.basicConfig(level=logging.INFO) engine = create_engine(settings.DATABASE_URL) Session = sessionmaker(bind=engine) connection = engine.connect() session = Session(bind=connection) exporter = ConsoleSpanExporter() if settings.TRACER_ENDPOINT_HOST and settings.TRACER_ENDPOINT_PORT: exporter = JaegerSpanExporter( service_name="promotion-grpc", agent_host_name=settings.TRACER_ENDPOINT_HOST, agent_port=settings.TRACER_ENDPOINT_PORT, ) trace.set_preferred_tracer_source_implementation(lambda T: TracerSource()) tracer = trace.get_tracer(__name__) ## span_processor = BatchExportSpanProcessor(exporter) span_processor = SimpleExportSpanProcessor(exporter) trace.tracer_source().add_span_processor(span_processor) tracer = trace.get_tracer(__name__) user_store = UserDataStore(session, tracer) user_case = UserUseCase(user_store, tracer) order_store = OrderDataStore(session, tracer) order_case = OrderUseCase(order_store, tracer) balance_client = BalanceClient(settings.BALANCE_TOKEN) balance_case = BalanceUseCase(order_case, balance_client, tracer) holiday_store = HolidayDataStore(settings.BLACK_FRIDAY_DATE, tracer) holiday_case = HolidayUseCase(holiday_store, tracer) authentication_case = AuthenticationUseCase(user_case, tracer) case = PromotionUseCase(discounts=[holiday_case, user_case], tracer=tracer) servicer = PromotionServicer(case, user_case, order_case, balance_case, authentication_case, tracer) server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) service.add_PromotionAPIServicer_to_server(servicer, server) logger.info("Listenning on port 50051.") server.add_insecure_port("[::]:50051") server.start() server.wait_for_termination()
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_source_implementation( lambda T: TracerSource()) # 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 setup_tracer(service_name): jaeger_exporter = jaeger.JaegerSpanExporter( service_name=service_name, # configure agent agent_host_name='jaeger', agent_port=6831, ) span_processor = BatchExportSpanProcessor(jaeger_exporter) trace.set_preferred_tracer_source_implementation(lambda T: TracerSource()) trace.tracer_source().add_span_processor(span_processor) postgres_tracer = trace.tracer_source().get_tracer('postgres') trace_integration(postgres_tracer)
def tracer(): trace.set_preferred_tracer_source_implementation(lambda T: TracerSource()) return trace.get_tracer(__name__)
) if os.getenv("EXPORTER") == "jaeger": from opentelemetry.ext.jaeger import JaegerSpanExporter exporter = JaegerSpanExporter( service_name="basic-service", agent_host_name="localhost", agent_port=6831, ) else: exporter = ConsoleSpanExporter() # The preferred tracer implementation must be set, as the opentelemetry-api # defines the interface with a no-op implementation. trace.set_preferred_tracer_source_implementation(lambda T: TracerSource()) # We tell OpenTelemetry who it is that is creating spans. In this case, we have # no real name (no setup.py), so we make one up. If we had a version, we would # also specify it here. tracer = trace.tracer_source().get_tracer(__name__) # SpanExporter receives the spans and send them to the target location. span_processor = BatchExportSpanProcessor(exporter) trace.tracer_source().add_span_processor(span_processor) with tracer.start_as_current_span("foo"): with tracer.start_as_current_span("bar"): with tracer.start_as_current_span("baz"): print(Context)