mirror of
https://github.com/tailscale/tailscale.git
synced 2026-07-29 08:46:33 -04:00
Go 1.27 enables GOEXPERIMENT=jsonv2 by default: encoding/json is now
backed by the json/v2 machinery, and github.com/go-json-experiment/json
compiles as a thin alias of the standard library's encoding/json/v2.
Several tag options and behaviors we relied on did not make the cut for
the final Go 1.27 API, breaking tailscaled at runtime and four packages'
tests. This change adapts to the final API while keeping the wire format
byte-for-byte identical on all Go versions.
First, the `format` tag option was demoted to experimental. Its mere
presence in a struct tag now makes marshaling and unmarshaling fail at
runtime. tailcfg.SSHAction.SessionDuration had `format:nano` (added in
a2dc517d7 to pin the v1 representation), so on Go 1.27 any netmap
containing an SSH policy failed to decode, breaking every PollNetMap.
Remove the option here and in net/speedtest; time.Duration still
marshals as int64 nanoseconds under encoding/json on all Go versions
(Go 1.27's v1 mode sets FormatDurationAsNano by default), so old
clients and servers are unaffected. Add a regression test locking in
the exact wire format.
Consequently, invert the cmd/vet jsontags rule: it previously required
an explicit `format` tag on time.Duration fields, which is now exactly
wrong. It now rejects any `format` tag option, which would have caught
this bug in CI.
Second, the `inline` tag option was renamed to `embed`. The standard
library silently ignores `inline`, while the pinned go-json-experiment
module (used on Go 1.26) only knows `inline`. Specify both options in
types/prefs and logtail; each implementation ignores the option it does
not know, producing identical output. Drop `inline` once we require
Go 1.27.
Third, encoding/json (v1) now dispatches to MarshalJSONTo and
UnmarshalJSONFrom methods and its v1 options flow into nested
jsonv2.MarshalEncode calls. Types whose v1 methods deliberately
routed through jsonv2 for v2 semantics (types/opt.Value, the
types/prefs preference types) would silently change wire format
(e.g. nil slices becoming null). Pin jsonv2.DefaultOptionsV2 in
their jsonv2 methods so the representation is the same regardless
of the entry point.
Finally, json.Marshal costs one more allocation under Go 1.27,
tripping the types/logger.AsJSON alloc test. Switch its fmt.Formatter
to jsonv2.MarshalWrite with explicit v1 options, which writes directly
to the fmt.State: one allocation on both toolchains with unchanged
output. Depaware files pick up the go-json-experiment/json/v1 options
shim as a new dependency of types/logger.
With this change, go test ./... passes with both Go 1.26.5 and
go1.27rc2.
Updates #20220
Fixes #20528
Fixes #20254
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
Change-Id: I694c7d57fd81e55a579c579e9be10032bca569d4
64 lines
1.4 KiB
Go
64 lines
1.4 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
//go:build ts_omit_logtail
|
|
|
|
package logtail
|
|
|
|
import (
|
|
"context"
|
|
"iter"
|
|
"time"
|
|
|
|
tslogger "tailscale.com/types/logger"
|
|
"tailscale.com/types/logid"
|
|
)
|
|
|
|
// Noop implementations of everything when ts_omit_logtail is set.
|
|
|
|
type Logger struct{}
|
|
|
|
type Buffer any
|
|
|
|
type Logtail struct {
|
|
ClientTime time.Time `json:"client_time,omitzero"`
|
|
ProcID uint32 `json:"proc_id,omitzero"`
|
|
ProcSeq uint64 `json:"proc_seq,omitzero"`
|
|
}
|
|
|
|
type LogEntry[T any] struct {
|
|
Logtail Logtail `json:"logtail,omitzero"`
|
|
Value T `json:",inline,embed"` // both options; see the non-omit variant in logtail.go
|
|
}
|
|
|
|
func UploadLogs[T any](ctx context.Context, conf Config, entries iter.Seq[LogEntry[T]]) error {
|
|
return nil
|
|
}
|
|
|
|
func Disable() {}
|
|
|
|
func (*Logger) SetEnabled(enabled bool) {}
|
|
|
|
func NewLogger(cfg Config, logf tslogger.Logf) *Logger {
|
|
return &Logger{}
|
|
}
|
|
|
|
func (*Logger) Write(p []byte) (n int, err error) {
|
|
return len(p), nil
|
|
}
|
|
|
|
func (*Logger) Logf(format string, args ...any) {}
|
|
func (*Logger) Shutdown(ctx context.Context) error { return nil }
|
|
func (*Logger) SetVerbosityLevel(level int) {}
|
|
|
|
func (l *Logger) SetSockstatsLabel(label any) {}
|
|
|
|
func (l *Logger) PrivateID() logid.PrivateID { return logid.PrivateID{} }
|
|
func (l *Logger) StartFlush() {}
|
|
|
|
func RegisterLogTap(dst chan<- string) (unregister func()) {
|
|
return func() {}
|
|
}
|
|
|
|
func (*Logger) SetNetMon(any) {}
|