Skip to content

Commit

Permalink
Merge pull request #49 from haoming29/univ-log
Browse files Browse the repository at this point in the history
Accept a logger as the universal logger in the package
  • Loading branch information
djw8605 authored Jan 23, 2024
2 parents 8128c83 + 0642582 commit 81fb94d
Show file tree
Hide file tree
Showing 19 changed files with 148 additions and 2,482 deletions.
23 changes: 23 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work

dist/
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.17-buster
FROM golang:1.20-bullseye

EXPOSE 9993
EXPOSE 8000
Expand Down
4 changes: 1 addition & 3 deletions amqp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package shoveler

import (
"errors"
"io/ioutil"
"math/rand"
"net/url"
"os"
"strings"
"time"

log "github.com/sirupsen/logrus"
"github.com/streadway/amqp"
)

Expand Down Expand Up @@ -139,7 +137,7 @@ func readMsg(messagesQueue chan<- []byte, queue *ConfirmationQueue) {
func readToken(tokenLocation string) (string, error) {
// Get the token password
// Read in the token file
tokenContents, err := ioutil.ReadFile(tokenLocation)
tokenContents, err := os.ReadFile(tokenLocation)
if err != nil {
log.Errorln("Unable to read file:", tokenLocation)
return "", err
Expand Down
3 changes: 1 addition & 2 deletions cmd/createtoken/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/pem"
"flag"
"fmt"
"io/ioutil"
"os"
"time"

Expand All @@ -25,7 +24,7 @@ func main() {
}

// Read in the private key
pemString, err := ioutil.ReadFile(flag.Args()[0])
pemString, err := os.ReadFile(flag.Args()[0])
if err != nil {
fmt.Println("Failed to read in private key:", os.Args[1], ":", err)
os.Exit(1)
Expand Down
37 changes: 22 additions & 15 deletions cmd/shoveler-status/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ package main
import (
_ "embed"
"errors"
"github.com/golang-jwt/jwt/v4"
"github.com/jessevdk/go-flags"
shoveler "github.com/opensciencegrid/xrootd-monitoring-shoveler"
"github.com/pterm/pterm"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"io"
"math/big"
"net/http"
"os"
"strconv"
"strings"
"time"

"github.com/golang-jwt/jwt/v4"
"github.com/jessevdk/go-flags"
shoveler "github.com/opensciencegrid/xrootd-monitoring-shoveler"
"github.com/pterm/pterm"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)

//go:embed shoveler-public.pem
Expand All @@ -28,6 +29,8 @@ var (
builtBy string
)

var logger *logrus.Logger

type Options struct {
Verbose []bool `short:"v" long:"verbose" description:"Show verbose debug information"`
Version bool `short:"V" long:"version" description:"Print version information"`
Expand All @@ -52,14 +55,17 @@ func main() {
shoveler.ShovelerDate = date
shoveler.ShovelerBuiltBy = builtBy

logger := logrus.New()
shoveler.SetLogger(logger)

// Parse flags from `args'. Note that here we use flags.ParseArgs for
// the sake of making a working example. Normally, you would simply use
// flags.Parse(&opts) which uses os.Args
if _, err := parser.Parse(); err != nil {
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
os.Exit(0)
} else {
log.Errorln(err)
logger.Errorln(err)
os.Exit(1)
}
}
Expand All @@ -69,27 +75,28 @@ func main() {
// Load the configuration
config := shoveler.Config{}
config.ReadConfig()

if len(options.Verbose) > 0 {
log.SetLevel(log.DebugLevel)
logger.SetLevel(logrus.DebugLevel)
viper.Debug()
} else {
log.SetLevel(log.WarnLevel)
logger.SetLevel(logrus.WarnLevel)
}
log.Debugln("Using configuration file:", viper.ConfigFileUsed())
logger.Debugln("Using configuration file:", viper.ConfigFileUsed())
spinnerConfig.Success()

CheckToken(config)

// Try to connect to the prometheus endpoint
if !config.Metrics {
pterm.Error.Println("Metrics are disabled in the configuration file")
log.Errorln("Metrics are disabled in the configuration file, unable to determine if shoveler is running")
logger.Errorln("Metrics are disabled in the configuration file, unable to determine if shoveler is running")
}
// Try downloading the metrics page
initialStats, err := CheckPrometheusEndpoint(config.MetricsPort)
if err != nil {
//pterm.Error.Println("Unable to connect to the shoveler metrics endpoint")
log.Errorln("Unable to connect to the shoveler metrics endpoint, unable to determine if shoveler is running", err)
logger.Errorln("Unable to connect to the shoveler metrics endpoint, unable to determine if shoveler is running", err)
os.Exit(1)
}

Expand All @@ -116,7 +123,7 @@ func main() {
secondStats, err := CheckPrometheusEndpoint(config.MetricsPort)
if err != nil {
spinnerPeriod.Fail("Unable to connect to the shoveler metrics endpoint: ", err)
//log.Errorln("Unable to connect to the shoveler metrics endpoint, unable to determine if shoveler is running", err)
//logger.Errorln("Unable to connect to the shoveler metrics endpoint, unable to determine if shoveler is running", err)
os.Exit(1)
}

Expand Down Expand Up @@ -167,7 +174,7 @@ func CheckToken(config shoveler.Config) {
token, err := parser.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
pubKey, err := jwt.ParseRSAPublicKeyFromPEM(publicKey)
if err != nil {
log.Errorln("Unable to parse the public key:", err)
logger.Errorln("Unable to parse the public key:", err)
return nil, err
}
return pubKey, nil
Expand Down Expand Up @@ -239,7 +246,7 @@ func CheckPrometheusEndpoint(metricsPort int) (ShovelerStats, error) {
func parsePrometheusMetric(line string) int64 {
flt, _, err := big.ParseFloat(strings.Split(line, " ")[1], 10, 0, big.ToNearestEven)
if err != nil {
log.Errorln("Unable to parse prometheus metric", line, ":", err)
logger.Errorln("Unable to parse prometheus metric", line, ":", err)
return 0
}
int, _ := flt.Int64()
Expand Down
42 changes: 23 additions & 19 deletions cmd/shoveler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"net"

shoveler "github.com/opensciencegrid/xrootd-monitoring-shoveler"
log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
)

var (
Expand All @@ -22,25 +22,29 @@ func main() {
shoveler.ShovelerDate = date
shoveler.ShovelerBuiltBy = builtBy

logger := logrus.New()
textFormatter := logrus.TextFormatter{}
textFormatter.DisableLevelTruncation = true
textFormatter.FullTimestamp = true
logrus.SetFormatter(&textFormatter)

shoveler.SetLogger(logger)

// Load the configuration
config := shoveler.Config{}
config.ReadConfig()
if DEBUG || config.Debug {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.WarnLevel)
}

// Configure the mapper
shoveler.ConfigureMap()

textFormatter := log.TextFormatter{}
textFormatter.DisableLevelTruncation = true
textFormatter.FullTimestamp = true
log.SetFormatter(&textFormatter)
if DEBUG || config.Debug {
logger.SetLevel(logrus.DebugLevel)
} else {
logger.SetLevel(logrus.WarnLevel)
}

// Log the version information
log.Infoln("Starting xrootd-monitoring-shoveler", version, "commit:", commit, "built on:", date, "built by:", builtBy)
logrus.Infoln("Starting xrootd-monitoring-shoveler", version, "commit:", commit, "built on:", date, "built by:", builtBy)

// Start the message queue
cq := shoveler.NewConfirmationQueue()
Expand All @@ -64,7 +68,7 @@ func main() {
IP: net.ParseIP(config.ListenIp),
}
conn, err := net.ListenUDP("udp", &addr)
log.Debugln("Listening for UDP messages at:", addr.String())
logger.Debugln("Listening for UDP messages at:", addr.String())

if err != nil {
panic(err)
Expand All @@ -74,13 +78,13 @@ func main() {
err = conn.SetReadBuffer(1024 * 1024)

if err != nil {
log.Warningln("Failed to set read buffer size to 1 MB:", err)
logger.Warningln("Failed to set read buffer size to 1 MB:", err)
}

defer func(conn *net.UDPConn) {
err := conn.Close()
if err != nil {
log.Errorln("Error closing UDP connection:", err)
logger.Errorln("Error closing UDP connection:", err)
}
}(conn)

Expand All @@ -90,10 +94,10 @@ func main() {
for _, dest := range config.DestUdp {
udpConn, err := net.Dial("udp", dest)
if err != nil {
log.Warningln("Unable to parse destination:", dest, "Will not forward UDP packets to this destination:", err)
logger.Warningln("Unable to parse destination:", dest, "Will not forward UDP packets to this destination:", err)
}
udpDestinations = append(udpDestinations, udpConn)
log.Infoln("Adding udp forward destination:", dest)
logger.Infoln("Adding udp forward destination:", dest)
}
}

Expand All @@ -103,7 +107,7 @@ func main() {
// Do stuff with the read bytes
if err != nil {
// output errors
log.Errorln("Failed to read from UDP connection:", err)
logger.Errorln("Failed to read from UDP connection:", err)
// If we failed to read from the UDP connection, I'm not
// sure what to do, maybe just continue as if nothing happened?
continue
Expand All @@ -118,15 +122,15 @@ func main() {
msg := shoveler.PackageUdp(buf[:rlen], remote)

// Send the message to the queue
log.Debugln("Sending msg:", string(msg))
logger.Debugln("Sending msg:", string(msg))
cq.Enqueue(msg)

// Send to the UDP destinations
if len(udpDestinations) > 0 {
for _, udpConn := range udpDestinations {
_, err := udpConn.Write(msg)
if err != nil {
log.Errorln("Failed to send message to UDP destination "+udpConn.RemoteAddr().String()+":", err)
logger.Errorln("Failed to send message to UDP destination "+udpConn.RemoteAddr().String()+":", err)
}
}
}
Expand Down
1 change: 0 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net/url"
"strings"

log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)

Expand Down
45 changes: 44 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/opensciencegrid/xrootd-monitoring-shoveler

go 1.16
go 1.20

require (
github.com/go-stomp/stomp/v3 v3.0.3
Expand All @@ -15,3 +15,46 @@ require (
github.com/streadway/amqp v1.0.0
github.com/stretchr/testify v1.8.4
)

require (
atomicgo.dev/cursor v0.1.1 // indirect
atomicgo.dev/keyboard v0.2.8 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gofrs/flock v0.7.1 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/gookit/color v1.5.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/lithammer/fuzzysearch v1.1.5 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 81fb94d

Please sign in to comment.