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))
예제 #2
0
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)
예제 #3
0
 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 do_test_get_envvar(self, envvar_suffix: str) -> None:
        global DUMMY_TRACER_PROVIDER  # pylint:disable=global-statement

        # Test is not runnable with this!
        self.assertFalse(sys.flags.ignore_environment)

        envname = "OPENTELEMETRY_PYTHON_IMPLEMENTATION_" + envvar_suffix
        os.environ[envname] = __name__
        try:
            tracer_provider = trace.tracer_provider()
            self.assertIs(tracer_provider, DUMMY_TRACER_PROVIDER)
        finally:
            DUMMY_TRACER_PROVIDER = None
            del os.environ[envname]
        self.assertIs(type(tracer_provider), DummyTracerProvider)
    # 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
with tracer.start_as_current_span("foo") as foo:
    time.sleep(0.1)
    foo.set_attribute("my_atribbute", True)
    foo.add_event("event in foo", {"name": "foo1"})
    with tracer.start_as_current_span("bar",
                                      links=[trace.Link(foo.get_context())
                                             ]) as bar:
        time.sleep(0.2)
        bar.set_attribute("speed", 100.0)

        with tracer.start_as_current_span("baz") as baz:
            time.sleep(0.3)
            baz.set_attribute("name", "mauricio")
from opentelemetry.sdk.trace.export import (
    BatchExportSpanProcessor,
    ConsoleSpanExporter,
)

if os.getenv("EXPORTER") == "jaeger":
    from opentelemetry.ext.jaeger import JaegerSpanExporter

    exporter = JaegerSpanExporter(
        service_name="http-client",
        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_provider_implementation(lambda T: TracerProvider())
tracer_provider = trace.tracer_provider()

# SpanExporter receives the spans and send them to the target location.
span_processor = BatchExportSpanProcessor(exporter)
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.
http_requests.enable(tracer_provider)
response = requests.get(url="http://127.0.0.1:5000/")
 def do_test_preferred_impl(self, setter: Callable[[Any], Any]) -> None:
     setter(get_opentelemetry_implementation)
     tracer_provider = trace.tracer_provider()
     self.assertIs(tracer_provider, DUMMY_TRACER_PROVIDER)
 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)
 def test_get_default(self):
     tracer_provider = trace.tracer_provider()
     self.assertIs(type(tracer_provider), trace.DefaultTracerProvider)
예제 #10
0
    exporter = JaegerSpanExporter(
        service_name="http-server",
        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_provider_implementation(lambda T: TracerProvider())
tracer = trace.get_tracer(__name__)

# SpanExporter receives the spans and send them to the target location.
span_processor = BatchExportSpanProcessor(exporter)
trace.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.
http_requests.enable(trace.tracer_provider())
app = flask.Flask(__name__)
app.wsgi_app = OpenTelemetryMiddleware(app.wsgi_app)


@app.route("/")
def hello():
    with tracer.start_as_current_span("parent"):
        requests.get("https://www.wikipedia.org/wiki/Rabbit")
    return "hello"
예제 #11
0
    def setUp(self):
        """Create an OpenTelemetry tracer and a shim before every test case."""

        self.shim = opentracingshim.create_tracer(trace.tracer_provider())