def test_uninstrument(self): http_requests.RequestsInstrumentor().uninstrument() result = requests.get(self.URL) self.assertEqual(result.text, "Hello!") span_list = self.memory_exporter.get_finished_spans() self.assertEqual(len(span_list), 0) # instrument again to avoid annoying warning message http_requests.RequestsInstrumentor().instrument()
def test_custom_tracer_provider(self): resource = resources.Resource.create({}) result = self.create_tracer_provider(resource=resource) tracer_provider, exporter = result http_requests.RequestsInstrumentor().uninstrument() http_requests.RequestsInstrumentor().instrument( tracer_provider=tracer_provider) result = requests.get(self.URL) self.assertEqual(result.text, "Hello!") span_list = exporter.get_finished_spans() self.assertEqual(len(span_list), 1) span = span_list[0] self.assertIs(span.resource, resource)
def setUp(self): super().setUp() http_requests.RequestsInstrumentor().instrument() httpretty.enable() httpretty.register_uri( httpretty.GET, self.URL, body="Hello!", )
def test_uninstrument_session(self): session1 = requests.Session() http_requests.RequestsInstrumentor().uninstrument_session(session1) result = session1.get(self.URL) self.assertEqual(result.text, "Hello!") span_list = self.memory_exporter.get_finished_spans() self.assertEqual(len(span_list), 0) # Test that other sessions as well as global requests is still # instrumented session2 = requests.Session() result = session2.get(self.URL) self.assertEqual(result.text, "Hello!") span_list = self.memory_exporter.get_finished_spans() self.assertEqual(len(span_list), 1) self.memory_exporter.clear() result = requests.get(self.URL) self.assertEqual(result.text, "Hello!") span_list = self.memory_exporter.get_finished_spans() self.assertEqual(len(span_list), 1)
def tearDown(self): super().tearDown() http_requests.RequestsInstrumentor().uninstrument() httpretty.disable()
import os import requests from opentelemetry import trace from opentelemetry.ext import http_requests from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import ( BatchExportSpanProcessor, ConsoleSpanExporter, ) # The preferred tracer implementation must be set, as the opentelemetry-api # defines the interface with a no-op implementation. # It must be done before instrumenting any library. trace.set_tracer_provider(TracerProvider()) # Enable instrumentation in the requests library. http_requests.RequestsInstrumentor().instrument() # Configure a console span exporter. exporter = ConsoleSpanExporter() 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. response = requests.get(url="http://127.0.0.1:5000/")