mirror of
https://github.com/BerriAI/litellm.git
synced 2025-04-26 11:14:04 +00:00
fix - basic success logging for redis cache
This commit is contained in:
parent
ea790c1d47
commit
e86fa19257
2 changed files with 46 additions and 2 deletions
|
@ -26,6 +26,16 @@ def print_verbose(print_statement):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def _get_parent_otel_span_from_kwargs(kwargs: Optional[dict] = None):
|
||||||
|
try:
|
||||||
|
if kwargs is None:
|
||||||
|
return None
|
||||||
|
_metadata = kwargs.get("metadata") or {}
|
||||||
|
return _metadata.get("litellm_parent_otel_span")
|
||||||
|
except:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
class BaseCache:
|
class BaseCache:
|
||||||
def set_cache(self, key, value, **kwargs):
|
def set_cache(self, key, value, **kwargs):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
@ -233,6 +243,9 @@ class RedisCache(BaseCache):
|
||||||
service=ServiceTypes.REDIS,
|
service=ServiceTypes.REDIS,
|
||||||
duration=_duration,
|
duration=_duration,
|
||||||
call_type="increment_cache",
|
call_type="increment_cache",
|
||||||
|
start_time=start_time,
|
||||||
|
end_time=end_time,
|
||||||
|
parent_otel_span=_get_parent_otel_span_from_kwargs(kwargs),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
return result
|
return result
|
||||||
|
@ -276,6 +289,9 @@ class RedisCache(BaseCache):
|
||||||
service=ServiceTypes.REDIS,
|
service=ServiceTypes.REDIS,
|
||||||
duration=_duration,
|
duration=_duration,
|
||||||
call_type="async_scan_iter",
|
call_type="async_scan_iter",
|
||||||
|
start_time=start_time,
|
||||||
|
end_time=end_time,
|
||||||
|
# parent_otel_span=_get_parent_otel_span_from_kwargs(kwargs)
|
||||||
)
|
)
|
||||||
) # DO NOT SLOW DOWN CALL B/C OF THIS
|
) # DO NOT SLOW DOWN CALL B/C OF THIS
|
||||||
return keys
|
return keys
|
||||||
|
@ -331,6 +347,9 @@ class RedisCache(BaseCache):
|
||||||
service=ServiceTypes.REDIS,
|
service=ServiceTypes.REDIS,
|
||||||
duration=_duration,
|
duration=_duration,
|
||||||
call_type="async_set_cache",
|
call_type="async_set_cache",
|
||||||
|
start_time=start_time,
|
||||||
|
end_time=end_time,
|
||||||
|
parent_otel_span=_get_parent_otel_span_from_kwargs(kwargs),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
@ -389,6 +408,9 @@ class RedisCache(BaseCache):
|
||||||
service=ServiceTypes.REDIS,
|
service=ServiceTypes.REDIS,
|
||||||
duration=_duration,
|
duration=_duration,
|
||||||
call_type="async_set_cache_pipeline",
|
call_type="async_set_cache_pipeline",
|
||||||
|
start_time=start_time,
|
||||||
|
end_time=end_time,
|
||||||
|
# parent_otel_span=_get_parent_otel_span_from_kwargs(kwargs)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
return results
|
return results
|
||||||
|
@ -434,6 +456,9 @@ class RedisCache(BaseCache):
|
||||||
service=ServiceTypes.REDIS,
|
service=ServiceTypes.REDIS,
|
||||||
duration=_duration,
|
duration=_duration,
|
||||||
call_type="async_increment",
|
call_type="async_increment",
|
||||||
|
start_time=start_time,
|
||||||
|
end_time=end_time,
|
||||||
|
parent_otel_span=_get_parent_otel_span_from_kwargs(kwargs),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
return result
|
return result
|
||||||
|
@ -540,6 +565,9 @@ class RedisCache(BaseCache):
|
||||||
service=ServiceTypes.REDIS,
|
service=ServiceTypes.REDIS,
|
||||||
duration=_duration,
|
duration=_duration,
|
||||||
call_type="async_get_cache",
|
call_type="async_get_cache",
|
||||||
|
start_time=start_time,
|
||||||
|
end_time=end_time,
|
||||||
|
parent_otel_span=_get_parent_otel_span_from_kwargs(kwargs),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
return response
|
return response
|
||||||
|
@ -583,6 +611,9 @@ class RedisCache(BaseCache):
|
||||||
service=ServiceTypes.REDIS,
|
service=ServiceTypes.REDIS,
|
||||||
duration=_duration,
|
duration=_duration,
|
||||||
call_type="async_batch_get_cache",
|
call_type="async_batch_get_cache",
|
||||||
|
start_time=start_time,
|
||||||
|
end_time=end_time,
|
||||||
|
# parent_otel_span=_get_parent_otel_span_from_kwargs(kwargs)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -100,19 +100,32 @@ class OpenTelemetry(CustomLogger):
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from opentelemetry.trace import Status, StatusCode
|
from opentelemetry.trace import Status, StatusCode
|
||||||
|
|
||||||
|
_start_time_ns = start_time
|
||||||
|
_end_time_ns = end_time
|
||||||
|
|
||||||
|
if isinstance(start_time, float):
|
||||||
|
_start_time_ns = int(int(start_time) * 1e9)
|
||||||
|
else:
|
||||||
|
_start_time_ns = self._to_ns(start_time)
|
||||||
|
|
||||||
|
if isinstance(end_time, float):
|
||||||
|
_end_time_ns = int(int(end_time) * 1e9)
|
||||||
|
else:
|
||||||
|
_end_time_ns = self._to_ns(end_time)
|
||||||
|
|
||||||
if parent_otel_span is not None:
|
if parent_otel_span is not None:
|
||||||
_span_name = payload.service
|
_span_name = payload.service
|
||||||
service_logging_span = self.tracer.start_span(
|
service_logging_span = self.tracer.start_span(
|
||||||
name=_span_name,
|
name=_span_name,
|
||||||
context=trace.set_span_in_context(parent_otel_span),
|
context=trace.set_span_in_context(parent_otel_span),
|
||||||
start_time=self._to_ns(start_time),
|
start_time=_start_time_ns,
|
||||||
)
|
)
|
||||||
service_logging_span.set_attribute(key="call_type", value=payload.call_type)
|
service_logging_span.set_attribute(key="call_type", value=payload.call_type)
|
||||||
service_logging_span.set_attribute(
|
service_logging_span.set_attribute(
|
||||||
key="service", value=payload.service.value
|
key="service", value=payload.service.value
|
||||||
)
|
)
|
||||||
service_logging_span.set_status(Status(StatusCode.OK))
|
service_logging_span.set_status(Status(StatusCode.OK))
|
||||||
service_logging_span.end(end_time=self._to_ns(end_time))
|
service_logging_span.end(end_time=_end_time_ns)
|
||||||
|
|
||||||
async def async_post_call_failure_hook(
|
async def async_post_call_failure_hook(
|
||||||
self, original_exception: Exception, user_api_key_dict: UserAPIKeyAuth
|
self, original_exception: Exception, user_api_key_dict: UserAPIKeyAuth
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue