Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
cmd: add --log-format alongside --log-level
Tomas Tormo3a727da69282
diff --git a/cmd/agentmesh-control-plane/main.go b/cmd/agentmesh-control-plane/main.goindex 7d0893a8..d9224fa9 100644--- a/cmd/agentmesh-control-plane/main.go+++ b/cmd/agentmesh-control-plane/main.go@@ -57,6 +57,7 @@ var (stsIssuerURL stringinsecureSkipTLSVerify boollogLevel string+ logFormat stringautoApproveEnrollment bool)@@ -79,7 +80,7 @@ func main() {return nil},Run: func(cmd *cobra.Command, args []string) {- logging.Setup(logLevel, "")+ logging.Setup(logging.Options{Level: logLevel, Format: logFormat})if oidcIssuer == "" && workloadIssuer == "" {logger.Fatalf("OIDC issuer is required (use --issuer or --workload-issuer flag)")@@ -201,6 +202,7 @@ func main() {rootCmd.Flags().StringVar(&stsIssuerURL, "sts-issuer-url", "", "Canonical external URL of this control plane for OIDC/STS issuer and discovery metadata (or env AGENTMESH_STS_ISSUER_URL)")rootCmd.Flags().BoolVar(&insecureSkipTLSVerify, "insecure-skip-tls-verify", false, "Skip TLS verification for OIDC providers")rootCmd.Flags().StringVar(&logLevel, "log-level", "info", "Log level (debug, info, warn, error)")+ rootCmd.Flags().StringVar(&logFormat, "log-format", "", "Log format (text, json); when unset, the LOG_FORMAT env var decides, else text")rootCmd.Flags().BoolVar(&autoApproveEnrollment, "auto-approve-enrollment", false, "Auto-approve valid bootstrap token enrollment requests")adminCmd := &cobra.Command{diff --git a/cmd/agentmesh-node/main.go b/cmd/agentmesh-node/main.goindex 2569dd43..71505822 100644--- a/cmd/agentmesh-node/main.go+++ b/cmd/agentmesh-node/main.go@@ -85,6 +85,7 @@ var (assumeYesFlag boolofflineAccessFlag boollogLevelFlag string+ logFormatFlag stringkeyGracePeriodFlag time.DurationallowLoopbackFlag boolannouncePrivateFlag bool@@ -315,7 +316,7 @@ func main() {return}// Duplicate logs to the ring buffer served by /debug/logs.- logging.Setup(logLevelFlag, "ringbuffer://")+ logging.Setup(logging.Options{Level: logLevelFlag, Format: logFormatFlag, SinkURL: "ringbuffer://"})apiTokenFlag = resolveDaemonSecret("api-token", apiTokenPathFlag, "AGENTMESH_API_TOKEN")bootstrapTokenFlag = resolveSecretFlag("bootstrap-token", bootstrapTokenFlag, bootstrapTokenPathFlag)@@ -869,6 +870,7 @@ func main() {runCmd.Flags().BoolVar(&enableRelayFlag, "enable-relay", false, "Allow this node to serve as a relay for others")runCmd.Flags().BoolVar(&daemonizeFlag, "daemonize", false, "Run the node in the background and return once its local API answers (writes agentmesh-node.log and agentmesh-node.pid to the data directory, and generates an API token there if none is configured)")runCmd.Flags().StringVar(&logLevelFlag, "log-level", "info", "Log level (debug, info, warn, error)")+ runCmd.Flags().StringVar(&logFormatFlag, "log-format", "", "Log format (text, json); when unset, the LOG_FORMAT env var decides, else text")runCmd.Flags().DurationVar(&keyGracePeriodFlag, "key-grace-period", 24*time.Hour, "Key grace period for old keys (e.g. 24h)")runCmd.Flags().BoolVar(&allowLoopbackFlag, "allow-loopback", false, "Allow publishing and connecting to loopback/link-local addresses")runCmd.Flags().BoolVar(&announcePrivateFlag, "announce-private", true, "Publish this host's private (RFC1918/ULA) addresses to the mesh; keep enabled for LAN or on-premises meshes, disable when peers are only reachable through routers or public addresses")diff --git a/cmd/agentmesh-one/main.go b/cmd/agentmesh-one/main.goindex 73d5f81e..832e146c 100644--- a/cmd/agentmesh-one/main.go+++ b/cmd/agentmesh-one/main.go@@ -59,6 +59,7 @@ func main() {oidcClientID stringallowedAudiencesFlag stringlogLevel string+ logFormat stringtunnelProvider stringtunnelTokenPath stringtunnelInstall bool@@ -75,7 +76,7 @@ func main() {Short: "Agent Mesh - all-in-one standalone server",Version: version.String(),Run: func(cmd *cobra.Command, args []string) {- logging.Setup(logLevel, "")+ logging.Setup(logging.Options{Level: logLevel, Format: logFormat})// Secrets arrive through a file or the environment, never as a// flag value that would sit in `ps` and shell history. Env keeps@@ -214,6 +215,7 @@ func main() {rootCmd.Flags().StringVar(&oidcClientID, "oidc-client-id", "", "OAuth client id advertised via /info (defaults to the first allowed audience)")rootCmd.Flags().StringVar(&allowedAudiencesFlag, "allowed-audiences", api.DefaultAudience, "Comma-separated list of allowed OIDC audiences")rootCmd.Flags().StringVar(&logLevel, "log-level", "", "Log level: debug, info, warn, error")+ rootCmd.Flags().StringVar(&logFormat, "log-format", "", "Log format (text, json); when unset, the LOG_FORMAT env var decides, else text")rootCmd.Flags().StringVar(&tunnelProvider, "tunnel", "", "Publish the listener on a public URL through a tunnel provider ("+strings.Join(tunnel.Names(), ", ")+"); sets the external URL")rootCmd.Flags().StringVar(&tunnelTokenPath, "tunnel-token-path", "", "File containing the tunnel provider token (or env AGENTMESH_TUNNEL_TOKEN); use with --tunnel <provider> --external-url https://...")rootCmd.Flags().BoolVar(&tunnelInstall, "tunnel-install", false, "Download the pinned connector binary (cloudflared "+tunnel.CloudflaredVersion+", digest-verified) into <data-dir>/bin without asking; implies accepting its license")diff --git a/cmd/agentmesh-router/main.go b/cmd/agentmesh-router/main.goindex a7da7fcd..d80504d6 100644--- a/cmd/agentmesh-router/main.go+++ b/cmd/agentmesh-router/main.go@@ -46,6 +46,7 @@ var (allowLoopback boolconnsPerSourceIP intlogLevel string+ logFormat stringdhtProviderAddrTTL time.DurationdhtMaxRecordAge time.DurationlowWaterMark int@@ -69,7 +70,7 @@ func main() {Short: "Agent Mesh - libp2p Router Node",Version: version.String(),Run: func(cmd *cobra.Command, args []string) {- logging.Setup(logLevel, "")+ logging.Setup(logging.Options{Level: logLevel, Format: logFormat})opts := router.Options{ControlPlaneURL: controlPlaneURL,@@ -144,6 +145,7 @@ func main() {rootCmd.Flags().BoolVar(&allowLoopback, "allow-loopback", false, "Allow loopback and link-local addresses for discovery")rootCmd.Flags().IntVar(&connsPerSourceIP, "conns-per-source-ip", 0, "Max inbound connections per source IP; 0 is a quarter of --high-watermark, so filling the router takes at least four addresses. Raise it where more members share one address")rootCmd.Flags().StringVar(&logLevel, "log-level", "info", "Log level (debug, info, warn, error)")+ rootCmd.Flags().StringVar(&logFormat, "log-format", "", "Log format (text, json); when unset, the LOG_FORMAT env var decides, else text")rootCmd.Flags().DurationVar(&dhtProviderAddrTTL, "dht-provider-addr-ttl", router.DefaultDHTProviderAddrTTL, "How long a DHT provider record lives after its last announcement")rootCmd.Flags().DurationVar(&dhtMaxRecordAge, "dht-max-record-age", 0, "Maximum age for DHT records (0s uses library default)")rootCmd.Flags().IntVar(&lowWaterMark, "low-watermark", 0, "Connections the router trims down to once it is over --high-watermark; 0 is 15% below the high watermark, so a trim sheds that share and no more")diff --git a/internal/logging/logging.go b/internal/logging/logging.goindex 1108ee80..5d3c1029 100644--- a/internal/logging/logging.go+++ b/internal/logging/logging.go@@ -23,33 +23,47 @@ import (var logger = golog.Logger("logging")-// Setup applies the sink URL, the LOG_FORMAT=json bridge and the log level.-// go-log reads GOLOG_LOG_FMT at package init, before main runs, so the-// config has to be re-applied here through SetupLogging.-func Setup(level, sinkURL string) {+// Options selects how a binary logs. Zero values keep the defaults: info+// level, the LOG_FORMAT env var (else text) and no extra sink.+type Options struct {+ Level string // log level: debug, info, warn or error+ Format string // output format: text or json; empty falls back to $LOG_FORMAT, else text+ SinkURL string // extra go-log sink, e.g. the node's ring buffer+}++// Setup applies opts process-wide. go-log reads its GOLOG_* env at package+// init, before main runs, so the config is re-applied here through+// SetupLogging.+func Setup(opts Options) {+ lvl, levelErr := golog.LevelFromString(opts.Level)+ if levelErr != nil {+ lvl = golog.LevelInfo+ }+ format := opts.Format+ badFormat := format != "" && format != "text" && format != "json"+ if format == "" || badFormat {+ format = os.Getenv("LOG_FORMAT")+ }+cfg := golog.GetConfig()- if sinkURL != "" {- cfg.URL = sinkURL+ if opts.SinkURL != "" {+ cfg.URL = opts.SinkURL}- if os.Getenv("LOG_FORMAT") == "json" {+ switch format {+ case "json":cfg.Format = golog.JSONOutput+ case "text":+ cfg.Format = golog.PlaintextOutput}golog.SetupLogging(cfg)-- lvl := golog.LevelInfo- var levelErr error- if level != "" {- if parsed, err := golog.LevelFromString(level); err == nil {- lvl = parsed- } else {- levelErr = err- }- }golog.SetAllLoggers(lvl)- // Warn after SetAllLoggers, which sets this logger to a level that lets- // the warning through.++ // go-log starts at error level, so warnings only show after SetAllLoggers.if levelErr != nil {- logger.Warnf("invalid log level %q, falling back to info", level)+ logger.Warnf("invalid log level %q, using info", opts.Level)+ }+ if badFormat {+ logger.Warnf("invalid log format %q, want text or json; using LOG_FORMAT", opts.Format)}// go-libp2p-kad-dht logs routine small-mesh conditions (empty routing// table, no closer peers) at INFO/ERROR, the latter through a malformeddiff --git a/internal/logging/logging_test.go b/internal/logging/logging_test.goindex 1f62c6ac..ff61abaa 100644--- a/internal/logging/logging_test.go+++ b/internal/logging/logging_test.go@@ -21,11 +21,36 @@ import ("go.uber.org/zap/zapcore")-func TestSetupFormatBridge(t *testing.T) {- t.Setenv("LOG_FORMAT", "json")- Setup("info", "")- if got := golog.GetConfig().Format; got != golog.JSONOutput {- t.Fatalf("format = %v, want %v (JSONOutput)", got, golog.JSONOutput)+func TestSetupFormat(t *testing.T) {+ cases := []struct {+ name string+ env string+ flag string+ want golog.LogFormat+ }{+ {"env selects json", "json", "", golog.JSONOutput},+ {"env selects text", "text", "", golog.PlaintextOutput},+ {"flag wins over env", "json", "text", golog.PlaintextOutput},+ {"flag selects json", "", "json", golog.JSONOutput},+ {"invalid flag falls back to env", "json", "bogus", golog.JSONOutput},+ }+ for _, tc := range cases {+ t.Run(tc.name, func(t *testing.T) {+ t.Setenv("LOG_FORMAT", tc.env)+ Setup(Options{Format: tc.flag})+ if got := golog.GetConfig().Format; got != tc.want {+ t.Errorf("format = %v, want %v", got, tc.want)+ }+ })+ }+}++func TestSetupInvalidFormatKeepsCurrent(t *testing.T) {+ t.Setenv("LOG_FORMAT", "")+ Setup(Options{Format: "text"})+ Setup(Options{Format: "bogus"})+ if got := golog.GetConfig().Format; got != golog.PlaintextOutput {+ t.Errorf("format = %v, want %v (PlaintextOutput): an invalid value must keep the current format", got, golog.PlaintextOutput)}}@@ -33,27 +58,27 @@ func TestSetupLevels(t *testing.T) {dht := golog.Logger("dht")app := golog.Logger("logging-test-app")- Setup("info", "")- if !app.Desugar().Core().Enabled(zapcore.InfoLevel) {- t.Error("info level not applied to ordinary loggers")+ Setup(Options{Level: "info"})+ if got := app.Desugar().Core().Enabled(zapcore.InfoLevel); !got {+ t.Errorf("app Enabled(Info) = %v, want true after Setup(info)", got)}- if dht.Desugar().Core().Enabled(zapcore.ErrorLevel) {- t.Error("dht logger not suppressed at info level")+ if got := dht.Desugar().Core().Enabled(zapcore.ErrorLevel); got {+ t.Errorf("dht Enabled(Error) = %v, want false (suppressed) at info level", got)}- Setup("debug", "")- if !dht.Desugar().Core().Enabled(zapcore.ErrorLevel) {- t.Error("explicit debug level must keep dht logs")+ Setup(Options{Level: "debug"})+ if got := dht.Desugar().Core().Enabled(zapcore.ErrorLevel); !got {+ t.Errorf("dht Enabled(Error) = %v, want true at explicit debug level", got)}}func TestSetupInvalidLevelFallsBackToInfo(t *testing.T) {app := golog.Logger("logging-test-fallback")- Setup("degub", "")- if !app.Desugar().Core().Enabled(zapcore.InfoLevel) {- t.Error("invalid level must fall back to info, got stricter")+ Setup(Options{Level: "degub"})+ if got := app.Desugar().Core().Enabled(zapcore.InfoLevel); !got {+ t.Errorf("app Enabled(Info) = %v, want true: invalid level must fall back to info", got)}- if app.Desugar().Core().Enabled(zapcore.DebugLevel) {- t.Error("invalid level must fall back to info, got debug")+ if got := app.Desugar().Core().Enabled(zapcore.DebugLevel); got {+ t.Errorf("app Enabled(Debug) = %v, want false: invalid level must fall back to info, not debug", got)}}diff --git a/site/content/docs/reference/agentmesh-node.md b/site/content/docs/reference/agentmesh-node.mdindex 8f4dfc85..82c5f167 100644--- a/site/content/docs/reference/agentmesh-node.md+++ b/site/content/docs/reference/agentmesh-node.md@@ -127,7 +127,8 @@ explains how to enroll.| `--monitor-bootstrap` | `2m` | Delay before the router-connection monitor starts. || `--monitor-interval` | `1m` | How often the monitor checks that a router is connected. || `--autorelay-min-interval`, `--autorelay-boot-delay`, `--autorelay-backoff` | `30s`, `0s`, `3s` | libp2p AutoRelay tuning. |-| `--log-level` | `info` | `debug`, `info`, `warn` or `error`. `LOG_FORMAT=json` selects JSON output. |+| `--log-level` | `info` | `debug`, `info`, `warn` or `error`. |+| `--log-format` | | `text` or `json`. When unset, the `LOG_FORMAT` env var decides, else `text`. |## `reset`diff --git a/site/content/docs/reference/agentmesh-one.md b/site/content/docs/reference/agentmesh-one.mdindex b7f073d9..d6cc1694 100644--- a/site/content/docs/reference/agentmesh-one.md+++ b/site/content/docs/reference/agentmesh-one.md@@ -48,7 +48,8 @@ downloaded tunnel connector.| `--allowed-audiences` | `agentmesh-audience` | Accepted OIDC audiences. || `--enroll-qr` | when stdout is a terminal | Print a device-enrollment QR code at startup. Only for `https` URLs. || `--enroll-qr-max-usages` | `1` | How many devices the startup QR admits. |-| `--log-level` | `info` | `debug`, `info`, `warn`, `error`. `LOG_FORMAT=json` selects JSON output. |+| `--log-level` | `info` | `debug`, `info`, `warn`, `error`. |+| `--log-format` | | `text` or `json`. When unset, the `LOG_FORMAT` env var decides, else `text`. |### Embedded component tunablesdiff --git a/site/content/docs/reference/control-plane.md b/site/content/docs/reference/control-plane.mdindex dd67824f..ea073e5d 100644--- a/site/content/docs/reference/control-plane.md+++ b/site/content/docs/reference/control-plane.md@@ -39,7 +39,8 @@ agentmesh-control-plane admin unban --peer <id> lift a ban| `--lease-duration` | `15m` | How long a router lease lasts without renewal. || `--node-retention` | `720h` (30 days) | How long the record of an enrolled node is kept after its session expires. Banned nodes are kept forever. `0` keeps every record. || `--mesh-reconnect-interval` | `30s` | How often the event publisher re-reads the router leases and dials any router it is not connected to. On each router it runs the mesh handshake with a credential it mints for its own peer under the signing key, with the role `mesh:role:control-plane`, so the router holds its connection as a member's. |-| `--log-level` | `info` | `debug`, `info`, `warn`, `error`. `LOG_FORMAT=json` selects JSON output. |+| `--log-level` | `info` | `debug`, `info`, `warn`, `error`. |+| `--log-format` | | `text` or `json`. When unset, the `LOG_FORMAT` env var decides, else `text`. |## HTTP APIdiff --git a/site/content/docs/reference/router.md b/site/content/docs/reference/router.mdindex 8e1a4ef4..dd1cd9bf 100644--- a/site/content/docs/reference/router.md+++ b/site/content/docs/reference/router.md@@ -52,7 +52,8 @@ replica count.| `--auth-deadline` | `1m` | How long an inbound connection may stay open without its peer passing the handshake. A member handshakes the moment it connects; a member attached elsewhere whose DHT client came to query is done in less, as is the control plane's event publisher, which presents a credential of its own. What is left at the deadline is closed, so a connection without a session costs the router a minute, and the watermarks mean members. `0` keeps every connection. || `--metrics-addr` | off | Serve `/metrics`, `/healthz`, `/readyz` and `/debug/peer` without authentication on this address. `/readyz` returns `200` once the router is enrolled and the libp2p host is up. Keep this address separate from the libp2p ports and inside the cluster. || `--pprof-addr` | off | Serve Go runtime profiles under `/debug/pprof/` on this address, for attributing memory or goroutines the metrics show as held. Unauthenticated, and a heap profile is a dump of process memory, which holds the router's key: bind it to loopback and reach it with `kubectl port-forward`. |-| `--log-level` | `info` | `debug`, `info`, `warn`, `error`. `LOG_FORMAT=json` selects JSON output. |+| `--log-level` | `info` | `debug`, `info`, `warn`, `error`. |+| `--log-format` | | `text` or `json`. When unset, the `LOG_FORMAT` env var decides, else `text`. |## What it does at run time