Skip to content

Commit

Permalink
fix: retrieve provider and product type in search metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
dalpasso committed Jan 11, 2024
1 parent 2b889a9 commit 2fa09fc
Showing 1 changed file with 34 additions and 11 deletions.
45 changes: 34 additions & 11 deletions eodag/utils/instrumentation/eodag/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ def _instrument_search(
:type request_overhead_duration_seconds: Histogram
"""
overhead_timers: Dict[str, OverheadTimer] = {}
trace_attributes: Dict[str, Any] = {}

wrapped_server_search_stac_items = server.search_stac_items

@functools.wraps(wrapped_server_search_stac_items)
Expand All @@ -128,16 +130,26 @@ def wrapper_server_search_stac_items(
provider: Optional[str] = None,
method: Optional[str] = "GET",
) -> Dict[str, Any]:
args_collections = arguments.get("collections", None)
if isinstance(args_collections, str):
product_type = args_collections.split(",")[0] if args_collections else None
elif isinstance(args_collections, list) and len(args_collections) > 0:
product_type = args_collections[0]
else:
product_type = None

# use catalogs from path or if it is empty, collections from args
collections = arguments.get("collections", None)
product_type = None
if catalogs:
product_type = catalogs[0]
elif collections:
if isinstance(collections, str):
product_type = collections.split(",")[0] if collections else None
elif isinstance(collections, list) and len(collections) > 0:
product_type = collections[0]
if not product_type:
logger.warning("Collections argument type should be Array")

span_name = "core-search"
attributes = {"provider": provider, "product_type": product_type}
attributes = {}
if provider:
attributes["provider"] = provider
if product_type:
attributes["product_type"] = product_type

with tracer.start_as_current_span(
span_name, kind=SpanKind.CLIENT, attributes=attributes
Expand All @@ -146,11 +158,9 @@ def wrapper_server_search_stac_items(
trace_id = span.get_span_context().trace_id
timer = OverheadTimer()
overhead_timers[trace_id] = timer
trace_attributes[trace_id] = attributes
timer.start_global_timer()

# Product type counter
searched_product_types_counter.add(1, attributes)

# Call wrapped function
try:
result = wrapped_server_search_stac_items(
Expand All @@ -161,6 +171,13 @@ def wrapper_server_search_stac_items(
finally:
timer.stop_global_timer()

# Retrieve possible updated attributes
attributes = trace_attributes[trace_id]
span.set_attributes(attributes)

# Product type counter
searched_product_types_counter.add(1, attributes)

# Duration histograms
request_duration_seconds.record(
timer.get_global_time(), attributes=attributes
Expand All @@ -169,6 +186,7 @@ def wrapper_server_search_stac_items(
timer.get_overhead_time(), attributes=attributes
)
del overhead_timers[trace_id]
del trace_attributes[trace_id]

if exception is not None:
raise exception.with_traceback(exception.__traceback__)
Expand All @@ -189,6 +207,7 @@ def wrapper_qssearch_request(
) -> Response:

span_name = "core-search"
# This is the provider's product type.
attributes = {
"provider": self.provider,
"product_type": self.product_type_def_params["productType"],
Expand All @@ -200,6 +219,10 @@ def wrapper_qssearch_request(
exception = None
trace_id = span.get_span_context().trace_id
timer = overhead_timers.get(trace_id)
parent_attributes = trace_attributes.get(trace_id)
parent_attributes["provider"] = self.provider
attributes = parent_attributes

start_time = default_timer()

# Call wrapped function
Expand Down

0 comments on commit 2fa09fc

Please sign in to comment.