Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bedrock Sync Chat Completion Instrumentation #953

Merged
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
159b500
Add AWS Bedrock testing infrastructure
umaannamalai Oct 2, 2023
df1bd65
Squashed commit of the following:
TimPansino Oct 13, 2023
b6835d0
Squashed commit of the following:
TimPansino Oct 13, 2023
cc3e285
Cache Package Version Lookups (#946)
TimPansino Oct 19, 2023
5996de6
Fix Redis Generator Methods (#947)
TimPansino Oct 19, 2023
360899b
TEMP
TimPansino Oct 19, 2023
4721025
Automatic RPM System Updates (#948)
TimPansino Oct 23, 2023
8ddd0f4
Bedrock titan extraction nearly complete
TimPansino Oct 24, 2023
bdbfdd3
Cleaning up titan bedrock implementation
TimPansino Oct 25, 2023
05dbe2a
TEMP
TimPansino Oct 25, 2023
c305566
Tests for bedrock passing
TimPansino Oct 26, 2023
024b24b
Cleaned up titan testing
TimPansino Oct 26, 2023
4bc91bf
Parametrized bedrock testing
TimPansino Oct 26, 2023
fbb5f4d
Add support for AI21-J2 models
TimPansino Oct 26, 2023
b8c063f
Change to dynamic no conversation id events
TimPansino Oct 26, 2023
a1e7732
Drop all openai refs
TimPansino Oct 30, 2023
a8d3f3e
[Mega-Linter] Apply linters fixes
TimPansino Oct 30, 2023
1707d6f
Adding response_id and response_model
TimPansino Oct 30, 2023
2191684
Drop python 3.7 tests for Hypercorn (#954)
lrafeei Oct 30, 2023
73f098c
Merge remote-tracking branch 'origin/develop-bedrock-instrumentation'…
TimPansino Oct 31, 2023
30020c6
Merge branch 'main' into feature-bedrock-sync-instrumentation
TimPansino Oct 31, 2023
cac0dc6
Merge remote-tracking branch 'origin/main' into feature-bedrock-sync-…
TimPansino Nov 1, 2023
3b4cf9d
Apply suggestions from code review
TimPansino Nov 1, 2023
97064e8
Remove unused import
TimPansino Nov 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Cache Package Version Lookups (#946)
* Cache _get_package_version

* Add Python 2.7 support to get_package_version caching

* [Mega-Linter] Apply linters fixes

* Bump tests

---------

Co-authored-by: SlavaSkvortsov <29122694+SlavaSkvortsov@users.noreply.github.com>
Co-authored-by: TimPansino <TimPansino@users.noreply.github.com>
3 people authored Oct 19, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit cc3e285c7075eb4aa45de45228af1fb15b2eca30
41 changes: 40 additions & 1 deletion newrelic/common/package_version_utils.py
Original file line number Diff line number Diff line change
@@ -14,6 +14,44 @@

import sys

try:
from functools import cache as _cache_package_versions
except ImportError:
from functools import wraps
from threading import Lock

_package_version_cache = {}
_package_version_cache_lock = Lock()

def _cache_package_versions(wrapped):
"""
Threadsafe implementation of caching for _get_package_version.

Python 2.7 does not have the @functools.cache decorator, and
must be reimplemented with support for clearing the cache.
"""

@wraps(wrapped)
def _wrapper(name):
if name in _package_version_cache:
return _package_version_cache[name]

with _package_version_cache_lock:
if name in _package_version_cache:
return _package_version_cache[name]

version = _package_version_cache[name] = wrapped(name)
return version

def cache_clear():
"""Cache clear function to mimic @functools.cache"""
with _package_version_cache_lock:
_package_version_cache.clear()

_wrapper.cache_clear = cache_clear
return _wrapper


# Need to account for 4 possible variations of version declaration specified in (rejected) PEP 396
VERSION_ATTRS = ("__version__", "version", "__version_tuple__", "version_tuple") # nosec
NULL_VERSIONS = frozenset((None, "", "0", "0.0", "0.0.0", "0.0.0.0", (0,), (0, 0), (0, 0, 0), (0, 0, 0, 0))) # nosec
@@ -67,6 +105,7 @@ def int_or_str(value):
return version


@_cache_package_versions
def _get_package_version(name):
module = sys.modules.get(name, None)
version = None
@@ -75,7 +114,7 @@ def _get_package_version(name):
if "importlib" in sys.modules and hasattr(sys.modules["importlib"], "metadata"):
try:
# In Python3.10+ packages_distribution can be checked for as well
if hasattr(sys.modules["importlib"].metadata, "packages_distributions"): # pylint: disable=E1101
if hasattr(sys.modules["importlib"].metadata, "packages_distributions"): # pylint: disable=E1101
distributions = sys.modules["importlib"].metadata.packages_distributions() # pylint: disable=E1101
distribution_name = distributions.get(name, name)
else:
24 changes: 22 additions & 2 deletions tests/agent_unittests/test_package_version_utils.py
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@
from newrelic.common.package_version_utils import (
NULL_VERSIONS,
VERSION_ATTRS,
_get_package_version,
get_package_version,
get_package_version_tuple,
)
@@ -31,7 +32,7 @@
# such as distribution_packages and removed pkg_resources.

IS_PY38_PLUS = sys.version_info[:2] >= (3, 8)
IS_PY310_PLUS = sys.version_info[:2] >= (3,10)
IS_PY310_PLUS = sys.version_info[:2] >= (3, 10)
SKIP_IF_NOT_IMPORTLIB_METADATA = pytest.mark.skipif(not IS_PY38_PLUS, reason="importlib.metadata is not supported.")
SKIP_IF_IMPORTLIB_METADATA = pytest.mark.skipif(
IS_PY38_PLUS, reason="importlib.metadata is preferred over pkg_resources."
@@ -46,7 +47,13 @@ def patched_pytest_module(monkeypatch):
monkeypatch.delattr(pytest, attr)

yield pytest



@pytest.fixture(scope="function", autouse=True)
def cleared_package_version_cache():
"""Ensure cache is empty before every test to exercise code paths."""
_get_package_version.cache_clear()


# This test only works on Python 3.7
@SKIP_IF_IMPORTLIB_METADATA
@@ -123,3 +130,16 @@ def test_mapping_import_to_distribution_packages():
def test_pkg_resources_metadata():
version = get_package_version("pytest")
assert version not in NULL_VERSIONS, version


def test_version_caching(monkeypatch):
# Add fake module to be deleted later
sys.modules["mymodule"] = sys.modules["pytest"]
setattr(pytest, "__version__", "1.0.0")
version = get_package_version("mymodule")
assert version not in NULL_VERSIONS, version

# Ensure after deleting that the call to _get_package_version still completes because of caching
del sys.modules["mymodule"]
version = get_package_version("mymodule")
assert version not in NULL_VERSIONS, version