Skip to content

Commit

Permalink
Revert public API errors
Browse files Browse the repository at this point in the history
  • Loading branch information
euniceek committed Jun 11, 2021
1 parent 092e341 commit 08542e1
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 56 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ env:
# Otherwise, set variable to the commit of your branch on
# opentelemetry-python-contrib which is compatible with these Core repo
# changes.
CONTRIB_REPO_SHA: ef05e54a3c188b9d313f74ec2196ba3337e6cbed
CONTRIB_REPO_SHA: 82c4f600872a72fedaebad758f0d5588dfb53a00

jobs:
build:
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.3.0-0.22b0...HEAD)

### Changed
- Updated `opentelemetry-opencensus-exporter` to use `service_name` of spans instead of resource
([#1897](https://github.com/open-telemetry/opentelemetry-python/pull/1897))

## [1.3.0-0.22b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.3.0-0.22b0) - 2021-06-01

### Added
Expand All @@ -26,6 +30,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Update protos to latest version release 0.9.0
([#1873](https://github.com/open-telemetry/opentelemetry-python/pull/1873))

### Fixed
- Updated `opentelementry-opentracing-shim` `ScopeShim` to report exceptions in
opentelemetry specification format, rather than opentracing spec format.

## [1.2.0, 0.21b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.2.0-0.21b0) - 2021-05-11

### Added
Expand Down
19 changes: 0 additions & 19 deletions docs-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,11 @@ sphinx-jekyll-builder
./opentelemetry-sdk

# Required by instrumentation and exporter packages
asgiref~=3.0
asyncpg>=0.12.0
ddtrace>=0.34.0
aiohttp~= 3.0
aiopg>=0.13.0
grpcio~=1.27
Deprecated>=1.2.6
django>=2.2
PyMySQL~=0.9.3
flask~=1.0
mysql-connector-python~=8.0
opentracing~=2.2.0
prometheus_client>=0.5.0,<1.0.0
psycopg2-binary>=2.7.3.1
pymemcache~=1.3
pymongo~=3.1
pyramid>=1.7
redis>=2.6
sqlalchemy>=1.0
thrift>=0.10.0
wrapt>=1.0.0,<2.0.0
celery>=4.0
psutil~=5.7.0
boto~=2.0
botocore~=1.0
starlette~=0.13
fastapi~=0.58.1
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,19 @@ def __init__(
else:
self.client = client

self.host_name = host_name
self.node = utils.get_node(service_name, host_name)

def export(self, spans: Sequence[ReadableSpan]) -> SpanExportResult:
# Populate service_name from first span
# We restrict any SpanProcessor to be only associated with a single
# TracerProvider, so it is safe to assume that all Spans in a single
# batch all originate from one TracerProvider (and in turn have all
# the same service_name)
if spans:
service_name = spans[0].resource.attributes.get(SERVICE_NAME)
if service_name:
self.node = utils.get_node(service_name, self.host_name)
try:
responses = self.client.Export(self.generate_span_requests(spans))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@

# pylint: disable=no-member
class TestCollectorSpanExporter(unittest.TestCase):
@mock.patch(
"opentelemetry.exporter.opencensus.trace_exporter.trace._TRACER_PROVIDER",
None,
)
def test_constructor(self):
mock_get_node = mock.Mock()
patch = mock.patch(
Expand Down Expand Up @@ -324,3 +328,46 @@ def test_export(self):
self.assertEqual(
getattr(output_identifier, "host_name"), "testHostName"
)

@mock.patch(
"opentelemetry.exporter.opencensus.trace_exporter.trace._TRACER_PROVIDER",
None,
)
def test_export_service_name(self):
trace_api.set_tracer_provider(
TracerProvider(
resource=Resource.create({SERVICE_NAME: "testServiceName"})
)
)
mock_client = mock.MagicMock()
mock_export = mock.MagicMock()
mock_client.Export = mock_export
host_name = "testHostName"
collector_exporter = OpenCensusSpanExporter(
client=mock_client, host_name=host_name
)
self.assertEqual(
collector_exporter.node.service_info.name, "testServiceName"
)

trace_id = 0x6E0C63257DE34C926F9EFCD03927272E
span_id = 0x34BF92DEEFC58C92
span_context = trace_api.SpanContext(
trace_id,
span_id,
is_remote=False,
trace_flags=TraceFlags(TraceFlags.SAMPLED),
)
resource = Resource.create({SERVICE_NAME: "test"})
otel_spans = [
trace._Span(
name="test1",
context=span_context,
kind=trace_api.SpanKind.CLIENT,
resource=resource,
)
]

result_status = collector_exporter.export(otel_spans)
self.assertEqual(SpanExportResult.SUCCESS, result_status)
self.assertEqual(collector_exporter.node.service_info.name, "test")
4 changes: 2 additions & 2 deletions opentelemetry-api/src/opentelemetry/baggage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
import typing
from types import MappingProxyType

from opentelemetry.context import _create_key, get_value, set_value
from opentelemetry.context import create_key, get_value, set_value
from opentelemetry.context.context import Context

_BAGGAGE_KEY = _create_key("baggage")
_BAGGAGE_KEY = create_key("baggage")


def get_all(
Expand Down
2 changes: 1 addition & 1 deletion opentelemetry-api/src/opentelemetry/context/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def wrapper( # type: ignore[misc]
return typing.cast(_F, wrapper) # type: ignore[misc]


def _create_key(keyname: str) -> str:
def create_key(keyname: str) -> str:
"""To allow cross-cutting concern to control access to their local state,
the RuntimeContext API provides a function which takes a keyname as input,
and returns a unique key.
Expand Down
4 changes: 2 additions & 2 deletions opentelemetry-api/src/opentelemetry/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
from opentelemetry.context.context import Context
from opentelemetry.environment_variables import OTEL_PYTHON_TRACER_PROVIDER
from opentelemetry.trace.propagation import (
_SPAN_KEY,
SPAN_KEY,
get_current_span,
set_span_in_context,
)
Expand Down Expand Up @@ -517,7 +517,7 @@ def use_span(
this mechanism if it was previously set manually.
"""
try:
token = context_api.attach(context_api.set_value(_SPAN_KEY, span))
token = context_api.attach(context_api.set_value(SPAN_KEY, span))
try:
yield span
finally:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
# limitations under the License.
from typing import Optional

from opentelemetry.context import _create_key, get_value, set_value
from opentelemetry.context import create_key, get_value, set_value
from opentelemetry.context.context import Context
from opentelemetry.trace.span import INVALID_SPAN, Span

_SPAN_KEY = _create_key("current-span")
SPAN_KEY = create_key("current-span")


def set_span_in_context(
Expand All @@ -30,7 +30,7 @@ def set_span_in_context(
context: a Context object. if one is not passed, the
default current context is used instead.
"""
ctx = set_value(_SPAN_KEY, span, context=context)
ctx = set_value(SPAN_KEY, span, context=context)
return ctx


Expand All @@ -44,7 +44,7 @@ def get_current_span(context: Optional[Context] = None) -> Span:
Returns:
The Span set in the context if it exists. INVALID_SPAN otherwise.
"""
span = get_value(_SPAN_KEY, context=context)
span = get_value(SPAN_KEY, context=context)
if span is None or not isinstance(span, Span):
return INVALID_SPAN
return span
15 changes: 6 additions & 9 deletions opentelemetry-api/tests/context/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
from opentelemetry.context.context import Context


def _do_work() -> str:
# pylint: disable=protected-access
key = context._create_key("say")
def do_work() -> str:
key = context.create_key("say")
context.attach(context.set_value(key, "bar"))
return key

Expand All @@ -30,24 +29,22 @@ def setUp(self):
context.attach(Context())

def test_context_key(self):
# pylint: disable=protected-access
key1 = context._create_key("say")
key2 = context._create_key("say")
key1 = context.create_key("say")
key2 = context.create_key("say")
self.assertNotEqual(key1, key2)
first = context.set_value(key1, "foo")
second = context.set_value(key2, "bar")
self.assertEqual(context.get_value(key1, context=first), "foo")
self.assertEqual(context.get_value(key2, context=second), "bar")

def test_context(self):
# pylint: disable=protected-access
key1 = context._create_key("say")
key1 = context.create_key("say")
self.assertIsNone(context.get_value(key1))
empty = context.get_current()
second = context.set_value(key1, "foo")
self.assertEqual(context.get_value(key1, context=second), "foo")

key2 = _do_work()
key2 = do_work()
self.assertEqual(context.get_value(key2), "bar")
third = context.get_current()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
B3SingleFormat,
)
from opentelemetry.propagators.textmap import DefaultGetter
from opentelemetry.trace.propagation import _SPAN_KEY
from opentelemetry.trace.propagation import SPAN_KEY


def get_child_parent_new_carrier(old_carrier, propagator):
Expand Down Expand Up @@ -248,7 +248,7 @@ def test_derived_ctx_is_returned_for_success(self):
},
old_ctx,
)
self.assertIn(_SPAN_KEY, new_ctx)
self.assertIn(SPAN_KEY, new_ctx)
for key, value in old_ctx.items(): # pylint:disable=no-member
self.assertIn(key, new_ctx)
# pylint:disable=unsubscriptable-object
Expand All @@ -258,7 +258,7 @@ def test_derived_ctx_is_returned_for_failure(self):
"""Ensure returned context is derived from the given context."""
old_ctx = Context({"k2": "v2"})
new_ctx = self.get_propagator().extract({}, old_ctx)
self.assertNotIn(_SPAN_KEY, new_ctx)
self.assertNotIn(SPAN_KEY, new_ctx)
for key, value in old_ctx.items(): # pylint:disable=no-member
self.assertIn(key, new_ctx)
# pylint:disable=unsubscriptable-object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@
# pylint:disable=no-member

import logging
from typing import Optional, TypeVar, Union
from types import TracebackType
from typing import Optional, Type, TypeVar, Union

from deprecated import deprecated
from opentracing import (
Expand All @@ -102,8 +103,8 @@
from opentelemetry.baggage import get_baggage, set_baggage
from opentelemetry.context import (
Context,
_create_key,
attach,
create_key,
detach,
get_value,
set_value,
Expand All @@ -124,7 +125,7 @@

ValueT = TypeVar("ValueT", int, float, bool, str)
logger = logging.getLogger(__name__)
_SHIM_KEY = _create_key("scope_shim")
SHIM_KEY = create_key("scope_shim")


def create_tracer(otel_tracer_provider: TracerProvider) -> "TracerShim":
Expand Down Expand Up @@ -356,7 +357,7 @@ def __init__(
):
super().__init__(manager, span)
self._span_cm = span_cm
self._token = attach(set_value(_SHIM_KEY, self))
self._token = attach(set_value(SHIM_KEY, self))

# TODO: Change type of `manager` argument to `opentracing.ScopeManager`? We
# need to get rid of `manager.tracer` for this.
Expand Down Expand Up @@ -409,16 +410,24 @@ def close(self):
ends the associated span**, regardless of the value passed in
*finish_on_close* when activating the span.
"""
self._end_span_scope(None, None, None)

detach(self._token)
def __exit__(self, exc_type, exc_val, exc_tb):
"""
Override the __exit__ method of `opentracing.scope.Scope` so we can report
exceptions correctly in opentelemetry specification format.
"""
self._end_span_scope(exc_type, exc_val, exc_tb)

def _end_span_scope(
self,
exc_type: Optional[Type[BaseException]],
exc_val: Optional[BaseException],
exc_tb: Optional[TracebackType],
) -> None:
detach(self._token)
if self._span_cm is not None:
# We don't have error information to pass to `__exit__()` so we
# pass `None` in all arguments. If the OpenTelemetry tracer
# implementation requires this information, the `__exit__()` method
# on `opentracing.Scope` should be overridden and modified to pass
# the relevant values to this `close()` method.
self._span_cm.__exit__(None, None, None)
self._span_cm.__exit__(exc_type, exc_val, exc_tb)
else:
self._span.unwrap().end()

Expand Down Expand Up @@ -485,7 +494,7 @@ def active(self) -> "ScopeShim":
return None

try:
return get_value(_SHIM_KEY)
return get_value(SHIM_KEY)
except KeyError:
span_context = SpanContextShim(span.get_span_context())
wrapped_span = SpanShim(self._tracer, span_context, span)
Expand Down
Loading

0 comments on commit 08542e1

Please sign in to comment.