Skip to content

Commit

Permalink
Merge branch 'main' into Fix-sqlalchemy-uninstrument
Browse files Browse the repository at this point in the history
  • Loading branch information
shalevr authored Jan 15, 2023
2 parents 8cd9168 + c92ba14 commit 3536964
Show file tree
Hide file tree
Showing 29 changed files with 1,011 additions and 39 deletions.
3 changes: 3 additions & 0 deletions .github/component_owners.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,8 @@ components:
instrumentation/opentelemetry-instrumentation-tornado:
- shalevr

instrumentation/opentelemetry-instrumentation-urllib:
- shalevr

instrumentation/opentelemetry-instrumentation-urllib3:
- shalevr
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,24 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## Fixed
### Added

- Add metric instrumentation for urllib
([#1553](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1553))
- `opentelemetry/sdk/extension/aws` Implement [`aws.ecs.*`](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/semantic_conventions/cloud_provider/aws/ecs.md) and [`aws.logs.*`](https://opentelemetry.io/docs/reference/specification/resource/semantic_conventions/cloud_provider/aws/logs/) resource attributes in the `AwsEcsResourceDetector` detector when the ECS Metadata v4 is available
([#1212](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1212))

### Fixed

- Fix aiopg instrumentation to work with aiopg < 2.0.0
([#1473](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1473))
- `opentelemetry-instrumentation-aws-lambda` Adds an option to configure `disable_aws_context_propagation` by
environment variable: `OTEL_LAMBDA_DISABLE_AWS_CONTEXT_PROPAGATION`
([#1507](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1507))
- Fix pymongo to collect the property DB_MONGODB_COLLECTION
([#1555](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1555))
- `opentelemetry-instrumentation-asgi` Fix keys() in class ASGIGetter to correctly fetch values from carrier headers.
([#1435](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1435))


## Version 1.15.0/0.36b0 (2022-12-10)
Expand Down
3 changes: 3 additions & 0 deletions docs-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@ pymemcache~=1.3
django>=2.2

# Required by instrumentation and exporter packages
aio_pika~=7.2.0
aiohttp~=3.0
aiopg>=0.13.0,<1.3.0
asyncpg>=0.12.0
boto~=2.0
botocore~=1.0
celery>=4.0
elasticsearch>=2.0,<9.0
flask~=2.0
falcon~=2.0
grpcio~=1.27
kafka-python>=2.0,<3.0
mysql-connector-python~=8.0
psutil>=5
pika>=0.12.0
Expand Down
7 changes: 7 additions & 0 deletions docs/instrumentation/aio_pika/aio_pika.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
OpenTelemetry aio pika Instrumentation
============================================

.. automodule:: opentelemetry.instrumentation.aio_pika
:members:
:undoc-members:
:show-inheritance:
7 changes: 7 additions & 0 deletions docs/instrumentation/elasticsearch/elasticsearch.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.. include:: ../../../instrumentation/opentelemetry-instrumentation-elasticsearch/README.rst


.. automodule:: opentelemetry.instrumentation.elasticsearch
:members:
:undoc-members:
:show-inheritance:
10 changes: 10 additions & 0 deletions docs/instrumentation/kafka_python/kafka_python.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.. include:: ../../../instrumentation/opentelemetry-instrumentation-kafka-python/README.rst
:end-before: References

API
---

.. automodule:: opentelemetry.instrumentation.kafka
:members:
:undoc-members:
:show-inheritance:
2 changes: 1 addition & 1 deletion instrumentation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@
| [opentelemetry-instrumentation-system-metrics](./opentelemetry-instrumentation-system-metrics) | psutil >= 5 | No
| [opentelemetry-instrumentation-tornado](./opentelemetry-instrumentation-tornado) | tornado >= 5.1.1 | Yes
| [opentelemetry-instrumentation-tortoiseorm](./opentelemetry-instrumentation-tortoiseorm) | tortoise-orm >= 0.17.0 | No
| [opentelemetry-instrumentation-urllib](./opentelemetry-instrumentation-urllib) | urllib | No
| [opentelemetry-instrumentation-urllib](./opentelemetry-instrumentation-urllib) | urllib | Yes
| [opentelemetry-instrumentation-urllib3](./opentelemetry-instrumentation-urllib3) | urllib3 >= 1.0.0, < 2.0.0 | Yes
| [opentelemetry-instrumentation-wsgi](./opentelemetry-instrumentation-wsgi) | wsgi | Yes
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@
# 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.

"""
Instrument `aio_pika` to trace RabbitMQ applications.
Instrument aio_pika to trace RabbitMQ applications.
Usage
-----
* Start broker backend
Start broker backend
.. code-block:: python
docker run -p 5672:5672 rabbitmq
* Run instrumented task
Run instrumented task
.. code-block:: python
Expand All @@ -42,9 +42,7 @@ async def main() -> None:
queue = await channel.declare_queue("hello")
await channel.default_exchange.publish(
Message(b"Hello World!"),
routing_key=queue.name,
)
routing_key=queue.name)
if __name__ == "__main__":
asyncio.run(main())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ def get(
return decoded

def keys(self, carrier: dict) -> typing.List[str]:
return [_key.decode("utf8") for (_key, _value) in carrier]
headers = carrier.get("headers") or []
return [_key.decode("utf8") for (_key, _value) in headers]


asgi_getter = ASGIGetter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@


class TestASGIGetter(TestCase):
def test_get_none(self):
def test_get_none_empty_carrier(self):
getter = ASGIGetter()
carrier = {}
val = getter.get(carrier, "test")
self.assertIsNone(val)

def test_get_none_empty_headers(self):
getter = ASGIGetter()
carrier = {"headers": []}
val = getter.get(carrier, "test")
self.assertIsNone(val)

def test_get_(self):
getter = ASGIGetter()
carrier = {"headers": [(b"test-key", b"val")]}
Expand All @@ -44,7 +50,22 @@ def test_get_(self):
"Should be case insensitive",
)

def test_keys(self):
def test_keys_empty_carrier(self):
getter = ASGIGetter()
keys = getter.keys({})
self.assertEqual(keys, [])

def test_keys_empty_headers(self):
getter = ASGIGetter()
keys = getter.keys({"headers": []})
self.assertEqual(keys, [])

def test_keys(self):
getter = ASGIGetter()
carrier = {"headers": [(b"test-key", b"val")]}
expected_val = ["test-key"]
self.assertEqual(
getter.keys(carrier),
expected_val,
"Should be equal",
)
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def request_hook(span, request):
def response_hook(span, request, response):
pass
DjangoInstrumentation().instrument(request_hook=request_hook, response_hook=response_hook)
DjangoInstrumentor().instrument(request_hook=request_hook, response_hook=response_hook)
Django Request object: https://docs.djangoproject.com/en/3.1/ref/request-response/#httprequest-objects
Django Response object: https://docs.djangoproject.com/en/3.1/ref/request-response/#httpresponse-objects
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,18 @@ def test_trace_response_headers(self):
)
self.memory_exporter.clear()

def test_uninstrument(self):
Client().get("/route/2020/template/")
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 1)

self.memory_exporter.clear()
_django_instrumentor.uninstrument()

Client().get("/route/2020/template/")
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 0)

# pylint: disable=too-many-locals
def test_wsgi_metrics(self):
_expected_metric_names = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,23 @@
es.get(index='my-index', doc_type='my-type', id=1)
Elasticsearch instrumentation prefixes operation names with the string "Elasticsearch". This
can be changed to a different string by either setting the `OTEL_PYTHON_ELASTICSEARCH_NAME_PREFIX`
can be changed to a different string by either setting the OTEL_PYTHON_ELASTICSEARCH_NAME_PREFIX
environment variable or by passing the prefix as an argument to the instrumentor. For example,
.. code-block:: python
ElasticsearchInstrumentor("my-custom-prefix").instrument()
The `instrument` method accepts the following keyword args:
The instrument() method accepts the following keyword args:
tracer_provider (TracerProvider) - an optional tracer provider
request_hook (Callable) - a function with extra user-defined logic to be performed before performing the request
this function signature is:
def request_hook(span: Span, method: str, url: str, kwargs)
this function signature is:
def request_hook(span: Span, method: str, url: str, kwargs)
response_hook (Callable) - a function with extra user-defined logic to be performed after performing the request
this function signature is:
def response_hook(span: Span, response: dict)
this function signature is:
def response_hook(span: Span, response: dict)
for example:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

"""
Instrument `kafka-python` to report instrumentation-kafka produced and consumed messages
Instrument kafka-python to report instrumentation-kafka produced and consumed messages
Usage
-----
Expand All @@ -30,24 +30,21 @@
producer = KafkaProducer(bootstrap_servers=['localhost:9092'])
producer.send('my-topic', b'raw_bytes')
# report a span of type consumer with the default settings
consumer = KafkaConsumer('my-topic',
group_id='my-group',
bootstrap_servers=['localhost:9092'])
consumer = KafkaConsumer('my-topic', group_id='my-group', bootstrap_servers=['localhost:9092'])
for message in consumer:
# process message
# process message
The `_instrument` method accepts the following keyword args:
The _instrument() method accepts the following keyword args:
tracer_provider (TracerProvider) - an optional tracer provider
produce_hook (Callable) - a function with extra user-defined logic to be performed before sending the message
this function signature is:
def produce_hook(span: Span, args, kwargs)
this function signature is:
def produce_hook(span: Span, args, kwargs)
consume_hook (Callable) - a function with extra user-defined logic to be performed after consuming a message
this function signature is:
def consume
_hook(span: Span, record: kafka.record.ABCRecord, args, kwargs)
this function signature is:
def consume_hook(span: Span, record: kafka.record.ABCRecord, args, kwargs)
for example:
.. code: python
from opentelemetry.instrumentation.kafka import KafkaInstrumentor
from kafka import KafkaProducer, KafkaConsumer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def started(self, event: monitoring.CommandStartedEvent):
statement = event.command_name
if command:
statement += " " + str(command)
collection = event.command.get(event.command_name)

try:
span = self._tracer.start_span(name, kind=SpanKind.CLIENT)
Expand All @@ -135,6 +136,10 @@ def started(self, event: monitoring.CommandStartedEvent):
)
span.set_attribute(SpanAttributes.DB_NAME, event.database_name)
span.set_attribute(SpanAttributes.DB_STATEMENT, statement)
if collection:
span.set_attribute(
SpanAttributes.DB_MONGODB_COLLECTION, collection
)
if event.connection_id is not None:
span.set_attribute(
SpanAttributes.NET_PEER_NAME, event.connection_id[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import redis

from opentelemetry import trace
from opentelemetry.instrumentation.redis import RedisInstrumentor
from opentelemetry.test.test_base import TestBase
from opentelemetry.trace import SpanKind
Expand Down Expand Up @@ -146,3 +147,16 @@ def request_hook(span, conn, args, kwargs):

span = spans[0]
self.assertEqual(span.attributes.get(custom_attribute_name), "GET")

def test_no_op_tracer_provider(self):
RedisInstrumentor().uninstrument()
tracer_provider = trace.NoOpTracerProvider()
RedisInstrumentor().instrument(tracer_provider=tracer_provider)

redis_client = redis.Redis()

with mock.patch.object(redis_client, "connection"):
redis_client.get("key")

spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 0)
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def test_no_op_tracer_provider(self):
engine = create_engine("sqlite:///:memory:")
SQLAlchemyInstrumentor().instrument(
engine=engine,
tracer_provider=trace.NoOpTracerProvider,
tracer_provider=trace.NoOpTracerProvider(),
)
cnx = engine.connect()
cnx.execute("SELECT 1 + 1;").fetchall()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,18 @@ def test_request(self):
self.assertEqual(span.resource.attributes["key1"], "value1")
self.assertEqual(span.resource.attributes["key2"], "value2")

def test_uninstrument(self):
self._client.get("/foobar")
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 3)

self.memory_exporter.clear()
self._instrumentor.uninstrument()

self._client.get("/foobar")
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 0)


class TestAutoInstrumentationHooks(TestStarletteManualInstrumentationHooks):
"""
Expand Down
Loading

0 comments on commit 3536964

Please sign in to comment.