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_provider_implementation( lambda _: TracerProvider()) # 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_provider()) instrument_app(flask_app)
def test_try_set_again(self): self.assertTrue(trace.tracer_provider()) # Try setting after the tracer_provider has already been created: with self.assertRaises(RuntimeError) as einfo: trace.set_preferred_tracer_provider_implementation( get_opentelemetry_implementation) self.assertIn("already loaded", str(einfo.exception))
def setUpClass(cls): global _MEMORY_EXPORTER # pylint:disable=global-statement trace_api.set_preferred_tracer_provider_implementation( lambda T: TracerProvider()) tracer_provider = trace_api.tracer_provider() _MEMORY_EXPORTER = InMemorySpanExporter() span_processor = export.SimpleExportSpanProcessor(_MEMORY_EXPORTER) tracer_provider.add_span_processor(span_processor)
def setUpClass(cls): """Set preferred tracer implementation only once rather than before every test method. """ trace.set_preferred_tracer_provider_implementation( lambda T: TracerProvider()) # 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)
import time from opentelemetry import trace from opentelemetry.ext import jaeger from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchExportSpanProcessor trace.set_preferred_tracer_provider_implementation(lambda T: TracerProvider()) tracer = trace.get_tracer(__name__) # create a JaegerSpanExporter jaeger_exporter = jaeger.JaegerSpanExporter( service_name="my-helloworld-service", # configure agent agent_host_name="localhost", agent_port=6831, # optional: configure also collector # collector_host_name="localhost", # collector_port=14268, # collector_endpoint="/api/traces?format=jaeger.thrift", # username=xxxx, # optional # password=xxxx, # optional ) # create a BatchExportSpanProcessor and add the exporter to it span_processor = BatchExportSpanProcessor(jaeger_exporter) # add to the tracer factory trace.tracer_provider().add_span_processor(span_processor) # create some spans for testing
def test_preferred_impl(self): trace.set_preferred_tracer_provider_implementation( get_opentelemetry_implementation) tracer_provider = trace.tracer_provider() self.assertIs(tracer_provider, DUMMY_TRACER_PROVIDER)