Skip to content

Commit

Permalink
feat: add support for static extra fields for JSON logs
Browse files Browse the repository at this point in the history
Fixes #7356

Signed-off-by: Andrey Smirnov <andrey.smirnov@siderolabs.com>
  • Loading branch information
smira committed Apr 2, 2024
1 parent 090143b commit 117e605
Show file tree
Hide file tree
Showing 14 changed files with 492 additions and 17 deletions.
16 changes: 16 additions & 0 deletions hack/release.toml
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,22 @@ kind: WatchdogTimerConfig
device: /dev/watchdog0
timeout: 3m0s
```
"""

[notes.logging]
title = "Logging"
description = """\
Talos Linux now supports setting extra tags when sending logs in JSON format:
```yaml
machine:
logging:
destinations:
- endpoint: "udp://127.0.0.1:12345/"
format: "json_lines"
extraTags:
server: s03-rack07
```
"""

[make_deps]
Expand Down
23 changes: 22 additions & 1 deletion internal/app/machined/pkg/controllers/runtime/kmsg_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
networkutils "github.com/siderolabs/talos/internal/app/machined/pkg/controllers/network/utils"
machinedruntime "github.com/siderolabs/talos/internal/app/machined/pkg/runtime"
"github.com/siderolabs/talos/internal/app/machined/pkg/runtime/logging"
"github.com/siderolabs/talos/pkg/machinery/config/config"
"github.com/siderolabs/talos/pkg/machinery/constants"
"github.com/siderolabs/talos/pkg/machinery/resources/network"
"github.com/siderolabs/talos/pkg/machinery/resources/runtime"
)
Expand Down Expand Up @@ -110,14 +112,33 @@ func (ctrl *KmsgLogDeliveryController) Run(ctx context.Context, r controller.Run
}
}

type logConfig struct {
endpoint *url.URL
}

func (c logConfig) Format() string {
return constants.LoggingFormatJSONLines
}

func (c logConfig) Endpoint() *url.URL {
return c.endpoint
}

func (c logConfig) ExtraTags() map[string]string {
return nil
}

//nolint:gocyclo
func (ctrl *KmsgLogDeliveryController) deliverLogs(ctx context.Context, r controller.Runtime, logger *zap.Logger, kmsgCh <-chan kmsg.Packet, destURLs []*url.URL) error {
if ctrl.drainSub == nil {
ctrl.drainSub = ctrl.Drainer.Subscribe()
}

// initialize all log senders
senders := xslices.Map(destURLs, logging.NewJSONLines)
destLogConfigs := xslices.Map(destURLs, func(u *url.URL) config.LoggingDestination {
return logConfig{endpoint: u}
})
senders := xslices.Map(destLogConfigs, logging.NewJSONLines)

defer func() {
closeCtx, closeCtxCancel := context.WithTimeout(context.Background(), logCloseTimeout)
Expand Down
16 changes: 12 additions & 4 deletions internal/app/machined/pkg/runtime/logging/sender_jsonlines.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,28 @@ import (
"time"

"github.com/siderolabs/talos/internal/app/machined/pkg/runtime"
"github.com/siderolabs/talos/pkg/machinery/config/config"
)

type jsonLinesSender struct {
endpoint *url.URL
endpoint *url.URL
extraTags map[string]string

sema chan struct{}
conn net.Conn
}

// NewJSONLines returns log sender that sends logs in JSON over TCP (newline-delimited)
// or UDP (one message per packet).
func NewJSONLines(endpoint *url.URL) runtime.LogSender {
func NewJSONLines(cfg config.LoggingDestination) runtime.LogSender {
sema := make(chan struct{}, 1)
sema <- struct{}{}

return &jsonLinesSender{
endpoint: endpoint,
sema: sema,
endpoint: cfg.Endpoint(),
extraTags: cfg.ExtraTags(),

sema: sema,
}
}

Expand All @@ -55,6 +59,10 @@ func (j *jsonLinesSender) marshalJSON(e *runtime.LogEvent) ([]byte, error) {
m["talos-time"] = e.Time.Format(time.RFC3339Nano)
m["talos-level"] = e.Level.String()

for k, v := range j.extraTags {
m[k] = v
}

return json.Marshal(m)
}

Expand Down
Loading

0 comments on commit 117e605

Please sign in to comment.