Skip to content

Commit

Permalink
Remove Logrus for log/slog (#82)
Browse files Browse the repository at this point in the history
* remove logrus in favor of slog
* use patched firecracker-sdk that will allow a non-logrus logger
* rework ui to use envs
  • Loading branch information
jordan-rash authored Jan 24, 2024
1 parent 91a1b0c commit a50a1dd
Show file tree
Hide file tree
Showing 26 changed files with 564 additions and 1,063 deletions.
15 changes: 8 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ require (
github.com/onsi/gomega v1.30.0
github.com/pkg/errors v0.9.1
github.com/rs/xid v1.5.0
github.com/sirupsen/logrus v1.9.3
github.com/tetratelabs/wazero v1.6.0
rogchap.com/v8go v0.9.0
)
Expand All @@ -35,21 +34,20 @@ require (
github.com/adrg/xdg v0.4.0 // indirect
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d // indirect
github.com/containerd/fifo v1.0.0 // indirect
github.com/containernetworking/cni v1.0.1 // indirect
github.com/containernetworking/plugins v1.0.1 // indirect
github.com/containernetworking/cni v1.1.2 // indirect
github.com/containernetworking/plugins v1.1.1 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-openapi/analysis v0.21.2 // indirect
github.com/go-openapi/errors v0.20.2 // indirect
github.com/go-openapi/errors v0.20.3 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.6 // indirect
github.com/go-openapi/loads v0.21.1 // indirect
github.com/go-openapi/runtime v0.24.0 // indirect
github.com/go-openapi/spec v0.20.4 // indirect
github.com/go-openapi/strfmt v0.21.2 // indirect
github.com/go-openapi/strfmt v0.21.3 // indirect
github.com/go-openapi/swag v0.21.1 // indirect
github.com/go-openapi/validate v0.22.0 // indirect
github.com/go-stack/stack v1.8.1 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/google/go-cmp v0.6.0 // indirect
Expand All @@ -70,10 +68,11 @@ require (
github.com/oklog/ulid v1.3.1 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/vektah/gqlparser/v2 v2.5.6 // indirect
github.com/vishvananda/netlink v1.1.1-0.20210330154013-f5de75959ad5 // indirect
github.com/vishvananda/netns v0.0.0-20210104183010-2eb08e3e575f // indirect
go.mongodb.org/mongo-driver v1.8.3 // indirect
go.mongodb.org/mongo-driver v1.10.0 // indirect
go.opencensus.io v0.22.3 // indirect
go.uber.org/atomic v1.4.0 // indirect
go.uber.org/multierr v1.1.0 // indirect
Expand All @@ -90,3 +89,5 @@ require (
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/firecracker-microvm/firecracker-go-sdk => github.com/jordan-rash/firecracker-go-sdk v0.0.0-20240124162534-a5295226c294
715 changes: 16 additions & 699 deletions go.sum

Large diffs are not rendered by default.

15 changes: 7 additions & 8 deletions internal/control-api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"context"
"encoding/json"
"fmt"
"log/slog"
"strings"
"time"

cloudevents "github.com/cloudevents/sdk-go"
"github.com/nats-io/nats.go"
"github.com/sirupsen/logrus"
)

// API subjects:
Expand All @@ -23,18 +23,18 @@ type Client struct {
nc *nats.Conn
timeout time.Duration
namespace string
log *logrus.Logger
log *slog.Logger
}

// Creates a new client to communicate with a group of NEX nodes, using the
// namespace of 'default' for applicable requests
func NewApiClient(nc *nats.Conn, timeout time.Duration, log *logrus.Logger) *Client {
func NewApiClient(nc *nats.Conn, timeout time.Duration, log *slog.Logger) *Client {
return NewApiClientWithNamespace(nc, timeout, "default", log)
}

// Creates a new client to communicate with a group of NEX nodes all within a given namespace. Note that
// this namespace is used for requests where it is mandatory
func NewApiClientWithNamespace(nc *nats.Conn, timeout time.Duration, namespace string, log *logrus.Logger) *Client {
func NewApiClientWithNamespace(nc *nats.Conn, timeout time.Duration, namespace string, log *slog.Logger) *Client {
return &Client{nc: nc, timeout: timeout, namespace: namespace, log: log}
}

Expand Down Expand Up @@ -226,17 +226,16 @@ func handleLogEntry(api *Client, ch chan EmittedLog) func(m *nats.Msg) {
*/
tokens := strings.Split(m.Subject, ".")
if len(tokens) != 6 {
api.log.Debug("token length not 6", "length", len(tokens), "subject", m.Subject)
return
}

var logEntry RawLog
err := json.Unmarshal(m.Data, &logEntry)
if err != nil {
api.log.WithError(err).Error("Log entry deserialization failure")
api.log.Error("Log entry deserialization failure", err)
return
}
if logEntry.Level == 0 {
logEntry.Level = logrus.DebugLevel
}

ch <- EmittedLog{
Namespace: tokens[2],
Expand Down
9 changes: 5 additions & 4 deletions internal/control-api/types.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package controlapi

import (
"log/slog"

cloudevents "github.com/cloudevents/sdk-go"
"github.com/sirupsen/logrus"
)

const (
Expand Down Expand Up @@ -81,9 +82,9 @@ type EmittedLog struct {
}

type RawLog struct {
Text string `json:"text"`
Level logrus.Level `json:"level"`
MachineId string `json:"machine_id"`
Text string `json:"text"`
Level slog.Level `json:"level"`
MachineId string `json:"machine_id"`
}

// Note this a wrapper to add context to a cloud event
Expand Down
4 changes: 4 additions & 0 deletions internal/models/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ type Options struct {
TlsFirst bool
// Namespace for scoping workload requests
Namespace string
// LogLevel is the log level to use
LogLevel string
// LogJSON enables JSON logging
LogJSON bool
}

type RunOptions struct {
Expand Down
22 changes: 11 additions & 11 deletions internal/node/agentcomms.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package nexnode
import (
"encoding/json"
"fmt"
"log/slog"
"strings"
"time"

cloudevents "github.com/cloudevents/sdk-go"
"github.com/nats-io/nats.go"
"github.com/sirupsen/logrus"
agentapi "github.com/synadia-io/nex/internal/agent-api"
)

Expand All @@ -28,19 +28,19 @@ func handleAgentLog(mgr *MachineManager) func(m *nats.Msg) {
var logentry agentapi.LogEntry
err := json.Unmarshal(m.Data, &logentry)
if err != nil {
mgr.log.WithError(err).Error("Failed to unmarshal log entry from agent")
mgr.log.Error("Failed to unmarshal log entry from agent", slog.Any("err", err))
return
}

outLog := emittedLog{
Text: logentry.Text,
Level: logrus.Level(logentry.Level),
Level: slog.Level(logentry.Level),
MachineId: vmId,
}

bytes, err := json.Marshal(outLog)
if err != nil {
mgr.log.WithError(err).Error("Failed to marshal our own log entry!")
mgr.log.Error("Failed to marshal our own log entry", slog.Any("err", err))
return
}

Expand All @@ -65,19 +65,19 @@ func handleAgentEvent(mgr *MachineManager) func(m *nats.Msg) {
var evt cloudevents.Event
err := json.Unmarshal(m.Data, &evt)
if err != nil {
mgr.log.WithError(err).Error("Failed to deserialize cloudevent from agent")
mgr.log.Error("Failed to deserialize cloudevent from agent", slog.Any("err", err))
return
}
mgr.log.WithField("vmid", vmId).WithField("type", evt.Type()).Info("Received agent event")
mgr.log.Info("Received agent event", slog.String("vmid", vmId), slog.String("type", evt.Type()))

err = mgr.PublishCloudEvent(vm.namespace, evt)
if err != nil {
mgr.log.WithError(err).Error("Failed to publish cloudevent")
mgr.log.Error("Failed to publish cloudevent", slog.Any("err", err))
return
}

if evt.Type() == agentapi.WorkloadStoppedEventType {
vm.shutDown()
vm.shutDown(mgr.log)
}
}
}
Expand All @@ -89,17 +89,17 @@ func handleHandshake(mgr *MachineManager) func(m *nats.Msg) {
var shake agentapi.HandshakeRequest
err := json.Unmarshal(m.Data, &shake)
if err != nil {
mgr.log.WithField("vmid", *shake.MachineId).WithField("message", *shake.Message).Error("Failed to handle agent handshake")
mgr.log.Error("Failed to handle agent handshake", slog.String("vmid", *shake.MachineId), slog.String("message", *shake.Message))
return
}

now := time.Now().UTC()
mgr.handshakes[*shake.MachineId] = now.Format(time.RFC3339)

mgr.log.WithField("vmid", *shake.MachineId).WithField("message", *shake.Message).Info("Received agent handshake")
mgr.log.Info("Received agent handshake", slog.String("vmid", *shake.MachineId), slog.String("message", *shake.Message))
err = m.Respond([]byte("OK"))
if err != nil {
mgr.log.WithError(err).Error("Failed to reply to agent handshake")
mgr.log.Error("Failed to reply to agent handshake", slog.Any("err", err))
}
}
}
Expand Down
Loading

0 comments on commit a50a1dd

Please sign in to comment.