1
0
mirror of https://github.com/ManyakRus/telegram_loki.git synced 2024-12-04 21:05:12 +02:00

сделал TextError

This commit is contained in:
Nikitin Aleksandr 2023-10-23 10:29:01 +03:00
parent 28c30114b2
commit e6d6b84a35
8 changed files with 33 additions and 11 deletions

View File

@ -4,9 +4,14 @@ TELEGRAM_APP_HASH=
TELEGRAM_PHONE_FROM=
TELEGRAM_PHONE_SEND_TEST=
TELEGRAM_CHAT_NAME=
TELEGRAM_MESSAGES_COUNT=
TELEGRAM_MESSAGES_COUNT=1
#http://logmon.yoursite.domen
LOKI_URL=
#LOKI_SEARCH_TEXT - is regular expression. Need escape characters, "[" = "\\\\["
LOKI_SEARCH_TEXT="\\\\[ERROR\\\\]|\\\\[PANIC\\\\]|level=error|level=panic|error:|panic:"
GRAFANA_LOGIN=
GRAFANA_PASSWORD=

View File

@ -24,6 +24,8 @@ TELEGRAM_APP_HASH=
TELEGRAM_PHONE_FROM=
TELEGRAM_PHONE_SEND_TEST=
LOKI_URL=
LOKI_SEARCH_TEXT="\\\\[ERROR\\\\]|\\\\[PANIC\\\\]|level=error|level=panic"
GRAFANA_LOGIN=
GRAFANA_PASSWORD=
TELEGRAM_CHAT_NAME=

View File

@ -1 +1 @@
2023-10-19 07:52:38.831
2023-10-19 12:46:11.219

View File

@ -1 +1 @@
00135
00140

View File

@ -11,12 +11,13 @@ var Settings SettingsINI
// SettingsINI - структура для хранения всех нужных переменных окружения
type SettingsINI struct {
LOKI_URL string
TELEGRAM_CHAT_NAME string
GRAFANA_LOGIN string
GRAFANA_PASSWORD string
INTERVAL_SEND_MINUTES int
LOKI_URL string
LOKI_SEARCH_TEXT string
TELEGRAM_CHAT_NAME string
TELEGRAM_MESSAGES_COUNT int
INTERVAL_SEND_MINUTES int
}
// FillSettings загружает переменные окружения в структуру из переменных окружения
@ -36,6 +37,11 @@ func FillSettings() {
if err != nil || Settings.TELEGRAM_MESSAGES_COUNT == 0 {
Settings.TELEGRAM_MESSAGES_COUNT = 1
}
Settings.LOKI_SEARCH_TEXT = os.Getenv("LOKI_SEARCH_TEXT")
if Settings.LOKI_SEARCH_TEXT == "" {
Settings.LOKI_SEARCH_TEXT = "error:|panic:|ERROR:|PANIC:"
//Settings.LOKI_SEARCH_TEXT = "error:%7Cpanic:%7CERROR:%7CPANIC:"
}
if Settings.TELEGRAM_CHAT_NAME == "" {
log.Panic("Error: Need fill TELEGRAM_CHAT_NAME")

View File

@ -66,12 +66,14 @@ func Start_period(Date1, Date2 time.Time) {
}
// Start_period1 - запускает чтение логов одного сервиса за период
func Start_period1(ServiceName, DeveloperName string, DateFrom, DateTo time.Time) {
func Start_period1(ServiceName, DeveloperName string, DateFrom, DateTo time.Time) error {
var err error
LokiMessage, err := loki.DownloadJSON(ServiceName, DateFrom, DateTo)
if err != nil {
log.Error("TestDownloadJSON() error: ", err)
log.Error("DownloadJSON() error: ", err)
micro.Pause(1000)
return err
}
for _, Result1 := range LokiMessage.Data.Result {
@ -118,6 +120,7 @@ func Start_period1(ServiceName, DeveloperName string, DateFrom, DateTo time.Time
}
}
return err
}
// FindURLLoki - находит URL ссылку в LOKI на которую можно кликнуть в телеграмме

View File

@ -2,12 +2,14 @@ package loki
import (
"encoding/json"
"errors"
"fmt"
"github.com/ManyakRus/starter/log"
"github.com/ManyakRus/telegram_loki/internal/config"
"github.com/ManyakRus/telegram_loki/internal/types"
"io"
"net/http"
"net/url"
"strconv"
"time"
)
@ -20,7 +22,8 @@ func QueryApp(ServiceName string, DateFrom, DateTo time.Time, Filter string) str
slimit := "1000"
query := "%7Bapp%3D%22" + ServiceName + "%22%7D"
if Filter != "" {
query = query + "%7C~%22(" + Filter + ")%22"
FilterURL := url.QueryEscape(Filter)
query = query + "%7C~%22(" + FilterURL + ")%22"
slimit = strconv.Itoa(config.Settings.TELEGRAM_MESSAGES_COUNT)
}
@ -36,7 +39,7 @@ func DownloadJSON(ServiceName string, DateFrom, DateTo time.Time) (types.Message
Otvet := types.Message{}
var err error
Filter := "error:%7Cpanic:%7CERROR:%7CPANIC:"
Filter := config.Settings.LOKI_SEARCH_TEXT
URL := QueryApp(ServiceName, DateFrom, DateTo, Filter)
//query := "%7Bapp%3D%22" + ServiceName + "%22%7D%7C~%22(error:%7Cpanic:%7CERROR:%7CPANIC:)%22"
//sTime1 := strconv.FormatInt(DateFrom.UnixNano(), 10)
@ -65,7 +68,9 @@ func DownloadJSON(ServiceName string, DateFrom, DateTo time.Time) (types.Message
case 200:
default:
{
log.Error("http request error: ", response.Status)
TextError := fmt.Sprint("http request error: ", response.Status)
err = errors.New(TextError)
log.Error(TextError)
return Otvet, err
}
}

View File

@ -24,6 +24,7 @@ TELEGRAM_APP_HASH=
TELEGRAM_PHONE_FROM=
TELEGRAM_PHONE_SEND_TEST=
LOKI_URL=
LOKI_SEARCH_TEXT="\\\\[ERROR\\\\]|\\\\[PANIC\\\\]|level=error|level=panic"
GRAFANA_LOGIN=
GRAFANA_PASSWORD=
TELEGRAM_CHAT_NAME=