Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion for making provider config optional #2

Open
wants to merge 17 commits into
base: issue_123
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion docs/api/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ OpenTelemetry Python API
context
metrics
trace
util.loader
4 changes: 0 additions & 4 deletions docs/api/util.loader.rst

This file was deleted.

25 changes: 25 additions & 0 deletions docs/examples/basic_tracer/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2020, OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from os import environ


def pytest_sessionstart(session): # pylint: disable=unused-argument
environ["OPENTELEMETRY_PYTHON_TRACER_PROVIDER"] = "sdk_tracer_provider"
environ["OPENTELEMETRY_PYTHON_METER_PROVIDER"] = "sdk_meter_provider"


def pytest_sessionfinish(session): # pylint: disable=unused-argument
environ.pop("OPENTELEMETRY_PYTHON_TRACER_PROVIDER")
environ.pop("OPENTELEMETRY_PYTHON_METER_PROVIDER")
7 changes: 1 addition & 6 deletions docs/examples/basic_tracer/tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import os

from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import (
BatchExportSpanProcessor,
ConsoleSpanExporter,
Expand Down Expand Up @@ -45,10 +44,6 @@
print("Using ConsoleSpanExporter")
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())

# 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.
Expand All @@ -57,7 +52,7 @@
# SpanExporter receives the spans and send them to the target location.
span_processor = BatchExportSpanProcessor(exporter)

trace.tracer_provider().add_span_processor(span_processor)
trace.get_tracer_provider().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"):
Expand Down
8 changes: 2 additions & 6 deletions docs/examples/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
from opentelemetry import trace
from opentelemetry.ext import http_requests
from opentelemetry.ext.wsgi import OpenTelemetryMiddleware
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import (
BatchExportSpanProcessor,
ConsoleSpanExporter,
Expand All @@ -39,19 +38,16 @@
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)
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.
http_requests.enable(trace.tracer_provider())
http_requests.enable(trace.get_tracer_provider())
app = flask.Flask(__name__)
app.wsgi_app = OpenTelemetryMiddleware(app.wsgi_app)

Expand Down
25 changes: 25 additions & 0 deletions docs/examples/http/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2020, OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from os import environ


def pytest_sessionstart(session): # pylint: disable=unused-argument
environ["OPENTELEMETRY_PYTHON_TRACER_PROVIDER"] = "sdk_tracer_provider"
environ["OPENTELEMETRY_PYTHON_METER_PROVIDER"] = "sdk_meter_provider"


def pytest_sessionfinish(session): # pylint: disable=unused-argument
environ.pop("OPENTELEMETRY_PYTHON_TRACER_PROVIDER")
environ.pop("OPENTELEMETRY_PYTHON_METER_PROVIDER")
6 changes: 1 addition & 5 deletions docs/examples/http/tracer_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

from opentelemetry import trace
from opentelemetry.ext import http_requests
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import (
BatchExportSpanProcessor,
ConsoleSpanExporter,
Expand All @@ -37,10 +36,7 @@
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()
tracer_provider = trace.get_tracer_provider()

# SpanExporter receives the spans and send them to the target location.
span_processor = BatchExportSpanProcessor(exporter)
Expand Down
3 changes: 1 addition & 2 deletions docs/examples/metrics/observer_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@
import psutil

from opentelemetry import metrics
from opentelemetry.sdk.metrics import LabelSet, MeterProvider
from opentelemetry.sdk.metrics import LabelSet
from opentelemetry.sdk.metrics.export import ConsoleMetricsExporter
from opentelemetry.sdk.metrics.export.batcher import UngroupedBatcher
from opentelemetry.sdk.metrics.export.controller import PushController

# Configure a stateful batcher
batcher = UngroupedBatcher(stateful=True)

metrics.set_preferred_meter_provider_implementation(lambda _: MeterProvider())
meter = metrics.get_meter(__name__)

# Exporter to export metrics to the console
Expand Down
3 changes: 1 addition & 2 deletions docs/examples/metrics/prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@

from opentelemetry import metrics
from opentelemetry.ext.prometheus import PrometheusMetricsExporter
from opentelemetry.sdk.metrics import Counter, MeterProvider
from opentelemetry.sdk.metrics import Counter
from opentelemetry.sdk.metrics.export.controller import PushController

# Start Prometheus client
start_http_server(port=8000, addr="localhost")

# Meter is responsible for creating and recording metrics
metrics.set_preferred_meter_provider_implementation(lambda _: MeterProvider())
meter = metrics.get_meter(__name__)
# exporter to export metrics to Prometheus
prefix = "MyAppPrefix"
Expand Down
5 changes: 1 addition & 4 deletions docs/examples/metrics/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,10 @@
import time

from opentelemetry import metrics
from opentelemetry.sdk.metrics import Counter, MeterProvider
from opentelemetry.sdk.metrics import Counter
from opentelemetry.sdk.metrics.export import ConsoleMetricsExporter
from opentelemetry.sdk.metrics.export.controller import PushController

# The preferred tracer implementation must be set, as the opentelemetry-api
# defines the interface with a no-op implementation.
metrics.set_preferred_meter_provider_implementation(lambda _: MeterProvider())
# Meter is responsible for creating and recording metrics
meter = metrics.get_meter(__name__)
# exporter to export metrics to the console
Expand Down
3 changes: 1 addition & 2 deletions docs/examples/metrics/simple_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import time

from opentelemetry import metrics
from opentelemetry.sdk.metrics import Counter, Measure, MeterProvider
from opentelemetry.sdk.metrics import Counter, Measure
from opentelemetry.sdk.metrics.export import ConsoleMetricsExporter
from opentelemetry.sdk.metrics.export.controller import PushController

Expand All @@ -44,7 +44,6 @@ def usage(argv):
sys.exit(1)

# Meter is responsible for creating and recording metrics
metrics.set_preferred_meter_provider_implementation(lambda _: MeterProvider())

# Meter's namespace corresponds to the string passed as the first argument Pass
# in True/False to indicate whether the batcher is stateful. True indicates the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import opentelemetry.ext.http_requests
from opentelemetry import trace
from opentelemetry.ext.flask import instrument_app
from opentelemetry.sdk.trace import TracerProvider


def configure_opentelemetry(flask_app: flask.Flask):
Expand All @@ -42,20 +41,14 @@ def configure_opentelemetry(flask_app: flask.Flask):
"""
# 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())
opentelemetry.ext.http_requests.enable(trace.get_tracer_provider())
instrument_app(flask_app)


Expand Down
5 changes: 1 addition & 4 deletions docs/examples/opentracing/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
from opentelemetry import trace
from opentelemetry.ext import opentracing_shim
from opentelemetry.ext.jaeger import JaegerSpanExporter
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleExportSpanProcessor
from rediscache import RedisCache

# Configure the tracer using the default implementation
trace.set_preferred_tracer_provider_implementation(lambda T: TracerProvider())
tracer_provider = trace.tracer_provider()
tracer_provider = trace.get_tracer_provider()

# Configure the tracer to export traces to Jaeger
jaeger_exporter = JaegerSpanExporter(
Expand Down
3 changes: 1 addition & 2 deletions examples/metrics/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@
from opentelemetry.ext.otcollector.metrics_exporter import (
CollectorMetricsExporter,
)
from opentelemetry.sdk.metrics import Counter, MeterProvider
from opentelemetry.sdk.metrics import Counter
from opentelemetry.sdk.metrics.export.controller import PushController

# Meter is responsible for creating and recording metrics
metrics.set_preferred_meter_provider_implementation(lambda _: MeterProvider())
meter = metrics.get_meter(__name__)
# exporter to export metrics to OT Collector
exporter = CollectorMetricsExporter(
Expand Down
1 change: 0 additions & 1 deletion ext/opentelemetry-ext-dbapi/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ Usage
from opentelemetry.trace import tracer_provider
from opentelemetry.ext.dbapi import trace_integration

trace.set_preferred_tracer_provider_implementation(lambda T: TracerProvider())
tracer = trace.get_tracer(__name__)
# Ex: mysql.connector
trace_integration(tracer_provider(), mysql.connector, "connect", "mysql")
Expand Down
25 changes: 25 additions & 0 deletions ext/opentelemetry-ext-flask/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2020, OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from os import environ


def pytest_sessionstart(session): # pylint: disable=unused-argument
environ["OPENTELEMETRY_PYTHON_TRACER_PROVIDER"] = "sdk_tracer_provider"
environ["OPENTELEMETRY_PYTHON_METER_PROVIDER"] = "sdk_meter_provider"


def pytest_sessionfinish(session): # pylint: disable=unused-argument
environ.pop("OPENTELEMETRY_PYTHON_TRACER_PROVIDER")
environ.pop("OPENTELEMETRY_PYTHON_METER_PROVIDER")
2 changes: 0 additions & 2 deletions ext/opentelemetry-ext-jaeger/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ gRPC is still not supported by this implementation.

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

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
Expand All @@ -26,7 +24,7 @@
span_processor = BatchExportSpanProcessor(jaeger_exporter)

# add to the tracer factory
trace.tracer_provider().add_span_processor(span_processor)
trace.get_tracer_provider().add_span_processor(span_processor)

# create some spans for testing
with tracer.start_as_current_span("foo") as foo:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,8 @@
import time

from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.ext.opentracing_shim import create_tracer

# Tell OpenTelemetry which Tracer implementation to use.
trace.set_preferred_tracer_provider_implementation(lambda T: TracerProvider())

# Create an OpenTelemetry Tracer.
otel_tracer = trace.get_tracer(__name__)

Expand Down
25 changes: 25 additions & 0 deletions ext/opentelemetry-ext-opentracing-shim/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2020, OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from os import environ


def pytest_sessionstart(session): # pylint: disable=unused-argument
environ["OPENTELEMETRY_PYTHON_TRACER_PROVIDER"] = "sdk_tracer_provider"
environ["OPENTELEMETRY_PYTHON_METER_PROVIDER"] = "sdk_meter_provider"


def pytest_sessionfinish(session): # pylint: disable=unused-argument
environ.pop("OPENTELEMETRY_PYTHON_TRACER_PROVIDER")
environ.pop("OPENTELEMETRY_PYTHON_METER_PROVIDER")
11 changes: 1 addition & 10 deletions ext/opentelemetry-ext-opentracing-shim/tests/test_shim.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
from opentelemetry import propagators, trace
from opentelemetry.context import Context
from opentelemetry.ext.opentracing_shim import util
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.trace.propagation import (
get_span_from_context,
set_span_in_context,
Expand All @@ -44,18 +43,10 @@ class TestShim(TestCase):
def setUp(self):
"""Create an OpenTelemetry tracer and a shim before every test case."""

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

@classmethod
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()

Expand Down
Loading