Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(log): better log outputs when dispatching to downstreams #644

Merged
merged 7 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions core/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package core

import (
"github.com/yomorun/yomo/core/metadata"
"golang.org/x/exp/slog"
)

const (
Expand Down Expand Up @@ -68,17 +67,3 @@ func SetTracedToMetadata(m metadata.M, traced bool) {
}
m.Set(MetaTraced, tracedString)
}

// MetadataSlogAttr returns slog.Attr from metadata.
func MetadataSlogAttr(md metadata.M) slog.Attr {
kvStrings := make([]any, len(md)*2)
i := 0
for k, v := range md {
kvStrings[i] = k
i++
kvStrings[i] = v
i++
}

return slog.Group("metadata", kvStrings...)
}
27 changes: 0 additions & 27 deletions core/metadata_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package core

import (
"bytes"
"testing"

"github.com/stretchr/testify/assert"
"github.com/yomorun/yomo/core/metadata"
"golang.org/x/exp/slog"
)

func TestMetadata(t *testing.T) {
Expand All @@ -26,27 +23,3 @@ func TestMetadata(t *testing.T) {
SetTracedToMetadata(md, false)
assert.Equal(t, false, GetTracedFromMetadata(md))
}

func TestMetadataSlogAttr(t *testing.T) {
md := metadata.New(map[string]string{
"aaaa": "bbbb",
})

buf := bytes.NewBuffer(nil)

logger := slog.New(slog.NewTextHandler(buf, &slog.HandlerOptions{
AddSource: false,
Level: slog.LevelDebug,
// display time attr.
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
if a.Key == "time" {
return slog.Attr{}
}
return a
},
}))

logger.Debug("test metadata", MetadataSlogAttr(md))

assert.Equal(t, "level=DEBUG msg=\"test metadata\" metadata.aaaa=bbbb\n", buf.String())
}
6 changes: 5 additions & 1 deletion core/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,11 @@ func (s *Server) dispatchToDownstreams(c *Context) {
dataFrame.Metadata = mdBytes

for _, ds := range s.downstreams {
c.Logger.Info("dispatching to downstream", "tid", tid, "sid", sid, "tag", dataFrame.Tag, "data_length", len(dataFrame.Payload), "downstream_id", ds.ClientID())
c.Logger.Info(
"dispatching to downstream",
"tid", tid, "sid", sid, "tag", dataFrame.Tag, "data_length", len(dataFrame.Payload),
"downstream_id", ds.ClientID(), "downstream_name", ds.Name(),
)
_ = ds.WriteFrame(dataFrame)
}
}
Expand Down
6 changes: 5 additions & 1 deletion zipper.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ func NewZipper(name string, meshConfig map[string]config.Downstream, options ...
)
downstream := core.NewClient(name, core.ClientTypeUpstreamZipper, clientOptions...)

server.Logger().Info("add downstream", "name", downstreamName, "addr", addr, "downstream_id", downstream.ClientID())
server.Logger().Info(
"add downstream",
"name", downstreamName, "addr", addr,
woorui marked this conversation as resolved.
Show resolved Hide resolved
"downstream_id", downstream.ClientID(), "downstream_name", downstream.Name(),
woorui marked this conversation as resolved.
Show resolved Hide resolved
)
server.AddDownstreamServer(addr, downstream)
}

Expand Down