Skip to content

Commit

Permalink
Added calls to error handlers (#274)
Browse files Browse the repository at this point in the history
* Added handler errors to send stack to platform

* added exec func

* fixed the error handler call in the main module.

* increased version

* Fixed syntax
  • Loading branch information
kochetovd authored Mar 4, 2024
1 parent bc597e0 commit 5a9da9a
Show file tree
Hide file tree
Showing 15 changed files with 121 additions and 28 deletions.
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

const (
ReleemAgentVersion = "1.12.0"
ReleemAgentVersion = "1.13.0"
)

type Config struct {
Expand Down
2 changes: 1 addition & 1 deletion current_version_agent
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.12.0
1.13.0
63 changes: 63 additions & 0 deletions errors/releemErrors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package errors

import (
"net/http"
"strings"

"github.com/Releem/mysqlconfigurer/config"
"github.com/advantageous/go-logback/logging"

"time"
)

type ReleemErrorsRepeater struct {
logger logging.Logger
configuration *config.Config
}

func (repeater ReleemErrorsRepeater) ProcessErrors(message string) interface{} {
var env string
bodyReader := strings.NewReader(message)

repeater.logger.Debug("Result Send data: ", message)
var api_domain string
if repeater.configuration != nil {
env = repeater.configuration.Env
} else {
env = "prod"
}
if env == "dev2" {
api_domain = "https://api.dev2.releem.com/v2/events/saving_agent_errors_log"
} else if env == "dev" {
api_domain = "https://api.dev.releem.com/v2/events/saving_agent_errors_log"
} else if env == "stage" {
api_domain = "https://api.stage.releem.com/v2/events/saving_agent_errors_log"
} else {
api_domain = "https://api.releem.com/v2/events/saving_agent_errors_log"
}
req, err := http.NewRequest(http.MethodPost, api_domain, bodyReader)
if err != nil {
repeater.logger.Error("Request: could not create request: ", err)
return nil
}
if repeater.configuration != nil {
req.Header.Set("x-releem-api-key", repeater.configuration.ApiKey)
}

client := http.Client{
Timeout: 30 * time.Second,
}

res, err := client.Do(req)
if err != nil {
repeater.logger.Error("Request: error making http request: ", err)
return nil
}
repeater.logger.Debug("Response: status code: ", res.StatusCode)
return nil
}

func NewReleemErrorsRepeater(configuration *config.Config) ReleemErrorsRepeater {
logger := logging.NewSimpleLogger("ReleemRepeaterMetrics")
return ReleemErrorsRepeater{logger, configuration}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/rds v1.64.5
github.com/go-sql-driver/mysql v1.7.1
github.com/hashicorp/hcl v1.0.0
github.com/pkg/errors v0.9.1
github.com/shirou/gopsutil/v3 v3.23.11
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGw
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4=
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw=
Expand Down
6 changes: 3 additions & 3 deletions install.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/bin/bash
# install.sh - Version 1.12.0
# install.sh - Version 1.13.0
# (C) Releem, Inc 2022
# All rights reserved

# Releem installation script: install and set up the Releem Agent on supported Linux distributions
# using the package manager.

set -e
install_script_version=1.12.0
install_script_version=1.13.0
logfile="releem-install.log"

WORKDIR="/opt/releem"
Expand All @@ -28,7 +28,7 @@ exec 1>&-
exec 1>$npipe 2>&1

function on_exit() {
curl -s -L -d @$logfile -H "x-releem-api-key: $RELEEM_API_KEY" -H "Content-Type: application/json" -X POST https://api.releem.com/v1/events/saving_log
curl -s -L -d @$logfile -H "x-releem-api-key: $RELEEM_API_KEY" -H "Content-Type: application/json" -X POST https://api.releem.com/v2/events/saving_log
rm -f $npipe
}

Expand Down
6 changes: 4 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/Releem/daemon"
"github.com/Releem/mysqlconfigurer/config"
"github.com/Releem/mysqlconfigurer/metrics"
m "github.com/Releem/mysqlconfigurer/metrics"
r "github.com/Releem/mysqlconfigurer/repeater"
t "github.com/Releem/mysqlconfigurer/tasks"
Expand Down Expand Up @@ -58,9 +57,12 @@ func IsPath(path string, logger logging.Logger) bool {
// Manage by daemon commands or run the daemon
func (service *Service) Manage(logger logging.Logger, configFile string, command []string, TypeConfiguration string, AgentEvents string, AgentTask string) (string, error) {
var gatherers, gatherers_configuration []m.MetricsGatherer
var Mode metrics.Mode
var Mode m.Mode
var configuration *config.Config
usage := "Usage: myservice install | remove | start | stop | status"

defer m.HandlePanic(configuration, logger)

// if received any kind of command, do it
if len(command) >= 1 {
switch command[0] {
Expand Down
18 changes: 18 additions & 0 deletions metrics/Metrics.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package metrics

import (
"fmt"

"github.com/Releem/mysqlconfigurer/config"
e "github.com/Releem/mysqlconfigurer/errors"
"github.com/advantageous/go-logback/logging"
"github.com/pkg/errors"
)

type MetricType byte
type MetricIntervalType byte

Expand Down Expand Up @@ -75,3 +84,12 @@ func MapJoin(map1, map2 MetricGroupValue) MetricGroupValue {
}
return map1
}

func HandlePanic(configuration *config.Config, logger logging.Logger) {
if r := recover(); r != nil {
err := errors.WithStack(fmt.Errorf("%v", r))
logger.Printf("%+v", err)
sender := e.NewReleemErrorsRepeater(configuration)
sender.ProcessErrors(fmt.Sprintf("%+v", err))
}
}
4 changes: 2 additions & 2 deletions metrics/dbMetricsBase.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func NewDbMetricsBaseGatherer(logger logging.Logger, db *sql.DB, configuration *

func (DbMetricsBase *DbMetricsBaseGatherer) GetMetrics(metrics *Metrics) error {
// Mysql Status
output := make(MetricGroupValue)
{
output := make(MetricGroupValue)
var row MetricValue
rows, err := DbMetricsBase.db.Query("SHOW STATUS")

Expand All @@ -51,8 +51,8 @@ func (DbMetricsBase *DbMetricsBaseGatherer) GetMetrics(metrics *Metrics) error {
}
output[row.name] = row.value
}

rows.Close()

rows, err = DbMetricsBase.db.Query("SHOW GLOBAL STATUS")
if err != nil {
DbMetricsBase.logger.Error(err)
Expand Down
7 changes: 7 additions & 0 deletions metrics/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ func makeTerminateChannel() <-chan os.Signal {
func RunWorker(gatherers []MetricsGatherer, gatherers_configuration []MetricsGatherer, repeaters map[string]MetricsRepeater, logger logging.Logger,
configuration *config.Config, configFile string, Mode Mode) {
var GenerateTimer *time.Timer
defer HandlePanic(configuration, logger)
if logger == nil {
if configuration.Debug {
logger = logging.NewSimpleDebugLogger("Worker")
} else {
logger = logging.NewSimpleLogger("Worker")
}
}

logger.Debug(configuration)
timer := time.NewTimer(1 * time.Second)
configTimer := time.NewTimer(configuration.ReadConfigSeconds * time.Second)
Expand All @@ -53,6 +55,7 @@ func RunWorker(gatherers []MetricsGatherer, gatherers_configuration []MetricsGat

timer.Reset(configuration.TimePeriodSeconds * time.Second)
go func() {
defer HandlePanic(configuration, logger)
Ready = false
metrics := collectMetrics(gatherers, logger)
if Ready {
Expand All @@ -76,6 +79,7 @@ func RunWorker(gatherers []MetricsGatherer, gatherers_configuration []MetricsGat
case <-GenerateTimer.C:
GenerateTimer.Reset(configuration.GenerateConfigSeconds * time.Second)
go func() {
defer HandlePanic(configuration, logger)
Ready = false
logger.Println(" * Collecting metrics to recommend a config...")
metrics := collectMetrics(append(gatherers, gatherers_configuration...), logger)
Expand All @@ -97,6 +101,7 @@ func processTaskFunc(metrics Metrics, repeaters map[string]MetricsRepeater, logg
}

func processTask(metrics Metrics, repeaters map[string]MetricsRepeater, logger logging.Logger, configuration *config.Config) {
defer HandlePanic(configuration, logger)
output := make(MetricGroupValue)
//metrics := collectMetrics(gatherers, logger)
task := processRepeaters(metrics, repeaters["Tasks"], configuration, logger)
Expand Down Expand Up @@ -191,6 +196,8 @@ func processTask(metrics Metrics, repeaters map[string]MetricsRepeater, logger l

func processRepeaters(metrics Metrics, repeaters MetricsRepeater,
configuration *config.Config, logger logging.Logger) interface{} {
defer HandlePanic(configuration, logger)

result, err := repeaters.ProcessMetrics(configuration, metrics)
if err != nil {
logger.PrintError("Repeater failed", err)
Expand Down
6 changes: 3 additions & 3 deletions mysqlconfigurer.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
# mysqlconfigurer.sh - Version 1.12.0
# mysqlconfigurer.sh - Version 1.13.0
# (C) Releem, Inc 2022
# All rights reserved

Expand All @@ -12,7 +12,7 @@ MYSQLTUNER_REPORT=$MYSQLCONFIGURER_PATH"mysqltunerreport.json"
RELEEM_MYSQL_VERSION=$MYSQLCONFIGURER_PATH"mysql_version"
MYSQLCONFIGURER_CONFIGFILE="${MYSQLCONFIGURER_PATH}${MYSQLCONFIGURER_FILE_NAME}"
MYSQL_MEMORY_LIMIT=0
VERSION="1.12.0"
VERSION="1.13.0"
RELEEM_INSTALL_PATH=$MYSQLCONFIGURER_PATH"install.sh"
logfile="releem-mysqlconfigurer.log"

Expand All @@ -26,7 +26,7 @@ exec 1>&-
exec 1>$npipe 2>&1

function on_exit() {
curl -s -L -d @$logfile -H "x-releem-api-key: $RELEEM_API_KEY" -H "Content-Type: application/json" -X POST https://api.releem.com/v1/events/configurer_log
curl -s -L -d @$logfile -H "x-releem-api-key: $RELEEM_API_KEY" -H "Content-Type: application/json" -X POST https://api.releem.com/v2/events/configurer_log
rm -f $npipe
}

Expand Down
8 changes: 4 additions & 4 deletions repeater/releemEvents.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ func (repeater ReleemEventsRepeater) ProcessMetrics(context m.MetricContext, met
var api_domain string
env := context.GetEnv()
if env == "dev2" {
api_domain = "https://api.dev2.releem.com/v1/events/"
api_domain = "https://api.dev2.releem.com/v2/events/"
} else if env == "dev" {
api_domain = "https://api.dev.releem.com/v1/events/"
api_domain = "https://api.dev.releem.com/v2/events/"
} else if env == "stage" {
api_domain = "https://api.stage.releem.com/v1/events/"
api_domain = "https://api.stage.releem.com/v2/events/"
} else {
api_domain = "https://api.releem.com/v1/events/"
api_domain = "https://api.releem.com/v2/events/"
}
api_domain += repeater.Mode.ModeType

Expand Down
8 changes: 4 additions & 4 deletions tasks/taskGet.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ func (repeater ReleemTasksRepeater) ProcessMetrics(context m.MetricContext, metr
var api_domain string
env := context.GetEnv()
if env == "dev2" {
api_domain = "https://api.dev2.releem.com/v1/tasks/task_get"
api_domain = "https://api.dev2.releem.com/v2/tasks/task_get"
} else if env == "dev" {
api_domain = "https://api.dev.releem.com/v1/tasks/task_get"
api_domain = "https://api.dev.releem.com/v2/tasks/task_get"
} else if env == "stage" {
api_domain = "https://api.stage.releem.com/v1/tasks/task_get"
api_domain = "https://api.stage.releem.com/v2/tasks/task_get"
} else {
api_domain = "https://api.releem.com/v1/tasks/task_get"
api_domain = "https://api.releem.com/v2/tasks/task_get"
}
req, err := http.NewRequest(http.MethodPost, api_domain, bodyReader)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions tasks/taskSet.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ func (repeater ReleemTaskSetRepeater) ProcessMetrics(context m.MetricContext, me
var api_domain string
env := context.GetEnv()
if env == "dev2" {
api_domain = "https://api.dev2.releem.com/v1/tasks/"
api_domain = "https://api.dev2.releem.com/v2/tasks/"
} else if env == "dev" {
api_domain = "https://api.dev.releem.com/v1/tasks/"
api_domain = "https://api.dev.releem.com/v2/tasks/"
} else if env == "stage" {
api_domain = "https://api.stage.releem.com/v1/tasks/"
api_domain = "https://api.stage.releem.com/v2/tasks/"
} else {
api_domain = "https://api.releem.com/v1/tasks/"
api_domain = "https://api.releem.com/v2/tasks/"
}
api_domain += repeater.Mode.ModeType

Expand Down
8 changes: 4 additions & 4 deletions tasks/taskStatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ func (repeater ReleemTaskStatusRepeater) ProcessMetrics(context m.MetricContext,
var api_domain string
env := context.GetEnv()
if env == "dev2" {
api_domain = "https://api.dev2.releem.com/v1/tasks/task_status"
api_domain = "https://api.dev2.releem.com/v2/tasks/task_status"
} else if env == "dev" {
api_domain = "https://api.dev.releem.com/v1/tasks/task_status"
api_domain = "https://api.dev.releem.com/v2/tasks/task_status"
} else if env == "stage" {
api_domain = "https://api.stage.releem.com/v1/tasks/task_status"
api_domain = "https://api.stage.releem.com/v2/tasks/task_status"
} else {
api_domain = "https://api.releem.com/v1/tasks/task_status"
api_domain = "https://api.releem.com/v2/tasks/task_status"
}
req, err := http.NewRequest(http.MethodPost, api_domain, bodyReader)
if err != nil {
Expand Down

0 comments on commit 5a9da9a

Please sign in to comment.