Skip to content

Commit

Permalink
feat: add loki apis to core (#616)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tbaile authored Apr 3, 2024
1 parent af8e83d commit e4d5530
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 2 deletions.
5 changes: 4 additions & 1 deletion core/api-server/api-server-logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ var (
entityNameFlag = ""
searchFlag = ""
timezone = ""
instance = ""
)

var RootCmd = &cobra.Command{
Expand Down Expand Up @@ -84,6 +85,7 @@ func Execute() {
LogsCmd.Flags().StringVarP(&entityNameFlag, "name", "n", "", "get logs for a specific entity name. used in node or module")
LogsCmd.Flags().StringVarP(&searchFlag, "search", "s", "", "get logs for a specific search string")
LogsCmd.Flags().StringVarP(&timezone, "timezone", "z", "", "get logs in a specific timezone")
LogsCmd.Flags().StringVarP(&instance, "instance", "i", "", "search for logs in a specific instance. (Example: loki1, loki2, ...)")

// check errors on cmd execution
if err := RootCmd.Execute(); err != nil {
Expand All @@ -110,7 +112,8 @@ func Logs() {
"to": "` + toFlag + `",
"entity": "` + entityFlag + `",
"entity_name": "` + entityNameFlag + `",
"timezone": "` + timezone + `"
"timezone": "` + timezone + `",
"instance": "` + instance + `"
}
}
`
Expand Down
1 change: 1 addition & 0 deletions core/api-server/models/socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type LogsStartAction struct {
Entity string `json:"entity" structs:"entity"`
EntityName string `json:"entity_name" structs:"entity_name"`
TimeZone string `json:"timezone" structs:"timezone"`
Instance string `json:"instance" structs:"instance"`
}

type LogsStopAction struct {
Expand Down
4 changes: 4 additions & 0 deletions core/api-server/socket/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ func Action(socketAction models.SocketAction, s *melody.Session, wg *sync.WaitGr
envs = append(envs, "TZ="+timezone)
}

if len(logsAction.Instance) > 0 {
envs = append(envs, "LOKI_INSTANCE="+logsAction.Instance)
}

// check date
if len(logsAction.From) > 0 {
from = "--from=" + logsAction.From
Expand Down
5 changes: 4 additions & 1 deletion core/imageroot/usr/local/bin/logcli
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ if 'LOKI_ADDR' in os.environ:
logcli_env = os.environ
else:
rdb = agent.redis_connect(use_replica=True)
loki_agent_id = agent.resolve_agent_id("loki@cluster")
if 'LOKI_INSTANCE' in os.environ:
loki_agent_id = 'module/' + os.getenv('LOKI_INSTANCE')
else:
loki_agent_id = agent.resolve_agent_id("loki@cluster")
loki_env = rdb.hgetall(loki_agent_id + '/environment') or {}

logcli_env = os.environ.copy()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env python3

#
# Copyright (C) 2024 Nethesis S.r.l.
# SPDX-License-Identifier: GPL-3.0-or-later
#

import json
import sys

import agent
from cluster import modules

redis_client = agent.redis_connect()
default_instance = redis_client.get("cluster/default_instance/loki")
instances = modules.list_installed(redis_client).get("ghcr.io/nethserver/loki", [])

response = []
for instance in instances:
item = {
"instance_id": instance["id"],
"instance_label": instance["ui_name"],
"node_id": instance["node"],
"node_label": instance["node_ui_name"],
"active": instance["id"] == default_instance
}

try:
task_response = agent.tasks.run(
agent_id=f'module/{instance["id"]}',
action='get-configuration',
endpoint="redis://cluster-leader"
)
if task_response["exit_code"] == 0:
item["offline"] = False
item["retention_days"] = task_response["output"]["retention_days"]
item["active_from"] = task_response["output"]["active_from"]
if "active_to" in task_response["output"]:
item["active_to"] = task_response["output"]["active_to"]
else:
item["offline"] = True
except Exception as e:
item["offline"] = True

response.append(item)

json.dump({
'instances': response
}, sys.stdout)
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "List loki instances",
"$id": "http://schema.nethserver.org/cluster/list-loki-instances-output.json",
"type": "object",
"required": [
"instances"
],
"properties": {
"instances": {
"type": "array",
"items": {
"type": "object",
"required": [
"instance_id",
"instance_label",
"node_id",
"node_label",
"active",
"offline"
],
"properties": {
"instance_id": {
"type": "string",
"description": "The Loki instance identifier."
},
"instance_label": {
"type": "string",
"description": "The Loki instance label, if empty string, there's no label."
},
"node_id": {
"type": "string",
"description": "The node identifier where the Loki instance is running."
},
"node_label": {
"type": "string",
"description": "The node label where the Loki instance is running, can be empty."
},
"active": {
"type": "boolean",
"description": "The Loki instance is the currently active."
},
"offline": {
"type": "boolean",
"description": "The Loki instance is offline."
},
"retention_days": {
"type": "integer",
"description": "The retention days for the Loki instance."
},
"active_from": {
"type": "string",
"format": "date-time",
"description": "The ISO 8601 date-time when the Loki instance was activated."
},
"active_to": {
"type": "string",
"format": "date-time",
"description": "The ISO 8601 date-time when the Loki instance was deactivated."
}
}
}
}
}
}

0 comments on commit e4d5530

Please sign in to comment.