diff --git a/go.mod b/go.mod index edac38e550..ffd413ac3a 100644 --- a/go.mod +++ b/go.mod @@ -20,7 +20,7 @@ require ( github.com/google/uuid v1.6.0 github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.1 github.com/hashicorp/go-hclog v1.6.3 - github.com/hashicorp/go-plugin v1.6.3 + github.com/hashicorp/go-plugin v1.7.0 github.com/iancoleman/strcase v0.3.0 github.com/invopop/jsonschema v0.13.0 github.com/jackc/pgx/v4 v4.18.3 @@ -83,7 +83,6 @@ require ( github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59 // indirect github.com/bahlo/generic-list-go v0.2.0 // indirect github.com/beorn7/perks v1.0.1 // indirect - github.com/bufbuild/protocompile v0.14.1 // indirect github.com/buger/goterm v1.0.4 // indirect github.com/buger/jsonparser v1.1.1 // indirect github.com/cenkalti/backoff/v5 v5.0.2 // indirect @@ -112,7 +111,6 @@ require ( github.com/jackc/pgproto3/v2 v2.3.3 // indirect github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect github.com/jackc/pgtype v1.14.4 // indirect - github.com/jhump/protoreflect v1.15.3 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/compress v1.18.0 // indirect github.com/klauspost/cpuid/v2 v2.2.10 // indirect diff --git a/go.sum b/go.sum index 81ae10f5c9..c082658dd6 100644 --- a/go.sum +++ b/go.sum @@ -143,8 +143,8 @@ github.com/hako/durafmt v0.0.0-20200710122514-c0fb7b4da026 h1:BpJ2o0OR5FV7vrkDYf github.com/hako/durafmt v0.0.0-20200710122514-c0fb7b4da026/go.mod h1:5Scbynm8dF1XAPwIwkGPqzkM/shndPm79Jd1003hTjE= github.com/hashicorp/go-hclog v1.6.3 h1:Qr2kF+eVWjTiYmU7Y31tYlP1h0q/X3Nl3tPGdaB11/k= github.com/hashicorp/go-hclog v1.6.3/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M= -github.com/hashicorp/go-plugin v1.6.3 h1:xgHB+ZUSYeuJi96WtxEjzi23uh7YQpznjGh0U0UUrwg= -github.com/hashicorp/go-plugin v1.6.3/go.mod h1:MRobyh+Wc/nYy1V4KAXUiYfzxoYhs7V1mlH1Z7iY2h0= +github.com/hashicorp/go-plugin v1.7.0 h1:YghfQH/0QmPNc/AZMTFE3ac8fipZyZECHdDPshfk+mA= +github.com/hashicorp/go-plugin v1.7.0/go.mod h1:BExt6KEaIYx804z8k4gRzRLEvxKVb+kn0NMcihqOqb8= github.com/hashicorp/yamux v0.1.2 h1:XtB8kyFOyHXYVFnwT5C3+Bdo8gArse7j2AQ0DA0Uey8= github.com/hashicorp/yamux v0.1.2/go.mod h1:C+zze2n6e/7wshOZep2A70/aQU6QBRWJO/G6FT1wIns= github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI= @@ -202,8 +202,8 @@ github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0f github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= github.com/jackc/puddle v1.3.0/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= -github.com/jhump/protoreflect v1.15.3 h1:6SFRuqU45u9hIZPJAoZ8c28T3nK64BNdp9w6jFonzls= -github.com/jhump/protoreflect v1.15.3/go.mod h1:4ORHmSBmlCW8fh3xHmJMGyul1zNqZK4Elxc8qKP+p1k= +github.com/jhump/protoreflect v1.17.0 h1:qOEr613fac2lOuTgWN4tPAtLL7fUSbuJL5X5XumQh94= +github.com/jhump/protoreflect v1.17.0/go.mod h1:h9+vUUL38jiBzck8ck+6G/aeMX8Z4QUY/NiJPwPNi+8= github.com/jmoiron/sqlx v1.4.0 h1:1PLqN7S1UYp5t4SrVVnt4nUVNemrDAtxlulVe+Qgm3o= github.com/jmoiron/sqlx v1.4.0/go.mod h1:ZrZ7UsYB/weZdl2Bxg6jCRO9c3YHl8r3ahlKmRT4JLY= github.com/jonboulle/clockwork v0.5.0 h1:Hyh9A8u51kptdkR+cqRpT1EebBwTn1oK9YfGYbdFz6I= diff --git a/pkg/loop/internal/test/logger_loop.go b/pkg/loop/internal/test/logger_loop.go index 485ae80b01..ff1e8b6d0d 100644 --- a/pkg/loop/internal/test/logger_loop.go +++ b/pkg/loop/internal/test/logger_loop.go @@ -3,6 +3,7 @@ package test import ( "context" "errors" + "strconv" "time" "github.com/hashicorp/go-plugin" @@ -24,6 +25,27 @@ const ( DEBUG ) +func FormatLevel(l int) string { + switch l { + case PANIC: + return "panic" + case FATAL: + return "fatal" + case CRITICAL: + return "critical" + case ERROR: + return "error" + case INFO: + return "info" + case WARN: + return "warn" + case DEBUG: + return "debug" + default: + return "unknown-" + strconv.Itoa(l) + } +} + // NOTE: This is part of the test package because it needs to be imported by the test binary at `./internal/test/cmd` // as well as the test at `./pkg/loop/logger_loop_test.go` type GRPCPluginLoggerTest struct { diff --git a/pkg/loop/logger.go b/pkg/loop/logger.go index 426a236aa0..693098331b 100644 --- a/pkg/loop/logger.go +++ b/pkg/loop/logger.go @@ -22,6 +22,7 @@ import ( func HCLogLogger(l logger.Logger) hclog.Logger { hcl := hclog.NewInterceptLogger(&hclog.LoggerOptions{ Output: io.Discard, // only write through p.Logger Sink + Level: hclog.Debug, }) hcl.RegisterSink(&hclSinkAdapter{l: logger.Sugared(l)}) return hcl @@ -116,23 +117,22 @@ func parseJSON(input string) (*logMessage, error) { return entry, nil } -// logDebug will parse msg and figure out if it's a panic, fatal or critical log message, this is done here because the hashicorp plugin will push any -// unrecognizable message from stderr as a debug statement -func logDebug(msg string, l logger.SugaredLogger, args ...any) { +// maybeCritical recognizes panic, fatal or critical log message, and logs them again at critical level. +func maybeCritical(msg string, l logger.SugaredLogger, args ...any) bool { if strings.HasPrefix(msg, "panic:") { l.Criticalw(fmt.Sprintf("[PANIC] %s", msg), args...) + return true } else if log, err := parseJSON(msg); err == nil { switch log.Level { case "fatal": l.Criticalw(fmt.Sprintf("[FATAL] %s", log.Message), flattenExtraArgs(log)...) + return true case "critical", "dpanic": l.Criticalw(log.Message, flattenExtraArgs(log)...) - default: - l.Debugw(log.Message, flattenExtraArgs(log)...) + return true } - } else { - l.Debugw(msg, args...) } + return false } func (h *hclSinkAdapter) Accept(_ string, level hclog.Level, msg string, args ...any) { @@ -152,13 +152,17 @@ func (h *hclSinkAdapter) Accept(_ string, level hclog.Level, msg string, args .. case hclog.Trace: l.Debugw(msg, args...) case hclog.Debug: - logDebug(msg, l, args...) + if !maybeCritical(msg, l, args...) { // panic/fatal/critical used to come through as stderr/out which maps to debug + l.Debugw(msg, args...) + } case hclog.Info: l.Infow(msg, args...) case hclog.Warn: l.Warnw(msg, args...) case hclog.Error: - l.Errorw(msg, args...) + if !maybeCritical(msg, l, args...) { // as of v1.7.0, panic/fatal errors are send at error level + l.Errorw(msg, args...) + } } } diff --git a/pkg/loop/logger_loop_test.go b/pkg/loop/logger_loop_test.go index 7a58a46925..f1eb41866e 100644 --- a/pkg/loop/logger_loop_test.go +++ b/pkg/loop/logger_loop_test.go @@ -14,10 +14,7 @@ import ( ) func TestHCLogLoggerPanic(t *testing.T) { - lggr, ol := logger.TestObservedSugared(t, zapcore.DebugLevel) - type testCase struct { - name string level int expectedMessage string expectedCustomKey string @@ -77,7 +74,9 @@ func TestHCLogLoggerPanic(t *testing.T) { } for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { + t.Run(test.FormatLevel(tt.level), func(t *testing.T) { + t.Parallel() + lggr, ol := logger.TestObservedSugared(t, zapcore.DebugLevel) loggerTest := &test.GRPCPluginLoggerTest{SugaredLogger: lggr} cc := loggerTest.ClientConfig() cc.Cmd = NewHelperProcessCommand(test.PluginLoggerTestName, false, tt.level)