Skip to content

Commit b0fb929

Browse files
committed
Mark instrumentation points for SDK
1 parent 7b98c51 commit b0fb929

File tree

6 files changed

+50
-12
lines changed

6 files changed

+50
-12
lines changed

newrelic/hooks/external_botocore.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ def extract_bedrock_claude_model(request_body, response_body=None):
243243
chat_completion_summary_dict = {
244244
"request.max_tokens": request_body.get("max_tokens_to_sample", ""),
245245
"request.temperature": request_body.get("temperature", ""),
246-
"response.number_of_messages": len(input_message_list)
246+
"response.number_of_messages": len(input_message_list),
247247
}
248248

249249
if response_body:
@@ -549,6 +549,12 @@ def _nr_clientcreator__create_api_method_(wrapped, instance, args, kwargs):
549549
return tracer(wrapped)
550550

551551

552+
def _nr_clientcreator__create_methods(wrapped, instance, args, kwargs):
553+
class_attributes = wrapped(*args, **kwargs)
554+
class_attributes["_nr_wrapped"] = True
555+
return class_attributes
556+
557+
552558
def _bind_make_request_params(operation_model, request_dict, *args, **kwargs):
553559
return operation_model, request_dict
554560

@@ -579,3 +585,4 @@ def instrument_botocore_endpoint(module):
579585

580586
def instrument_botocore_client(module):
581587
wrap_function_wrapper(module, "ClientCreator._create_api_method", _nr_clientcreator__create_api_method_)
588+
wrap_function_wrapper(module, "ClientCreator._create_methods", _nr_clientcreator__create_methods)

newrelic/hooks/mlmodel_openai.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -834,21 +834,33 @@ def wrap_base_client_process_response(wrapped, instance, args, kwargs):
834834

835835

836836
def instrument_openai_util(module):
837-
wrap_function_wrapper(module, "convert_to_openai_object", wrap_convert_to_openai_object)
837+
if hasattr(module, "convert_to_openai_object"):
838+
wrap_function_wrapper(module, "convert_to_openai_object", wrap_convert_to_openai_object)
839+
# This is to mark where we instrument so the SDK knows not to instrument them
840+
# again.
841+
setattr(module.convert_to_openai_object, "_nr_wrapped", True)
838842

839843

840844
def instrument_openai_api_resources_embedding(module):
841-
if hasattr(module.Embedding, "create"):
842-
wrap_function_wrapper(module, "Embedding.create", wrap_embedding_sync)
843-
if hasattr(module.Embedding, "acreate"):
844-
wrap_function_wrapper(module, "Embedding.acreate", wrap_embedding_async)
845+
if hasattr(module, "Embedding"):
846+
if hasattr(module.Embedding, "create"):
847+
wrap_function_wrapper(module, "Embedding.create", wrap_embedding_sync)
848+
if hasattr(module.Embedding, "acreate"):
849+
wrap_function_wrapper(module, "Embedding.acreate", wrap_embedding_async)
850+
# This is to mark where we instrument so the SDK knows not to instrument them
851+
# again.
852+
setattr(module.Embedding, "_nr_wrapped", True)
845853

846854

847855
def instrument_openai_api_resources_chat_completion(module):
848-
if hasattr(module.ChatCompletion, "create"):
849-
wrap_function_wrapper(module, "ChatCompletion.create", wrap_chat_completion_sync)
850-
if hasattr(module.ChatCompletion, "acreate"):
851-
wrap_function_wrapper(module, "ChatCompletion.acreate", wrap_chat_completion_async)
856+
if hasattr(module, "ChatCompletion"):
857+
if hasattr(module.ChatCompletion, "create"):
858+
wrap_function_wrapper(module, "ChatCompletion.create", wrap_chat_completion_sync)
859+
if hasattr(module.ChatCompletion, "acreate"):
860+
wrap_function_wrapper(module, "ChatCompletion.acreate", wrap_chat_completion_async)
861+
# This is to mark where we instrument so the SDK knows not to instrument them
862+
# again.
863+
setattr(module.ChatCompletion, "_nr_wrapped", True)
852864

853865

854866
def instrument_openai_resources_chat_completions(module):
@@ -858,7 +870,6 @@ def instrument_openai_resources_chat_completions(module):
858870
wrap_function_wrapper(module, "AsyncCompletions.create", wrap_chat_completion_async)
859871

860872

861-
# OpenAI v1 instrumentation points
862873
def instrument_openai_resources_embeddings(module):
863874
if hasattr(module, "Embeddings"):
864875
if hasattr(module.Embeddings, "create"):

tests/external_botocore/test_bedrock_chat_completion.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,7 @@ def _test():
287287
exercise_model(prompt="Invalid Token", temperature=0.7, max_tokens=100)
288288

289289
_test()
290+
291+
292+
def test_bedrock_chat_completion_functions_marked_as_wrapped_for_sdk_compatibility(bedrock_server):
293+
assert bedrock_server._nr_wrapped

tests/external_botocore/test_bedrock_embeddings.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2010 New Relic, Inc.
1+
# Copyright 2010 New Relic, Inc.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -172,3 +172,7 @@ def _test():
172172
exercise_model(prompt="Invalid Token", temperature=0.7, max_tokens=100)
173173

174174
_test()
175+
176+
177+
def test_bedrock_chat_completion_functions_marked_as_wrapped_for_sdk_compatibility(bedrock_server):
178+
assert bedrock_server._nr_wrapped

tests/mlmodel_openai/test_chat_completion.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,3 +371,9 @@ def test_openai_chat_completion_async_disabled_custom_event_settings(loop):
371371
model="gpt-3.5-turbo", messages=_test_openai_chat_completion_messages, temperature=0.7, max_tokens=100
372372
)
373373
)
374+
375+
376+
def test_openai_chat_completion_functions_marked_as_wrapped_for_sdk_compatibility():
377+
assert openai.ChatCompletion._nr_wrapped
378+
assert openai.ChatCompletion._nr_wrapped
379+
assert openai.util.convert_to_openai_object

tests/mlmodel_openai/test_embeddings.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,9 @@ def test_openai_embedding_async_disabled_custom_insights_events(loop):
148148
loop.run_until_complete(
149149
openai.Embedding.acreate(input="This is an embedding test.", model="text-embedding-ada-002")
150150
)
151+
152+
153+
def test_openai_embedding_functions_marked_as_wrapped_for_sdk_compatibility():
154+
assert openai.Embedding._nr_wrapped
155+
assert openai.Embedding._nr_wrapped
156+
assert openai.util.convert_to_openai_object

0 commit comments

Comments
 (0)