Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
9 changes: 1 addition & 8 deletions holmes/plugins/prompts/_fetch_logs.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,7 @@
** If there are too many logs, or not enough, narrow or widen the timestamps
* If you are not provided with time information. Ignore start_timestamp and end_timestamp. Loki will default to the latest logs.
{%- elif coralogix_ts and coralogix_ts.get_status() == "enabled" -%}
* For any logs, including for investigating kubernetes problems, use fetch_coralogix_logs_for_resource
* Use the tool fetch_coralogix_logs_for_resource to get the logs of any kubernetes pod, coralogix application, or coralogix subsystem
* Prefer fetching logs through coralogix and avoid using kubectl logs commands
* Before fetching logs through coralogix, use `kubectl` commands to get the correct resource names.
* If you have an issue id or finding id, use `fetch_finding_by_id` as it contains time information about the issue (`starts_at`, `updated_at` and `ends_at`).
** Then, defaults to `start_timestamp=-300` (5 minutes before end_timestamp) and `end_timestamp=<issue start_at time>`.
** If there are too many logs, or not enough, narrow or widen the timestamps
* If no logs are found, retry with increasing the start/end time range by one hour.
{% include '_default_log_prompt.jinja2' %}
{%- elif k8s_base_ts and k8s_base_ts.get_status() == "enabled" -%}
{% include '_default_log_prompt.jinja2' %}
{%- elif k8s_yaml_ts and k8s_yaml_ts.get_status() == "enabled" -%}
Expand Down
38 changes: 16 additions & 22 deletions holmes/plugins/toolsets/coralogix/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,18 @@
from holmes.plugins.toolsets.coralogix.utils import (
CoralogixConfig,
CoralogixQueryResult,
get_resource_label,
merge_log_results,
parse_logs,
CoralogixLogsMethodology,
)
from holmes.plugins.toolsets.logging_api import FetchLogsParams
from holmes.plugins.toolsets.utils import (
get_param_or_raise,
process_timestamps_to_rfc3339,
)


DEFAULT_TIME_SPAN_SECONDS = 86400
DEFAULT_LOG_COUNT = 1000
DEFAULT_LOG_COUNT = 2000 # Coralogix's default is 2000


class CoralogixTier(str, Enum):
Expand Down Expand Up @@ -54,35 +53,30 @@ def health_check(domain: str, api_key: str) -> Tuple[bool, str]:
return False, f"Failed with status_code={response.status_code}. {response.text}"


def build_query_string(config: CoralogixConfig, params: Any) -> str:
resource_name = get_param_or_raise(params, "resource_name")
label = get_resource_label(params, config)

namespace_name = params.get("namespace_name", None)

log_count = params.get("log_count", DEFAULT_LOG_COUNT)

def build_query_string(config: CoralogixConfig, params: FetchLogsParams) -> str:
query_filters = []
if namespace_name:
query_filters.append(f"{config.labels.namespace}:{namespace_name}")
query_filters.append(f"{label}:/{resource_name}/")
query_filters.append(f'{config.labels.namespace}:"{params.namespace}"')
query_filters.append(f'{config.labels.pod}:"{params.pod_name}"')

if params.match:
query_filters.append(f'{config.labels.log_message}:"{params.match}"')

query_string = " AND ".join(query_filters)
query_string = f"source logs | lucene '{query_string}' | limit {log_count}"
query_string = f"source logs | lucene '{query_string}' | limit {params.limit or DEFAULT_LOG_COUNT}"
return query_string


def get_start_end(config: CoralogixConfig, params: dict):
def get_start_end(params: FetchLogsParams):
(start, end) = process_timestamps_to_rfc3339(
start_timestamp=params.get("start"),
end_timestamp=params.get("end"),
start_timestamp=params.start_time,
end_timestamp=params.end_time,
default_time_span_seconds=DEFAULT_TIME_SPAN_SECONDS,
)
return (start, end)


def build_query(config: CoralogixConfig, params: dict, tier: CoralogixTier):
(start, end) = get_start_end(config, params)
def build_query(config: CoralogixConfig, params: FetchLogsParams, tier: CoralogixTier):
(start, end) = get_start_end(params)

query_string = build_query_string(config, params)
return {
Expand All @@ -97,7 +91,7 @@ def build_query(config: CoralogixConfig, params: dict, tier: CoralogixTier):


def query_logs_for_tier(
config: CoralogixConfig, params: dict, tier: CoralogixTier
config: CoralogixConfig, params: FetchLogsParams, tier: CoralogixTier
) -> CoralogixQueryResult:
http_status = None
try:
Expand All @@ -122,7 +116,7 @@ def query_logs_for_tier(


def query_logs_for_all_tiers(
config: CoralogixConfig, params: dict
config: CoralogixConfig, params: FetchLogsParams
) -> CoralogixQueryResult:
methodology = config.logs_retrieval_methodology
result: CoralogixQueryResult
Expand Down
137 changes: 43 additions & 94 deletions holmes/plugins/toolsets/coralogix/toolset_coralogix_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@
CallablePrerequisite,
StructuredToolResult,
ToolResultStatus,
Tool,
ToolParameter,
Toolset,
ToolsetTag,
)

from holmes.plugins.toolsets.coralogix.api import (
DEFAULT_LOG_COUNT,
DEFAULT_TIME_SPAN_SECONDS,
build_query_string,
get_start_end,
health_check,
Expand All @@ -22,84 +17,68 @@
build_coralogix_link_to_logs,
stringify_flattened_logs,
)
from holmes.plugins.toolsets.logging_api import (
BaseLoggingToolset,
FetchLogsParams,
LoggingTool,
)
from holmes.plugins.toolsets.utils import (
STANDARD_END_DATETIME_TOOL_PARAM_DESCRIPTION,
TOOLSET_CONFIG_MISSING_ERROR,
standard_start_datetime_tool_param_description,
)


class BaseCoralogixToolset(Toolset):
config: Optional[CoralogixConfig] = None
class CoralogixLogsToolset(BaseLoggingToolset):
def __init__(self):
super().__init__(
name="coralogix/logs",
description="Toolset for interacting with Coralogix to fetch logs",
docs_url="https://docs.robusta.dev/master/configuration/holmesgpt/toolsets/coralogix_logs.html",
icon_url="https://avatars.githubusercontent.com/u/35295744?s=200&v=4",
prerequisites=[CallablePrerequisite(callable=self.prerequisites_callable)],
tools=[
LoggingTool(self),
],
tags=[ToolsetTag.CORE],
)

def get_example_config(self):
example_config = CoralogixConfig(
api_key="<cxuw_...>", team_hostname="my-team", domain="eu2.coralogix.com"
)
return example_config.model_dump()

def prerequisites_callable(self, config: dict[str, Any]) -> Tuple[bool, str]:
if not config:
return False, TOOLSET_CONFIG_MISSING_ERROR
self.config = CoralogixConfig(**config)
if self.config.api_key:
return health_check(domain=self.config.domain, api_key=self.config.api_key)
else:
return False, "Missing configuration field 'api_key'"

class BaseCoralogixTool(Tool):
toolset: BaseCoralogixToolset


class FetchLogs(BaseCoralogixTool):
def __init__(self, toolset: BaseCoralogixToolset):
super().__init__(
name="fetch_coralogix_logs_for_resource",
description="Retrieve logs using coralogix",
parameters={
"resource_type": ToolParameter(
description="The type of resource. Can be one of pod, application or subsystem. Defaults to pod.",
type="string",
required=False,
),
"resource_name": ToolParameter(
description='Regular expression to match the resource name. This can be a regular expression. For example "<pod-name>.*" will match any pod name starting with "<pod-name>"',
type="string",
required=True,
),
"namespace_name": ToolParameter(
description="The Kubernetes namespace to filter logs",
type="string",
required=False,
),
"start": ToolParameter(
description=standard_start_datetime_tool_param_description(
DEFAULT_TIME_SPAN_SECONDS
),
type="string",
required=False,
),
"end": ToolParameter(
description=STANDARD_END_DATETIME_TOOL_PARAM_DESCRIPTION,
type="string",
required=False,
),
"log_count": ToolParameter(
description=f"Maximum number of logs to retrieve (default: {DEFAULT_LOG_COUNT})",
type="integer",
required=False,
),
},
toolset=toolset,
)
@property
def coralogix_config(self) -> Optional[CoralogixConfig]:
return self.config

def _invoke(self, params: Any) -> StructuredToolResult:
if not self.toolset.config:
def fetch_logs(self, params: FetchLogsParams) -> StructuredToolResult:
if not self.coralogix_config:
return StructuredToolResult(
status=ToolResultStatus.ERROR,
error="The coralogix/logs toolset is not configured",
data=None,
params=params,
error=f"The {self.name} toolset is not configured",
params=params.model_dump(),
)

logs_data = query_logs_for_all_tiers(config=self.toolset.config, params=params)
(start, end) = get_start_end(config=self.toolset.config, params=params)
query_string = build_query_string(config=self.toolset.config, params=params)
logs_data = query_logs_for_all_tiers(
config=self.coralogix_config, params=params
)
(start, end) = get_start_end(params=params)
query_string = build_query_string(config=self.coralogix_config, params=params)

url = build_coralogix_link_to_logs(
config=self.toolset.config, lucene_query=query_string, start=start, end=end
config=self.coralogix_config,
lucene_query=query_string,
start=start,
end=end,
)

data: str
Expand All @@ -118,35 +97,5 @@ def _invoke(self, params: Any) -> StructuredToolResult:
data=data,
url=url,
invocation=query_string,
params=params,
)

def get_parameterized_one_liner(self, params) -> str:
if not self.toolset.config:
return "The coralogix/logs toolset is not configured"
query_string = build_query_string(self.toolset.config, params)
return f"fetching coralogix logs. query={query_string}"


class CoralogixLogsToolset(BaseCoralogixToolset):
def __init__(self):
super().__init__(
name="coralogix/logs",
description="Toolset for interacting with Coralogix to fetch logs",
docs_url="https://docs.robusta.dev/master/configuration/holmesgpt/toolsets/coralogix_logs.html",
icon_url="https://avatars.githubusercontent.com/u/35295744?s=200&v=4",
prerequisites=[CallablePrerequisite(callable=self.prerequisites_callable)],
tools=[
FetchLogs(self),
],
tags=[ToolsetTag.CORE],
params=params.model_dump(),
)

def prerequisites_callable(self, config: dict[str, Any]) -> Tuple[bool, str]:
if not config:
return False, TOOLSET_CONFIG_MISSING_ERROR
self.config = CoralogixConfig(**config)
if self.config.api_key:
return health_check(domain=self.config.domain, api_key=self.config.api_key)
else:
return False, "Missing configuration field 'api_key'"
Loading