Skip to content

Commit

Permalink
Don't use pkg_resources.get_distribution(..).version (#256)
Browse files Browse the repository at this point in the history
* Pin upstream version of opentelemetry when running tests

Fix in #254 didn't apply to tox environments that run `pip install .`

`test_detects_gce` was failing with opentelemetry-sdk version 1.19.0
instead of 1.18.0

* Don't use `pkg_resources.get_distribution(..).version`

Similar to discussion at googleapis/python-api-core#27 I am using opentelemetry in an environment where modules are vendored without pip. So, pkg_resources can not be used to search for a module at runtime to get its version.

---------

Co-authored-by: Aaron Abbott <[email protected]>
  • Loading branch information
DouglasHeriot and aabmass authored Aug 3, 2023
1 parent 9e1d393 commit c487ff7
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from typing import Dict, List, NoReturn, Optional, Set, Union

import google.auth
import pkg_resources
from google.api.distribution_pb2 import Distribution
from google.api.label_pb2 import LabelDescriptor
from google.api.metric_pb2 import Metric as GMetric
Expand All @@ -44,6 +43,7 @@
from opentelemetry.resourcedetector.gcp_resource_detector._mapping import (
get_monitored_resource,
)
from opentelemetry.sdk import version as opentelemetry_sdk_version
from opentelemetry.sdk.metrics.export import (
Gauge,
Histogram,
Expand All @@ -62,7 +62,7 @@
UNIQUE_IDENTIFIER_KEY = "opentelemetry_id"
NANOS_PER_SECOND = 10**9

_OTEL_SDK_VERSION = pkg_resources.get_distribution("opentelemetry-sdk").version
_OTEL_SDK_VERSION = opentelemetry_sdk_version.__version__
_USER_AGENT = f"opentelemetry-python {_OTEL_SDK_VERSION}; google-cloud-metric-exporter {__version__}"

# Set user-agent metadata, see https://github.com/grpc/grpc/issues/23644 and default options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@

import google.auth
import opentelemetry.trace as trace_api
import pkg_resources
from google.cloud.trace_v2 import BatchWriteSpansRequest, TraceServiceClient
from google.cloud.trace_v2 import types as trace_types
from google.cloud.trace_v2.services.trace_service.transports import (
Expand All @@ -108,6 +107,7 @@
from opentelemetry.resourcedetector.gcp_resource_detector._mapping import (
get_monitored_resource,
)
from opentelemetry.sdk import version as opentelemetry_sdk_version
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import Event
from opentelemetry.sdk.trace.export import (
Expand All @@ -122,7 +122,7 @@

logger = logging.getLogger(__name__)

_OTEL_SDK_VERSION = pkg_resources.get_distribution("opentelemetry-sdk").version
_OTEL_SDK_VERSION = opentelemetry_sdk_version.__version__
_USER_AGENT = f"opentelemetry-python {_OTEL_SDK_VERSION}; google-cloud-trace-exporter {__version__}"

# Set user-agent metadata, see https://github.com/grpc/grpc/issues/23644 and default options
Expand Down Expand Up @@ -493,9 +493,7 @@ def _extract_attributes(
if add_agent_attr:
attributes_dict["g.co/agent"] = _format_attribute_value(
"opentelemetry-python {}; google-cloud-trace-exporter {}".format(
_strip_characters(
pkg_resources.get_distribution("opentelemetry-sdk").version
),
_strip_characters(_OTEL_SDK_VERSION),
_strip_characters(__version__),
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import unittest
from unittest import mock

import pkg_resources
from google.cloud.trace_v2.services.trace_service.transports import (
TraceServiceGrpcTransport,
)
Expand All @@ -43,6 +42,7 @@
_truncate_str,
)
from opentelemetry.exporter.cloud_trace.version import __version__
from opentelemetry.sdk import version as opentelemetry_sdk_version
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import Event
from opentelemetry.sdk.trace import _Span as Span
Expand Down Expand Up @@ -94,9 +94,7 @@ def setUpClass(cls):
)
cls.agent_code = _format_attribute_value(
"opentelemetry-python {}; google-cloud-trace-exporter {}".format(
_strip_characters(
pkg_resources.get_distribution("opentelemetry-sdk").version
),
_strip_characters(opentelemetry_sdk_version.__version__),
_strip_characters(__version__),
)
)
Expand Down

0 comments on commit c487ff7

Please sign in to comment.