Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion grafana_loki_mcp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
# Default configuration
DEFAULT_GRAFANA_URL = os.environ.get("GRAFANA_URL", "")
DEFAULT_GRAFANA_API_KEY = os.environ.get("GRAFANA_API_KEY", "")
DEFAULT_MAX_LOG_LINES = int(os.environ.get("MAX_LOG_LINES", "100"))


class GrafanaClient:
Expand Down Expand Up @@ -554,7 +555,7 @@ def query_loki(
Optional[str],
"End time (Grafana format like 'now', ISO format, Unix timestamp, or RFC3339)",
] = None,
limit: Annotated[int, "Maximum number of log lines to return"] = 100,
limit: Annotated[int, "Maximum number of log lines to return"] = DEFAULT_MAX_LOG_LINES,
direction: Annotated[str, "Query direction ('forward' or 'backward')"] = "backward",
max_per_line: Annotated[
int, "Maximum characters per log line (0 for unlimited)"
Expand Down
28 changes: 28 additions & 0 deletions tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,3 +328,31 @@ def test_query_loki_time_range(
# Expect nanoseconds since epoch for ISO8601 as strings
assert kwargs["params"]["start"] == "1710410400000000000"
assert kwargs["params"]["end"] == "1710414000000000000"


@patch.dict(os.environ, {"MAX_LOG_LINES": "50"})
def test_max_log_lines_environment_variable() -> None:
"""Test that MAX_LOG_LINES environment variable is used as default."""
# Import after setting environment variable to ensure it's picked up
import importlib

# Force reload the module to pick up the new environment variable
import grafana_loki_mcp.server
importlib.reload(grafana_loki_mcp.server)

# Check that the environment variable is properly read
assert grafana_loki_mcp.server.DEFAULT_MAX_LOG_LINES == 50


@patch.dict(os.environ, {}, clear=True)
def test_max_log_lines_default_value() -> None:
"""Test that MAX_LOG_LINES defaults to 100 when not set."""
# Import after clearing environment variables
import importlib

# Force reload the module to pick up the cleared environment
import grafana_loki_mcp.server
importlib.reload(grafana_loki_mcp.server)

# Check that the default value is used
assert grafana_loki_mcp.server.DEFAULT_MAX_LOG_LINES == 100