Skip to content

Commit

Permalink
upgrade packages for opentelemetry-python 0.15b0 (#91)
Browse files Browse the repository at this point in the history
* upgrade packages for opentelemetry-python 0.15b0

* remove commented code
  • Loading branch information
aabmass authored Nov 4, 2020
1 parent 750c9e9 commit 7b71183
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 25 deletions.
4 changes: 2 additions & 2 deletions dev-constraints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ Sphinx==3.1.2
# development before GA. After GA, we will build against specific releases.
# Bump the commit frequently during development whenever you are missing
# changes from upstream.
opentelemetry-api~=0.14b0
opentelemetry-sdk~=0.14b0
opentelemetry-api~=0.15b0
opentelemetry-sdk~=0.15b0
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ def _get_monitored_resource(
if resource_attributes.get("cloud.provider") != "gcp":
return None
resource_type = resource_attributes["gcp.resource_type"]
if resource_type not in OT_RESOURCE_LABEL_TO_GCP:
if (
not isinstance(resource_type, str)
or resource_type not in OT_RESOURCE_LABEL_TO_GCP
):
return None
return MonitoredResource(
type=resource_type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ def _translate_to_cloud_trace(
MAX_SPAN_ATTRS,
add_agent_attr=True,
),
"links": _extract_links(span.links),
"status": _extract_status(span.status),
"links": _extract_links(span.links), # type: ignore[has-type]
"status": _extract_status(span.status), # type: ignore[arg-type]
"time_events": _extract_events(span.events),
"span_kind": _extract_span_kind(span.kind),
}
Expand Down Expand Up @@ -220,7 +220,7 @@ def _extract_status(status: trace_api.Status) -> Optional[Status]:
"""Convert a Status object to protobuf object."""
if not status:
return None
status_dict = {"details": None, "code": status.canonical_code.value}
status_dict = {"details": None, "code": status.status_code.value}

if status.description is not None:
status_dict["message"] = status.description
Expand Down Expand Up @@ -279,7 +279,7 @@ def _extract_events(events: Sequence[Event]) -> ProtoSpan.TimeEvents:
dropped_annontations = len(events) - MAX_NUM_EVENTS
events = events[:MAX_NUM_EVENTS]
for event in events:
if len(event.attributes) > MAX_EVENT_ATTRS:
if event.attributes and len(event.attributes) > MAX_EVENT_ATTRS:
logger.warning(
"Event %s has more then %s attributes, some will be truncated",
event.name,
Expand Down Expand Up @@ -349,7 +349,10 @@ def _extract_resources(resource: Resource) -> Dict[str, str]:
if resource_attributes.get("cloud.provider") != "gcp":
return {}
resource_type = resource_attributes["gcp.resource_type"]
if resource_type not in OT_RESOURCE_ATTRIBUTE_TO_GCP:
if (
not isinstance(resource_type, str)
or resource_type not in OT_RESOURCE_ATTRIBUTE_TO_GCP
):
return {}
return {
"g.co/r/{}/{}".format(resource_type, gcp_resource_key,): str(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,13 @@ def test_export(self):
)
)
meter = meter_provider.get_meter(__name__)
counter = meter.create_metric(
counter = meter.create_counter(
# TODO: remove "opentelemetry/" prefix which is a hack
# https://github.com/GoogleCloudPlatform/opentelemetry-operations-python/issues/84
name="opentelemetry/name",
description="desc",
unit="1",
value_type=int,
metric_type=metrics.Counter,
)
# interval doesn't matter, we don't start the thread and just run
# tick() instead
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
from opentelemetry.sdk.trace import _Span as Span
from opentelemetry.trace import Link, SpanContext, SpanKind
from opentelemetry.trace.status import Status as SpanStatus
from opentelemetry.trace.status import StatusCanonicalCode
from opentelemetry.trace.status import StatusCode


# pylint: disable=too-many-public-methods
Expand Down Expand Up @@ -159,7 +159,7 @@ def test_export(self):
}
),
"links": None,
"status": None,
"status": Status(code=StatusCode.UNSET.value),
"time_events": None,
"start_time": None,
"end_time": None,
Expand All @@ -183,19 +183,20 @@ def test_extract_none_status(self):

def test_extract_status_code(self):
self.assertEqual(
_extract_status(SpanStatus(canonical_code=StatusCanonicalCode.OK)),
Status(details=None, code=0),
_extract_status(SpanStatus(status_code=StatusCode.OK)),
Status(details=None, code=StatusCode.OK.value),
)

def test_extract_status_code_and_desc(self):
self.assertEqual(
_extract_status(
SpanStatus(
canonical_code=StatusCanonicalCode.UNKNOWN,
description="error_desc",
status_code=StatusCode.UNSET, description="error_desc",
)
),
Status(details=None, code=2, message="error_desc"),
Status(
details=None, code=StatusCode.UNSET.value, message="error_desc"
),
)

def test_extract_empty_attributes(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ class CloudTraceFormatPropagator(textmap.TextMapPropagator):

def extract(
self,
get_from_carrier: textmap.Getter[textmap.TextMapPropagatorT],
getter: textmap.Getter[textmap.TextMapPropagatorT],
carrier: textmap.TextMapPropagatorT,
context: typing.Optional[Context] = None,
) -> Context:
header = get_from_carrier(carrier, _TRACE_CONTEXT_HEADER_NAME)
header = getter.get(carrier, _TRACE_CONTEXT_HEADER_NAME)

if not header:
return trace.set_span_in_context(trace.INVALID_SPAN, context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
import typing
import unittest

import opentelemetry.trace as trace
Expand All @@ -21,6 +20,7 @@
_TRACE_CONTEXT_HEADER_NAME,
CloudTraceFormatPropagator,
)
from opentelemetry.trace.propagation import textmap
from opentelemetry.trace.span import (
INVALID_SPAN_ID,
INVALID_TRACE_ID,
Expand All @@ -29,9 +29,7 @@
get_hexadecimal_trace_id,
)


def get_dict_value(dict_object: typing.Dict[str, str], key: str) -> str:
return dict_object.get(key, "")
dict_getter = textmap.DictGetter()


class TestCloudTraceFormatPropagator(unittest.TestCase):
Expand All @@ -44,7 +42,7 @@ def setUp(self):
def _extract(self, header_value):
"""Test helper"""
header = {_TRACE_CONTEXT_HEADER_NAME: [header_value]}
new_context = self.propagator.extract(get_dict_value, header)
new_context = self.propagator.extract(dict_getter, header)
return trace.get_current_span(new_context).get_span_context()

def _inject(self, span=None):
Expand All @@ -58,7 +56,7 @@ def _inject(self, span=None):

def test_no_context_header(self):
header = {}
new_context = self.propagator.extract(get_dict_value, header)
new_context = self.propagator.extract(dict_getter, header)
self.assertEqual(
trace.get_current_span(new_context).get_span_context(),
trace.INVALID_SPAN.get_span_context(),
Expand Down

0 comments on commit 7b71183

Please sign in to comment.