Skip to content

Commit

Permalink
Merge branch 'main' into fix_unix
Browse files Browse the repository at this point in the history
  • Loading branch information
xrmx authored May 28, 2024
2 parents 44a9fec + eb8e456 commit 356fb3e
Show file tree
Hide file tree
Showing 48 changed files with 821 additions and 183 deletions.
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -492,4 +492,4 @@ min-public-methods=2

# Exceptions that will emit a warning when being caught. Defaults to
# "Exception".
overgeneral-exceptions=Exception
overgeneral-exceptions=builtins.Exception
16 changes: 14 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- `opentelemetry-instrumentation-dbapi` Fix compatibility with Psycopg3 to extract libpq build version (#2500)[https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2500]
- `opentelemetry-instrumentation-grpc` AioClientInterceptor should propagate with a Metadata object
([#2363](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2363))
- `opentelemetry-instrumentation-boto3sqs` Instrument Session and resource
Expand All @@ -47,6 +48,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#2418](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2418))
- Use sqlalchemy version in sqlalchemy commenter instead of opentelemetry library version
([#2404](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2404))
- `opentelemetry-instrumentation-asyncio` Check for cancelledException in the future
([#2461](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2461))
- Remove SDK dependency from opentelemetry-instrumentation-grpc
([#2474](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2474))
- `opentelemetry-instrumentation-elasticsearch` Improved support for version 8
([#2420](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2420))
- `opentelemetry-instrumentation-elasticsearch` Disabling instrumentation with native OTel support enabled
([#2524](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2524))
- `opentelemetry-instrumentation-asyncio` Check for __name__ attribute in the coroutine
([#2521](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2521))
- `opentelemetry-util-http` Preserve brackets around literal IPv6 hosts ([#2552](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2552))


## Version 1.24.0/0.45b0 (2024-03-28)

Expand Down Expand Up @@ -118,7 +131,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#1959](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1959))
- `opentelemetry-resource-detector-azure` Added dependency for Cloud Resource ID attribute
([#2072](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2072))

## Version 1.21.0/0.42b0 (2023-11-01)

### Added
Expand Down Expand Up @@ -1532,4 +1545,3 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `opentelemetry-resource-detector-azure` Suppress instrumentation for `urllib` call
([#2178](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2178))
- AwsLambdaInstrumentor handles and re-raises function exception ([#2245](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2245))

4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ You can run `tox` with the following arguments:
`black` and `isort` are executed when `tox -e lint` is run. The reported errors can be tedious to fix manually.
An easier way to do so is:

1. Run `.tox/lint-some-package/bin/black .`
2. Run `.tox/lint-some-package/bin/isort .`
1. Run `.tox/lint/bin/black .`
2. Run `.tox/lint/bin/isort .`

Or you can call formatting and linting in one command by [pre-commit](https://pre-commit.com/):

Expand Down
2 changes: 1 addition & 1 deletion gen-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-c dev-requirements.txt
astor==0.8.1
jinja2==3.1.3
jinja2==3.1.4
markupsafe==2.0.1
isort
black
Expand Down
2 changes: 1 addition & 1 deletion instrumentation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
| [opentelemetry-instrumentation-confluent-kafka](./opentelemetry-instrumentation-confluent-kafka) | confluent-kafka >= 1.8.2, <= 2.3.0 | No | experimental
| [opentelemetry-instrumentation-dbapi](./opentelemetry-instrumentation-dbapi) | dbapi | No | experimental
| [opentelemetry-instrumentation-django](./opentelemetry-instrumentation-django) | django >= 1.10 | Yes | experimental
| [opentelemetry-instrumentation-elasticsearch](./opentelemetry-instrumentation-elasticsearch) | elasticsearch >= 2.0 | No | experimental
| [opentelemetry-instrumentation-elasticsearch](./opentelemetry-instrumentation-elasticsearch) | elasticsearch >= 6.0 | No | experimental
| [opentelemetry-instrumentation-falcon](./opentelemetry-instrumentation-falcon) | falcon >= 1.4.1, < 4.0.0 | Yes | experimental
| [opentelemetry-instrumentation-fastapi](./opentelemetry-instrumentation-fastapi) | fastapi ~= 0.58 | Yes | experimental
| [opentelemetry-instrumentation-flask](./opentelemetry-instrumentation-flask) | flask >= 1.0 | Yes | migration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from unittest.mock import MagicMock

import aiopg
import psycopg2

import opentelemetry.instrumentation.aiopg
from opentelemetry import trace as trace_api
Expand Down Expand Up @@ -384,7 +385,9 @@ def test_span_failed(self):
span.attributes[SpanAttributes.DB_STATEMENT], "Test query"
)
self.assertIs(span.status.status_code, trace_api.StatusCode.ERROR)
self.assertEqual(span.status.description, "Exception: Test Exception")
self.assertEqual(
span.status.description, "ProgrammingError: Test Exception"
)

def test_executemany(self):
db_integration = AiopgIntegration(self.tracer, "testcomponent")
Expand Down Expand Up @@ -570,17 +573,17 @@ class MockCursor:
# pylint: disable=unused-argument, no-self-use
async def execute(self, query, params=None, throw_exception=False):
if throw_exception:
raise Exception("Test Exception")
raise psycopg2.ProgrammingError("Test Exception")

# pylint: disable=unused-argument, no-self-use
async def executemany(self, query, params=None, throw_exception=False):
if throw_exception:
raise Exception("Test Exception")
raise psycopg2.ProgrammingError("Test Exception")

# pylint: disable=unused-argument, no-self-use
async def callproc(self, query, params=None, throw_exception=False):
if throw_exception:
raise Exception("Test Exception")
raise psycopg2.ProgrammingError("Test Exception")

def close(self):
pass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,21 +116,11 @@ class AsyncioInstrumentor(BaseInstrumentor):
"run_coroutine_threadsafe",
]

def __init__(self):
super().__init__()
self.process_duration_histogram = None
self.process_created_counter = None

self._tracer = None
self._meter = None
self._coros_name_to_trace: set = set()
self._to_thread_name_to_trace: set = set()
self._future_active_enabled: bool = False

def instrumentation_dependencies(self) -> Collection[str]:
return _instruments

def _instrument(self, **kwargs):
# pylint: disable=attribute-defined-outside-init
self._tracer = get_tracer(
__name__, __version__, kwargs.get("tracer_provider")
)
Expand Down Expand Up @@ -271,6 +261,8 @@ def trace_item(self, coro_or_future):
return coro_or_future

async def trace_coroutine(self, coro):
if not hasattr(coro, "__name__"):
return coro
start = default_timer()
attr = {
"type": "coroutine",
Expand Down Expand Up @@ -307,13 +299,17 @@ def trace_future(self, future):
)

def callback(f):
exception = f.exception()
attr = {
"type": "future",
"state": (
"cancelled"
if f.cancelled()
else determine_state(f.exception())
),
}
state = determine_state(exception)
attr["state"] = state
self.record_process(start, attr, span, exception)
self.record_process(
start, attr, span, None if f.cancelled() else f.exception()
)

future.add_done_callback(callback)
return future
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# 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.
import asyncio
from unittest.mock import patch

# pylint: disable=no-name-in-module
from opentelemetry.instrumentation.asyncio import AsyncioInstrumentor
from opentelemetry.instrumentation.asyncio.environment_variables import (
OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE,
)
from opentelemetry.test.test_base import TestBase
from opentelemetry.trace import get_tracer


class TestAsyncioAnext(TestBase):
@patch.dict(
"os.environ",
{OTEL_PYTHON_ASYNCIO_COROUTINE_NAMES_TO_TRACE: "async_func"},
)
def setUp(self):
super().setUp()
AsyncioInstrumentor().instrument()
self._tracer = get_tracer(
__name__,
)

def tearDown(self):
super().tearDown()
AsyncioInstrumentor().uninstrument()

# Asyncio anext() does not have __name__ attribute, which is used to determine if the coroutine should be traced.
# This test is to ensure that the instrumentation does not break when the coroutine does not have __name__ attribute.
def test_asyncio_anext(self):
async def main():
async def async_gen():
for it in range(2):
yield it

async_gen_instance = async_gen()
agen = anext(async_gen_instance)
await asyncio.create_task(agen)

asyncio.run(main())
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 0)
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import asyncio
from unittest.mock import patch

from opentelemetry.instrumentation.asyncio import AsyncioInstrumentor
from opentelemetry.instrumentation.asyncio.environment_variables import (
OTEL_PYTHON_ASYNCIO_FUTURE_TRACE_ENABLED,
)
from opentelemetry.test.test_base import TestBase
from opentelemetry.trace import get_tracer


class TestTraceFuture(TestBase):
@patch.dict(
"os.environ", {OTEL_PYTHON_ASYNCIO_FUTURE_TRACE_ENABLED: "true"}
)
def setUp(self):
super().setUp()
self._tracer = get_tracer(
__name__,
)
self.instrumentor = AsyncioInstrumentor()
self.instrumentor.instrument()

def tearDown(self):
super().tearDown()
self.instrumentor.uninstrument()

def test_trace_future_cancelled(self):
async def future_cancelled():
with self._tracer.start_as_current_span("root"):
future = asyncio.Future()
future = self.instrumentor.trace_future(future)
future.cancel()

try:
asyncio.run(future_cancelled())
except asyncio.CancelledError as exc:
self.assertEqual(isinstance(exc, asyncio.CancelledError), True)
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 2)
self.assertEqual(spans[0].name, "root")
self.assertEqual(spans[1].name, "asyncio future")

metrics = (
self.memory_metrics_reader.get_metrics_data()
.resource_metrics[0]
.scope_metrics[0]
.metrics
)
self.assertEqual(len(metrics), 2)

self.assertEqual(metrics[0].name, "asyncio.process.duration")
self.assertEqual(
metrics[0].data.data_points[0].attributes["state"], "cancelled"
)

self.assertEqual(metrics[1].name, "asyncio.process.created")
self.assertEqual(
metrics[1].data.data_points[0].attributes["state"], "cancelled"
)
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ def rest_api_handler(event, context):


def handler_exc(event, context):
# pylint: disable=broad-exception-raised
raise Exception("500 internal server error")
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
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, service_name: str, operation_name: str, api_params: dict) -> None
response_hook (Callable) - a function with extra user-defined logic to be performed after performing the request
this function signature is: def request_hook(span: Span, service_name: str, operation_name: str, result: dict) -> None
this function signature is: def response_hook(span: Span, service_name: str, operation_name: str, result: dict) -> None
for example:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jsonschema==4.21.1
jsonschema-specifications==2023.12.1
junit-xml==1.9
MarkupSafe==2.0.1
moto==2.2.20
moto==3.1.19
mpmath==1.3.0
networkx==3.1
packaging==23.2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,14 +324,14 @@ def test_get_item(self):
Key={"id": {"S": "1"}},
ConsistentRead=True,
AttributesToGet=["id"],
ProjectionExpression="1,2",
ProjectionExpression="PE",
ReturnConsumedCapacity="TOTAL",
)

span = self.assert_span("GetItem")
self.assert_table_names(span, self.default_table_name)
self.assert_consistent_read(span, True)
self.assert_projection(span, "1,2")
self.assert_projection(span, "PE")
self.assert_consumed_capacity(span, self.default_table_name)

@mock_dynamodb2
Expand Down Expand Up @@ -390,7 +390,7 @@ def test_query(self):
}
},
ScanIndexForward=True,
ProjectionExpression="1,2",
ProjectionExpression="PE",
ReturnConsumedCapacity="TOTAL",
)

Expand All @@ -403,7 +403,7 @@ def test_query(self):
self.assert_consistent_read(span, True)
self.assert_index_name(span, "lsi")
self.assert_limit(span, 42)
self.assert_projection(span, "1,2")
self.assert_projection(span, "PE")
self.assert_select(span, "ALL_ATTRIBUTES")
self.assert_consumed_capacity(span, self.default_table_name)

Expand All @@ -419,7 +419,7 @@ def test_scan(self):
Select="ALL_ATTRIBUTES",
TotalSegments=17,
Segment=21,
ProjectionExpression="1,2",
ProjectionExpression="PE",
ConsistentRead=True,
ReturnConsumedCapacity="TOTAL",
)
Expand All @@ -440,7 +440,7 @@ def test_scan(self):
self.assert_consistent_read(span, True)
self.assert_index_name(span, "lsi")
self.assert_limit(span, 42)
self.assert_projection(span, "1,2")
self.assert_projection(span, "PE")
self.assert_select(span, "ALL_ATTRIBUTES")
self.assert_consumed_capacity(span, self.default_table_name)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,12 +427,19 @@ def traced_execution(
if args and self._commenter_enabled:
try:
args_list = list(args)
if hasattr(self._connect_module, "__libpq_version__"):
libpq_version = self._connect_module.__libpq_version__
else:
libpq_version = (
self._connect_module.pq.__build_version__
)

commenter_data = {
# Psycopg2/framework information
"db_driver": f"psycopg2:{self._connect_module.__version__.split(' ')[0]}",
"dbapi_threadsafety": self._connect_module.threadsafety,
"dbapi_level": self._connect_module.apilevel,
"libpq_version": self._connect_module.__libpq_version__,
"libpq_version": libpq_version,
"driver_paramstyle": self._connect_module.paramstyle,
}
if self._commenter_options.get(
Expand Down
Loading

0 comments on commit 356fb3e

Please sign in to comment.