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

chore(deps): Update github.com/gwos/tcg/sdk from v8.7.2 to v8.8.0 #15947

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ require (
github.com/gorilla/websocket v1.5.3
github.com/gosnmp/gosnmp v1.37.0
github.com/grid-x/modbus v0.0.0-20240503115206-582f2ab60a18
github.com/gwos/tcg/sdk v0.0.0-20231124052037-1e832b843240
github.com/gwos/tcg/sdk v0.0.0-20240830123415-f8a34bba6358
github.com/harlow/kinesis-consumer v0.3.6-0.20240916192723-43900507c911
github.com/hashicorp/consul/api v1.29.2
github.com/hashicorp/go-uuid v1.0.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1486,8 +1486,8 @@ github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 h1:bkypFPDjIYGfCYD5mRBvpqxfYX1
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0/go.mod h1:P+Lt/0by1T8bfcF3z737NnSbmxQAppXMRziHUxPOC8k=
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c h1:6rhixN/i8ZofjG1Y75iExal34USq5p+wiN1tpie8IrU=
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c/go.mod h1:NMPJylDgVpX0MLRlPy15sqSwOFv/U1GZ2m21JhFfek0=
github.com/gwos/tcg/sdk v0.0.0-20231124052037-1e832b843240 h1:dQUb3aqhbE1Z/QximDiPfBbTkq90Pf6p2f4k5M2puus=
github.com/gwos/tcg/sdk v0.0.0-20231124052037-1e832b843240/go.mod h1:H3CAtDtRLVPIkShWzarGiKYVZqrBtWNJMMRtfqJ3rXI=
github.com/gwos/tcg/sdk v0.0.0-20240830123415-f8a34bba6358 h1:QmKzhYk6KMjUutu9Sy4DyOkRgj1Dv+iFnea4t8KrCZg=
github.com/gwos/tcg/sdk v0.0.0-20240830123415-f8a34bba6358/go.mod h1:h40FJV0HuULqXSSKf7kfCbOxEcQAD74a5e2LC2+rYiQ=
github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed h1:5upAirOpQc1Q53c0bnx2ufif5kANL7bfZWcc6VJWJd8=
github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed/go.mod h1:tMWxXQ9wFIaZeTI9F+hmhFiGpFmhOHzyShyFUhRm0H4=
github.com/harlow/kinesis-consumer v0.3.6-0.20240916192723-43900507c911 h1:eLNkr0OcBl7pzM6DCLSgVp3VQyS5ZrLnanXPqH5EmE0=
Expand Down
77 changes: 11 additions & 66 deletions plugins/outputs/groundwork/groundwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
package groundwork

import (
"bytes"
"context"
_ "embed"
"encoding/json"
Expand All @@ -12,13 +11,14 @@ import (
"strings"

"github.com/gwos/tcg/sdk/clients"
"github.com/gwos/tcg/sdk/logper"
sdkLog "github.com/gwos/tcg/sdk/log"
srebhan marked this conversation as resolved.
Show resolved Hide resolved
"github.com/gwos/tcg/sdk/transit"
"github.com/hashicorp/go-uuid"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/plugins/outputs"
logAdapter "github.com/influxdata/telegraf/plugins/outputs/groundwork/slog"
srebhan marked this conversation as resolved.
Show resolved Hide resolved
)

//go:embed sample.conf
Expand Down Expand Up @@ -95,36 +95,24 @@ func (g *Groundwork) Init() error {
username.Destroy()
password.Destroy()

logper.SetLogger(
func(fields interface{}, format string, a ...interface{}) {
g.Log.Error(adaptLog(fields, format, a...))
},
func(fields interface{}, format string, a ...interface{}) {
g.Log.Warn(adaptLog(fields, format, a...))
},
func(fields interface{}, format string, a ...interface{}) {
g.Log.Info(adaptLog(fields, format, a...))
},
func(fields interface{}, format string, a ...interface{}) {
g.Log.Debug(adaptLog(fields, format, a...))
},
func() bool { return g.Log.Level() >= telegraf.Debug },
)
/* adapt SDK logger */
sdkLog.Logger = logAdapter.NewLogger(g.Log).WithGroup("tcg.sdk")

return nil
}

func (g *Groundwork) Connect() error {
err := g.client.Connect()
if err != nil {
return fmt.Errorf("could not log in: %w", err)
return fmt.Errorf("could not login: %w", err)
}
return nil
}

func (g *Groundwork) Close() error {
err := g.client.Disconnect()
if err != nil {
return fmt.Errorf("could not log out: %w", err)
return fmt.Errorf("could not logout: %w", err)
}
return nil
}
Expand Down Expand Up @@ -346,15 +334,15 @@ func (g *Groundwork) parseMetric(metric telegraf.Metric) (metricMeta, *transit.M
}

if m, ok := metric.GetTag("message"); ok {
serviceObject.LastPluginOutput = m
serviceObject.LastPluginOutput = strings.ToValidUTF8(m, "?")
} else if m, ok := metric.GetField("message"); ok {
switch m := m.(type) {
case string:
serviceObject.LastPluginOutput = m
serviceObject.LastPluginOutput = strings.ToValidUTF8(m, "?")
case []byte:
serviceObject.LastPluginOutput = string(m)
serviceObject.LastPluginOutput = strings.ToValidUTF8(string(m), "?")
default:
serviceObject.LastPluginOutput = fmt.Sprintf("%v", m)
serviceObject.LastPluginOutput = strings.ToValidUTF8(fmt.Sprintf("%v", m), "?")
}
}

Expand Down Expand Up @@ -395,46 +383,3 @@ func validStatus(status string) bool {
}
return false
}

func adaptLog(fields interface{}, format string, a ...interface{}) string {
buf := &bytes.Buffer{}
if format != "" {
fmt.Fprintf(buf, format, a...)
}
fmtField := func(k string, v interface{}) {
format := " %s:"
if len(k) == 0 {
format = " "
}
if _, ok := v.(int); ok {
format += "%d"
} else {
format += "%q"
}
fmt.Fprintf(buf, format, k, v)
}
if ff, ok := fields.(interface {
LogFields() (map[string]interface{}, map[string][]byte)
}); ok {
m1, m2 := ff.LogFields()
for k, v := range m1 {
fmtField(k, v)
}
for k, v := range m2 {
fmtField(k, v)
}
} else if ff, ok := fields.(map[string]interface{}); ok {
for k, v := range ff {
fmtField(k, v)
}
} else if ff, ok := fields.([]interface{}); ok {
for _, v := range ff {
fmtField("", v)
}
}
out := buf.Bytes()
if len(out) > 1 {
out = append(bytes.ToUpper(out[0:1]), out[1:]...)
}
return string(out)
}
19 changes: 16 additions & 3 deletions plugins/outputs/groundwork/groundwork_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ import (
"testing"

"github.com/gwos/tcg/sdk/clients"
sdkLog "github.com/gwos/tcg/sdk/log"
"github.com/gwos/tcg/sdk/transit"
"github.com/stretchr/testify/require"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/logger"
logAdapter "github.com/influxdata/telegraf/plugins/outputs/groundwork/slog"
srebhan marked this conversation as resolved.
Show resolved Hide resolved
"github.com/influxdata/telegraf/testutil"
)

Expand All @@ -23,6 +26,16 @@ const (
customAppType = "SYSLOG"
)

var testLogger = func() telegraf.Logger {
if err := logger.SetupLogging(&logger.Config{Debug: true}); err != nil {
return nil
}
l := new(testutil.Logger)
sdkLog.Logger = logAdapter.NewLogger(l).WithGroup("tcg.sdk")

return l
}()

srebhan marked this conversation as resolved.
Show resolved Hide resolved
func TestWriteWithDefaults(t *testing.T) {
// Generate test metric with default name to test Write logic
intMetric := testutil.TestMetric(42, "IntMetric")
Expand Down Expand Up @@ -51,7 +64,7 @@ func TestWriteWithDefaults(t *testing.T) {
}))

i := Groundwork{
Log: testutil.Logger{},
Log: testLogger,
Server: server.URL,
AgentID: defaultTestAgentID,
DefaultHost: defaultHost,
Expand Down Expand Up @@ -101,7 +114,7 @@ func TestWriteWithFields(t *testing.T) {
}))

i := Groundwork{
Log: testutil.Logger{},
Log: testLogger,
Server: server.URL,
AgentID: defaultTestAgentID,
DefaultHost: defaultHost,
Expand Down Expand Up @@ -170,7 +183,7 @@ func TestWriteWithTags(t *testing.T) {
}))

i := Groundwork{
Log: testutil.Logger{},
Log: testLogger,
Server: server.URL,
AgentID: defaultTestAgentID,
DefaultHost: defaultHost,
Expand Down
111 changes: 111 additions & 0 deletions plugins/outputs/groundwork/slog/slog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package slog

import (
"context"
"encoding/json"
"log/slog"
"strings"
"sync"

"github.com/influxdata/telegraf"
)

// NewLogger creates telegraf.Logger adapter for slog.Logger
func NewLogger(l telegraf.Logger) *slog.Logger {
return slog.New(&TlgHandler{Log: l})
}

// TlgHandler translates slog.Record into telegraf.Logger call
// inspired by https://github.com/golang/example/blob/master/slog-handler-guide/README.md
type TlgHandler struct {
attrs []slog.Attr
groups []string

once sync.Once

GroupsFieldName string
MessageFieldName string

Log telegraf.Logger
}

func (h *TlgHandler) Enabled(_ context.Context, level slog.Level) bool {
l := h.Log.Level()
switch level {
case slog.LevelDebug:
return l >= telegraf.Debug
case slog.LevelInfo:
return l >= telegraf.Info
case slog.LevelWarn:
return l >= telegraf.Warn
case slog.LevelError:
return l >= telegraf.Error
default:
return l >= telegraf.Info
}
}

func (h *TlgHandler) Handle(_ context.Context, r slog.Record) error {
h.once.Do(func() {
if h.GroupsFieldName == "" {
h.GroupsFieldName = "logger"
}
if h.MessageFieldName == "" {
h.MessageFieldName = "message"
}
})
srebhan marked this conversation as resolved.
Show resolved Hide resolved

attrs := make([]slog.Attr, 0, 2+len(h.attrs)+r.NumAttrs())
attrs = append(attrs,
slog.String(h.MessageFieldName, r.Message),
slog.String(h.GroupsFieldName, strings.Join(h.groups, ",")),
)
for _, attr := range h.attrs {
if v, ok := attr.Value.Any().(json.RawMessage); ok {
attrs = append(attrs, slog.String(attr.Key, string(v)))
continue
}
attrs = append(attrs, attr)
}
r.Attrs(func(attr slog.Attr) bool {
if v, ok := attr.Value.Any().(json.RawMessage); ok {
attrs = append(attrs, slog.String(attr.Key, string(v)))
return true
}
attrs = append(attrs, attr)
return true
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need to construct an array of attributes instead of directly constructing the message for logging?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we just preparing log data: adjusting datatype for json.RawMessage for better formatting. Then we letting actual logger make his work.
In output we getting well-structured message.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I was wondering why you don't directly format it into []string instead of keeping them as []slog.Attr...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are non-string Attrs. Convert them here to string, then actual writer will convert it to bytes.. does it make sense? Onetime conversion in writer should be better.


var handle func(args ...interface{})
switch r.Level {
case slog.LevelDebug:
handle = h.Log.Debug
case slog.LevelInfo:
handle = h.Log.Info
case slog.LevelWarn:
handle = h.Log.Warn
case slog.LevelError:
handle = h.Log.Error
default:
handle = h.Log.Info
}
handle(attrs)
srebhan marked this conversation as resolved.
Show resolved Hide resolved

return nil
}

func (h *TlgHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
nested := &TlgHandler{GroupsFieldName: h.GroupsFieldName, MessageFieldName: h.MessageFieldName, Log: h.Log}
nested.attrs = append(nested.attrs, h.attrs...)
nested.groups = append(nested.groups, h.groups...)
nested.attrs = append(nested.attrs, attrs...)
return nested
}

func (h *TlgHandler) WithGroup(name string) slog.Handler {
nested := &TlgHandler{GroupsFieldName: h.GroupsFieldName, MessageFieldName: h.MessageFieldName, Log: h.Log}
nested.attrs = append(nested.attrs, h.attrs...)
nested.groups = append(nested.groups, h.groups...)
nested.groups = append(nested.groups, name)
return nested
}
srebhan marked this conversation as resolved.
Show resolved Hide resolved
Loading