Skip to content

Commit a7f118a

Browse files
committed
Fix according to CR Comments
1 parent e37b5fa commit a7f118a

File tree

6 files changed

+85
-15
lines changed

6 files changed

+85
-15
lines changed

CHANGELOG.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
- `opentelemetry-sdk-extension-aws` Add AWS resource detectors to extension package
1414
([#586](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/586))
1515
- `opentelemetry-instrumentation-asgi`, `opentelemetry-instrumentation-aiohttp-client`, `openetelemetry-instrumentation-fastapi`,
16-
`opentelemetry-instrumentation-starlette`, `opentelemetry-instrumentation-urllib`, `opentelemetry-instrumentation-urllib3`,
17-
`opentelemetry-instrumentation-pika` Added `request_hook` and `response_hook` callbacks
16+
`opentelemetry-instrumentation-starlette`, `opentelemetry-instrumentation-urllib`, `opentelemetry-instrumentation-urllib3` Added `request_hook` and `response_hook` callbacks
1817
([#576](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/576))
1918

2019
### Changed

instrumentation/opentelemetry-instrumentation-pika/setup.cfg

-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ packages=find_namespace:
4141
install_requires =
4242
opentelemetry-api ~= 1.5
4343
wrapt >= 1.0.0, < 2.0.0
44-
pika >= 1.1.0
4544

4645
[options.extras_require]
4746
test =

instrumentation/opentelemetry-instrumentation-pika/setup.py

+63-7
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,79 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
15+
16+
# DO NOT EDIT. THIS FILE WAS AUTOGENERATED FROM templates/instrumentation_setup.py.txt.
17+
# RUN `python scripts/generate_setup.py` TO REGENERATE.
18+
19+
20+
import distutils.cmd
21+
import json
1422
import os
23+
from configparser import ConfigParser
1524

1625
import setuptools
1726

27+
config = ConfigParser()
28+
config.read("setup.cfg")
29+
30+
# We provide extras_require parameter to setuptools.setup later which
31+
# overwrites the extra_require section from setup.cfg. To support extra_require
32+
# secion in setup.cfg, we load it here and merge it with the extra_require param.
33+
extras_require = {}
34+
if "options.extras_require" in config:
35+
for key, value in config["options.extras_require"].items():
36+
extras_require[key] = [v for v in value.split("\n") if v.strip()]
37+
1838
BASE_DIR = os.path.dirname(__file__)
39+
PACKAGE_INFO = {}
40+
1941
VERSION_FILENAME = os.path.join(
20-
BASE_DIR, "src", "opentelemetry", "instrumentation", "pika", "version.py",
42+
BASE_DIR, "src", "opentelemetry", "instrumentation", "pika", "version.py"
2143
)
22-
PACKAGE_INFO = {}
2344
with open(VERSION_FILENAME) as f:
2445
exec(f.read(), PACKAGE_INFO)
2546

47+
PACKAGE_FILENAME = os.path.join(
48+
BASE_DIR, "src", "opentelemetry", "instrumentation", "pika", "package.py"
49+
)
50+
with open(PACKAGE_FILENAME) as f:
51+
exec(f.read(), PACKAGE_INFO)
52+
53+
# Mark any instruments/runtime dependencies as test dependencies as well.
54+
extras_require["instruments"] = PACKAGE_INFO["_instruments"]
55+
test_deps = extras_require.get("test", [])
56+
for dep in extras_require["instruments"]:
57+
test_deps.append(dep)
58+
59+
extras_require["test"] = test_deps
60+
61+
62+
class JSONMetadataCommand(distutils.cmd.Command):
63+
64+
description = (
65+
"print out package metadata as JSON. This is used by OpenTelemetry dev scripts to ",
66+
"auto-generate code in other places",
67+
)
68+
user_options = []
69+
70+
def initialize_options(self):
71+
pass
72+
73+
def finalize_options(self):
74+
pass
75+
76+
def run(self):
77+
metadata = {
78+
"name": config["metadata"]["name"],
79+
"version": PACKAGE_INFO["__version__"],
80+
"instruments": PACKAGE_INFO["_instruments"],
81+
}
82+
print(json.dumps(metadata))
83+
84+
2685
setuptools.setup(
86+
cmdclass={"meta": JSONMetadataCommand},
2787
version=PACKAGE_INFO["__version__"],
28-
entry_points={
29-
"opentelemetry_instrumentor": [
30-
"pika = opentelemetry.instrumentation.pika:PikaInstrumentor"
31-
]
32-
},
88+
extras_require=extras_require,
3389
)

instrumentation/opentelemetry-instrumentation-pika/src/opentelemetry/instrumentation/pika/__init__.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
---
6767
"""
6868

69-
# pylint: disable=unused-argument
70-
from .pika_instrumentor import PikaInstrumentor
71-
from .version import __version__
69+
from .pika_instrumentor import ( # pylint: disable=unused-argument
70+
PikaInstrumentor,
71+
)
72+
from .version import __version__ # pylint: disable=unused-argument

instrumentation/opentelemetry-instrumentation-pika/src/opentelemetry/instrumentation/pika/pika_instrumentor.py

+15
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ def _uninstrument_channel_functions(channel: Channel) -> None:
7575
def instrument_channel(
7676
channel: Channel, tracer_provider: Optional[TracerProvider] = None,
7777
) -> None:
78+
if not hasattr(channel, "_is_instrumented_by_opentelemetry"):
79+
channel._is_instrumented_by_opentelemetry = False
80+
if channel._is_instrumented_by_opentelemetry:
81+
_LOG.warning(
82+
"Attempting to instrument Pika channel while already instrumented!"
83+
)
84+
return
7885
tracer = trace.get_tracer(__name__, __version__, tracer_provider)
7986
channel.__setattr__("__opentelemetry_tracer", tracer)
8087
if not hasattr(channel, "_impl"):
@@ -88,6 +95,14 @@ def instrument_channel(
8895

8996
@staticmethod
9097
def uninstrument_channel(channel: Channel) -> None:
98+
if (
99+
not hasattr(channel, "_is_instrumented_by_opentelemetry")
100+
or not channel._is_instrumented_by_opentelemetry
101+
):
102+
_LOG.error(
103+
"Attempting to uninstrument Pika channel while already uninstrumented!"
104+
)
105+
return
91106
if not hasattr(channel, "_impl"):
92107
_LOG.error("Could not find implementation for provided channel!")
93108
return

instrumentation/opentelemetry-instrumentation-pika/src/opentelemetry/instrumentation/pika/utils.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,13 @@ def _get_span(
105105
if context.get_value("suppress_instrumentation") or context.get_value(
106106
_SUPPRESS_INSTRUMENTATION_KEY
107107
):
108-
print("Suppressing instrumentation!")
109108
return None
110109
task_name = properties.type if properties.type else task_name
111110
span = tracer.start_span(
112111
context=ctx, name=_generate_span_name(task_name, operation)
113112
)
114-
_enrich_span(span, channel, properties, task_name, operation)
113+
if span.is_recording():
114+
_enrich_span(span, channel, properties, task_name, operation)
115115
return span
116116

117117

0 commit comments

Comments
 (0)