Skip to content

Commit

Permalink
enhanced api logging
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFireMike committed Dec 23, 2020
1 parent 54f46aa commit e574ca4
Show file tree
Hide file tree
Showing 12 changed files with 139 additions and 136 deletions.
2 changes: 1 addition & 1 deletion api/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func LoggerMiddleware() echo.MiddlewareFunc {

evt.Str("bytes_in", cl)
evt.Str("bytes_out", strconv.FormatInt(res.Size, 10))
evt.Msg("")
evt.Msg("handled request")

return err
}
Expand Down
42 changes: 0 additions & 42 deletions cmd/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,11 @@
package cmd

import (
"context"
"fmt"
"github.com/inexio/thola/core/database"
"github.com/inexio/thola/core/parser"
"github.com/inexio/thola/core/request"
"github.com/rs/xid"
"github.com/rs/zerolog/log"
flag "github.com/spf13/pflag"
"github.com/spf13/viper"
"os"
)

func addBinarySpecificDeviceFlags(fs *flag.FlagSet) {
fs.StringSlice("snmp-community", defaultSNMPCommunity, "Community strings for SNMP to use")
fs.StringSlice("snmp-version", defaultSNMPVersion, "SNMP versions to use (1, 2c or 3)")
fs.IntSlice("snmp-port", defaultSNMPPort, "Ports for SNMP to use")
}

func handleRequest(r request.Request) {
logger := log.With().Str("request_id", xid.New().String()).Logger()
ctx := logger.WithContext(context.Background())

db, err := database.GetDB(ctx)
if err != nil {
handleError(ctx, err)
os.Exit(3)
}

resp, err := request.ProcessRequest(ctx, r)
if err != nil {
handleError(ctx, err)
_ = db.CloseConnection(ctx)
os.Exit(3)
}

err = db.CloseConnection(ctx)
if err != nil {
handleError(ctx, err)
os.Exit(3)
}

b, err := parser.Parse(resp, viper.GetString("format"))
if err != nil {
log.Ctx(ctx).Error().Err(err).Msg("Request successful, but failed to parse response")
os.Exit(3)
}

fmt.Printf("%s\n", b)
os.Exit(resp.GetExitCode())
}
33 changes: 0 additions & 33 deletions cmd/device_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,11 @@
package cmd

import (
"context"
"fmt"
"github.com/inexio/thola/core/parser"
"github.com/inexio/thola/core/request"
"github.com/rs/xid"
"github.com/rs/zerolog/log"
flag "github.com/spf13/pflag"
"github.com/spf13/viper"
"os"
)

func addBinarySpecificDeviceFlags(fs *flag.FlagSet) {
fs.StringSlice("snmp-community", nil, "Community strings for SNMP to use")
fs.StringSlice("snmp-version", nil, "SNMP versions to use (1, 2c or 3)")
fs.IntSlice("snmp-port", nil, "Ports for SNMP to use")
}

func handleRequest(r request.Request) {
rid := xid.New().String()
logger := log.With().Str("request_id", rid).Logger()
ctx := logger.WithContext(request.NewContextWithRequestID(context.Background(), rid))

log.Ctx(ctx).Trace().Msg("sending request")

resp, err := request.ProcessRequest(ctx, r)
if err != nil {
handleError(ctx, err)
os.Exit(3)
}

log.Ctx(ctx).Trace().Msg("received response")

b, err := parser.Parse(resp, viper.GetString("format"))
if err != nil {
log.Ctx(ctx).Error().Err(err).Msg("Request successful, but failed to parse response")
os.Exit(3)
}

fmt.Printf("%s\n", b)
os.Exit(resp.GetExitCode())
}
48 changes: 0 additions & 48 deletions cmd/device_init.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
package cmd

import (
"context"
"fmt"
"github.com/inexio/thola/core/network"
"github.com/inexio/thola/core/parser"
"github.com/inexio/thola/core/request"
"github.com/inexio/thola/core/utility"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
Expand Down Expand Up @@ -176,45 +170,3 @@ func bindDeviceFlags(cmd *cobra.Command) error {
}
return nil
}

func getBaseRequest() request.BaseRequest {
var nullInt *int
var nullString *string
timeout := viper.GetInt("request.timeout")
parallelRequests := viper.GetInt("device.snmp-discover-par-requests")
discoverTimeout := viper.GetInt("device.snmp-discover-timeout")
retries := viper.GetInt("device.snmp-discover-retries")
authUsername := viper.GetString("device.http-username")
authPassword := viper.GetString("device.http-password")
return request.BaseRequest{
Timeout: utility.IfThenElse(deviceFlagSet.Changed("timeout"), &timeout, nullInt).(*int),
DeviceData: request.DeviceData{
IPAddress: viper.GetString("device.ip"),
ConnectionData: network.ConnectionData{
SNMP: &network.SNMPConnectionData{
Communities: utility.IfThenElse(deviceFlagSet.Changed("snmp-community"), viper.GetStringSlice("device.snmp-communities"), []string{}).([]string),
Versions: utility.IfThenElse(deviceFlagSet.Changed("snmp-version"), viper.GetStringSlice("device.snmp-versions"), []string{}).([]string),
Ports: utility.IfThenElse(deviceFlagSet.Changed("snmp-port"), viper.GetIntSlice("device.snmp-ports"), []int{}).([]int),
DiscoverParallelRequests: utility.IfThenElse(deviceFlagSet.Changed("snmp-discover-par-requests"), &parallelRequests, nullInt).(*int),
DiscoverTimeout: utility.IfThenElse(deviceFlagSet.Changed("snmp-discover-timeout"), &discoverTimeout, nullInt).(*int),
DiscoverRetries: utility.IfThenElse(deviceFlagSet.Changed("snmp-discover-retries"), &retries, nullInt).(*int),
},
HTTP: &network.HTTPConnectionData{
HTTPPorts: utility.IfThenElse(deviceFlagSet.Changed("http-port"), viper.GetIntSlice("device.http-ports"), []int{}).([]int),
HTTPSPorts: utility.IfThenElse(deviceFlagSet.Changed("https-port"), viper.GetIntSlice("device.https-ports"), []int{}).([]int),
AuthUsername: utility.IfThenElse(deviceFlagSet.Changed("http-username"), &authUsername, nullString).(*string),
AuthPassword: utility.IfThenElse(deviceFlagSet.Changed("http-password"), &authPassword, nullString).(*string),
},
},
},
}
}

func handleError(ctx context.Context, err error) {
b, err := parser.Parse(err, viper.GetString("format"))
if err != nil {
log.Ctx(ctx).Error().AnErr("parse_error", err).AnErr("original_error", err).Msg("failed to parse error")
} else {
fmt.Printf("%s\n", b)
}
}
41 changes: 40 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
package cmd

import (
"context"
"fmt"
"github.com/inexio/thola/core/database"
"github.com/inexio/thola/core/parser"
"github.com/inexio/thola/core/request"
"github.com/inexio/thola/doc"
"github.com/pkg/errors"
"github.com/rs/xid"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -178,7 +184,7 @@ var rootCMD = &cobra.Command{
},
Run: func(cmd *cobra.Command, args []string) {
if cmd.Flags().Lookup("version").Changed {
fmt.Println("v0.1.2")
fmt.Println(doc.Version)
} else {
fmt.Print(cmd.UsageString())
}
Expand All @@ -191,3 +197,36 @@ func Execute() {
os.Exit(1)
}
}

func handleRequest(r request.Request) {
logger := log.With().Str("request_id", xid.New().String()).Logger()
ctx := logger.WithContext(context.Background())

db, err := database.GetDB(ctx)
if err != nil {
handleError(ctx, err)
os.Exit(3)
}

resp, err := request.ProcessRequest(ctx, r)
if err != nil {
handleError(ctx, err)
_ = db.CloseConnection(ctx)
os.Exit(3)
}

err = db.CloseConnection(ctx)
if err != nil {
handleError(ctx, err)
os.Exit(3)
}

b, err := parser.Parse(resp, viper.GetString("format"))
if err != nil {
log.Ctx(ctx).Error().Err(err).Msg("Request successful, but failed to parse response")
os.Exit(3)
}

fmt.Printf("%s\n", b)
os.Exit(resp.GetExitCode())
}
33 changes: 32 additions & 1 deletion cmd/root_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
package cmd

import (
"context"
"fmt"
"github.com/inexio/thola/core/parser"
"github.com/inexio/thola/core/request"
"github.com/inexio/thola/doc"
"github.com/pkg/errors"
"github.com/rs/xid"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -120,15 +125,41 @@ var rootCMD = &cobra.Command{
},
Run: func(cmd *cobra.Command, args []string) {
if cmd.Flags().Lookup("version").Changed {
fmt.Println("v0.1.1")
fmt.Println(doc.Version)
} else {
fmt.Print(cmd.UsageString())
}
},
}

// Execute is the entrypoint for the CLI interface.
func Execute() {
if err := rootCMD.Execute(); err != nil {
os.Exit(1)
}
}

func handleRequest(r request.Request) {
rid := xid.New().String()
logger := log.With().Str("request_id", rid).Logger()
ctx := logger.WithContext(request.NewContextWithRequestID(context.Background(), rid))

log.Ctx(ctx).Trace().Msg("sending request")

resp, err := request.ProcessRequest(ctx, r)
if err != nil {
handleError(ctx, err)
os.Exit(3)
}

log.Ctx(ctx).Trace().Msg("received response")

b, err := parser.Parse(resp, viper.GetString("format"))
if err != nil {
log.Ctx(ctx).Error().Err(err).Msg("Request successful, but failed to parse response")
os.Exit(3)
}

fmt.Printf("%s\n", b)
os.Exit(resp.GetExitCode())
}
54 changes: 54 additions & 0 deletions cmd/shared.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package cmd

import (
"context"
"fmt"
"github.com/inexio/thola/core/network"
"github.com/inexio/thola/core/parser"
"github.com/inexio/thola/core/request"
"github.com/inexio/thola/core/utility"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
)

func getBaseRequest() request.BaseRequest {
var nullInt *int
var nullString *string
timeout := viper.GetInt("request.timeout")
parallelRequests := viper.GetInt("device.snmp-discover-par-requests")
discoverTimeout := viper.GetInt("device.snmp-discover-timeout")
retries := viper.GetInt("device.snmp-discover-retries")
authUsername := viper.GetString("device.http-username")
authPassword := viper.GetString("device.http-password")
return request.BaseRequest{
Timeout: utility.IfThenElse(deviceFlagSet.Changed("timeout"), &timeout, nullInt).(*int),
DeviceData: request.DeviceData{
IPAddress: viper.GetString("device.ip"),
ConnectionData: network.ConnectionData{
SNMP: &network.SNMPConnectionData{
Communities: utility.IfThenElse(deviceFlagSet.Changed("snmp-community"), viper.GetStringSlice("device.snmp-communities"), []string{}).([]string),
Versions: utility.IfThenElse(deviceFlagSet.Changed("snmp-version"), viper.GetStringSlice("device.snmp-versions"), []string{}).([]string),
Ports: utility.IfThenElse(deviceFlagSet.Changed("snmp-port"), viper.GetIntSlice("device.snmp-ports"), []int{}).([]int),
DiscoverParallelRequests: utility.IfThenElse(deviceFlagSet.Changed("snmp-discover-par-requests"), &parallelRequests, nullInt).(*int),
DiscoverTimeout: utility.IfThenElse(deviceFlagSet.Changed("snmp-discover-timeout"), &discoverTimeout, nullInt).(*int),
DiscoverRetries: utility.IfThenElse(deviceFlagSet.Changed("snmp-discover-retries"), &retries, nullInt).(*int),
},
HTTP: &network.HTTPConnectionData{
HTTPPorts: utility.IfThenElse(deviceFlagSet.Changed("http-port"), viper.GetIntSlice("device.http-ports"), []int{}).([]int),
HTTPSPorts: utility.IfThenElse(deviceFlagSet.Changed("https-port"), viper.GetIntSlice("device.https-ports"), []int{}).([]int),
AuthUsername: utility.IfThenElse(deviceFlagSet.Changed("http-username"), &authUsername, nullString).(*string),
AuthPassword: utility.IfThenElse(deviceFlagSet.Changed("http-password"), &authPassword, nullString).(*string),
},
},
},
}
}

func handleError(ctx context.Context, err error) {
b, err := parser.Parse(err, viper.GetString("format"))
if err != nil {
log.Ctx(ctx).Error().AnErr("parse_error", err).AnErr("original_error", err).Msg("failed to parse error")
} else {
fmt.Printf("%s\n", b)
}
}
3 changes: 2 additions & 1 deletion core/parser/human_readable_parser.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package parser

import (
"bytes"
"fmt"
"reflect"
"strconv"
Expand Down Expand Up @@ -86,5 +87,5 @@ func toHumanReadable(value reflect.Value, insertion int) []byte {
}
}

return outputString
return bytes.TrimSpace(outputString)
}
2 changes: 1 addition & 1 deletion core/parser/object_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func ToHumanReadable(i interface{}) ([]byte, error) {
if i == nil {
return []byte("null"), nil
}
return bytes.TrimSpace(toHumanReadable(reflect.ValueOf(i), 0)), nil
return toHumanReadable(reflect.ValueOf(i), 0), nil
}

// ToCheckPluginOutput parses the object to a check plugin format.
Expand Down
12 changes: 5 additions & 7 deletions core/request/client_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ package request
import (
"context"
"fmt"
"github.com/go-resty/resty/v2"
"github.com/inexio/go-monitoringplugin"
"github.com/inexio/thola/core/network"
"github.com/inexio/thola/core/parser"
"github.com/inexio/thola/core/tholaerr"
"github.com/labstack/echo"
"github.com/inexio/thola/doc"
"github.com/pkg/errors"
"github.com/spf13/viper"
"strings"
Expand Down Expand Up @@ -257,14 +256,13 @@ func sendToAPI(ctx context.Context, request Request, path, format string) ([]byt
return nil, errors.Wrapf(err, "failed to parse request to format '%s'", format)
}

var restyResponse *resty.Response
header := map[string]string{"User-Agent": "Thola Client " + doc.Version}
rid, ok := RequestIDFromContext(ctx)
if !ok {
restyResponse, err = client.Request(ctx, "POST", path, string(b), nil, nil)
} else {
restyResponse, err = client.Request(ctx, "POST", path, string(b), map[string]string{echo.HeaderXRequestID: rid}, nil)
if ok {
header["X-Request-ID"] = rid
}

restyResponse, err := client.Request(ctx, "POST", path, string(b), header, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to send request to api")
}
Expand Down
Loading

0 comments on commit e574ca4

Please sign in to comment.