tanmayrauth commented on code in PR #1343:
URL: https://github.com/apache/iceberg-go/pull/1343#discussion_r3501079368


##########
metrics/reporters_test.go:
##########
@@ -0,0 +1,143 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package metrics
+
+import (
+       "context"
+       "sync"
+       "testing"
+
+       "github.com/stretchr/testify/assert"
+       "github.com/stretchr/testify/require"
+)
+
+// testReport is a minimal MetricsReport. The marker is unexported (sealed to
+// this package), so report values can only be created from within the
+// package — this white-box test is how we exercise the reporters until the
+// concrete report types land.
+type testReport struct{ name string }
+
+func (testReport) isMetricsReport() {}
+
+// countingReporter records how many reports it received.
+type countingReporter struct {
+       mu    sync.Mutex
+       count int
+}
+
+func (c *countingReporter) Report(context.Context, MetricsReport) {
+       c.mu.Lock()
+       defer c.mu.Unlock()
+       c.count++
+}
+
+func (c *countingReporter) calls() int {
+       c.mu.Lock()
+       defer c.mu.Unlock()
+
+       return c.count
+}
+
+// panickingReporter always panics, to verify Combine isolates failures.
+type panickingReporter struct{}
+
+func (panickingReporter) Report(context.Context, MetricsReport) { 
panic("boom") }
+
+func TestNopReporter(t *testing.T) {
+       // NopReporter must accept anything, including nil, without panicking.
+       assert.NotPanics(t, func() {
+               NopReporter{}.Report(context.Background(), nil)
+               NopReporter{}.Report(context.Background(), testReport{})
+       })
+}
+
+func TestInMemoryReporter(t *testing.T) {
+       var r InMemoryReporter
+       require.Empty(t, r.Reports())
+
+       r.Report(context.Background(), nil) // ignored
+       require.Empty(t, r.Reports())
+
+       r.Report(context.Background(), testReport{name: "a"})
+       r.Report(context.Background(), testReport{name: "b"})
+
+       got := r.Reports()
+       require.Len(t, got, 2)
+       assert.Equal(t, testReport{name: "a"}, got[0])
+       assert.Equal(t, testReport{name: "b"}, got[1])
+
+       // Reports returns a copy: mutating it must not affect the reporter.
+       got[0] = nil
+       require.Len(t, r.Reports(), 2)
+
+       r.Reset()
+       require.Empty(t, r.Reports())
+}
+
+func TestLoggingReporterNilLoggerAndReport(t *testing.T) {
+       r := NewLoggingReporter(nil) // must fall back to slog.Default
+       require.NotNil(t, r)
+       assert.NotPanics(t, func() {
+               r.Report(context.Background(), nil)
+               r.Report(context.Background(), testReport{name: "x"})
+       })
+}
+
+func TestCombine(t *testing.T) {
+       t.Run("no reporters returns Nop", func(t *testing.T) {
+               assert.IsType(t, NopReporter{}, Combine())
+               assert.IsType(t, NopReporter{}, Combine(nil, nil))
+       })
+
+       t.Run("single reporter returned directly", func(t *testing.T) {
+               c := &countingReporter{}
+               assert.Same(t, c, Combine(c))
+               assert.Same(t, c, Combine(nil, c, nil))
+       })
+
+       t.Run("fans out to all", func(t *testing.T) {
+               a, b := &countingReporter{}, &countingReporter{}
+               Combine(a, b).Report(context.Background(), testReport{})
+               assert.Equal(t, 1, a.calls())
+               assert.Equal(t, 1, b.calls())
+       })
+
+       t.Run("isolates a panicking reporter", func(t *testing.T) {
+               after := &countingReporter{}
+               r := Combine(panickingReporter{}, after)
+               assert.NotPanics(t, func() {
+                       r.Report(context.Background(), testReport{})
+               })
+               assert.Equal(t, 1, after.calls(), "reporters after a panic 
still run")
+       })
+}
+
+func TestInMemoryReporterConcurrent(t *testing.T) {

Review Comment:
   Both done. Extended test-race in the Makefile to include ./metrics/..., and 
the test now runs a foreground goroutine looping on Reports() while the 100 
writers run, so -race actually exercises the read-under-write path instead of  
just confirming the goroutines finish. Verified locally with go test -race 
./metrics/....



##########
metrics/reporters_test.go:
##########
@@ -0,0 +1,143 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package metrics
+
+import (
+       "context"
+       "sync"
+       "testing"
+
+       "github.com/stretchr/testify/assert"
+       "github.com/stretchr/testify/require"
+)
+
+// testReport is a minimal MetricsReport. The marker is unexported (sealed to
+// this package), so report values can only be created from within the
+// package — this white-box test is how we exercise the reporters until the
+// concrete report types land.
+type testReport struct{ name string }
+
+func (testReport) isMetricsReport() {}
+
+// countingReporter records how many reports it received.
+type countingReporter struct {
+       mu    sync.Mutex
+       count int
+}
+
+func (c *countingReporter) Report(context.Context, MetricsReport) {
+       c.mu.Lock()
+       defer c.mu.Unlock()
+       c.count++
+}
+
+func (c *countingReporter) calls() int {
+       c.mu.Lock()
+       defer c.mu.Unlock()
+
+       return c.count
+}
+
+// panickingReporter always panics, to verify Combine isolates failures.
+type panickingReporter struct{}
+
+func (panickingReporter) Report(context.Context, MetricsReport) { 
panic("boom") }
+
+func TestNopReporter(t *testing.T) {
+       // NopReporter must accept anything, including nil, without panicking.
+       assert.NotPanics(t, func() {
+               NopReporter{}.Report(context.Background(), nil)
+               NopReporter{}.Report(context.Background(), testReport{})
+       })
+}
+
+func TestInMemoryReporter(t *testing.T) {
+       var r InMemoryReporter
+       require.Empty(t, r.Reports())
+
+       r.Report(context.Background(), nil) // ignored
+       require.Empty(t, r.Reports())
+
+       r.Report(context.Background(), testReport{name: "a"})
+       r.Report(context.Background(), testReport{name: "b"})
+
+       got := r.Reports()
+       require.Len(t, got, 2)
+       assert.Equal(t, testReport{name: "a"}, got[0])
+       assert.Equal(t, testReport{name: "b"}, got[1])
+
+       // Reports returns a copy: mutating it must not affect the reporter.
+       got[0] = nil
+       require.Len(t, r.Reports(), 2)
+
+       r.Reset()
+       require.Empty(t, r.Reports())
+}
+
+func TestLoggingReporterNilLoggerAndReport(t *testing.T) {
+       r := NewLoggingReporter(nil) // must fall back to slog.Default
+       require.NotNil(t, r)
+       assert.NotPanics(t, func() {

Review Comment:
   Splitted into two subtests: the nil-logger fallback keeps the NotPanics 
check, and a new subtest wires a slog.NewTextHandler over a bytes.Buffer and 
asserts the output contains level=INFO, iceberg metrics report, and the  report 
value — and that a nil report produces no output. It'll now fail if the log 
call is deleted, mis-leveled, or drops the report.



##########
metrics/reporters.go:
##########
@@ -0,0 +1,141 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package metrics
+
+import (
+       "context"
+       "log/slog"
+       "sync"
+)
+
+// NopReporter is a [Reporter] that discards every report. It is the default
+// when no reporter is configured, so that instrumentation is free unless a 
user
+// opts in. The zero value is ready to use.
+type NopReporter struct{}
+
+var _ Reporter = NopReporter{}
+
+// Report implements [Reporter] and does nothing.
+func (NopReporter) Report(context.Context, MetricsReport) {}
+
+// LoggingReporter is a [Reporter] that logs each report via an [slog.Logger]. 
It
+// is a convenient default for development and debugging.
+type LoggingReporter struct {
+       logger *slog.Logger
+}
+
+var _ Reporter = (*LoggingReporter)(nil)
+
+// NewLoggingReporter returns a [LoggingReporter] that logs to logger. If 
logger
+// is nil, [slog.Default] is used.
+func NewLoggingReporter(logger *slog.Logger) *LoggingReporter {
+       if logger == nil {
+               logger = slog.Default()
+       }
+
+       return &LoggingReporter{logger: logger}
+}
+
+// Report logs report at info level.
+func (r *LoggingReporter) Report(ctx context.Context, report MetricsReport) {
+       if report == nil {
+               return
+       }
+       r.logger.InfoContext(ctx, "iceberg metrics report", "report", report)
+}
+
+// InMemoryReporter is a [Reporter] that retains every report it receives. It 
is
+// primarily intended for tests and inspection. It is safe for concurrent use.
+type InMemoryReporter struct {
+       mu      sync.Mutex
+       reports []MetricsReport
+}
+
+var _ Reporter = (*InMemoryReporter)(nil)
+
+// Report appends report to the retained set.
+func (r *InMemoryReporter) Report(_ context.Context, report MetricsReport) {
+       if report == nil {
+               return
+       }
+       r.mu.Lock()
+       defer r.mu.Unlock()
+       r.reports = append(r.reports, report)
+}
+
+// Reports returns a copy of the reports received so far, in arrival order.
+func (r *InMemoryReporter) Reports() []MetricsReport {
+       r.mu.Lock()
+       defer r.mu.Unlock()
+
+       return append([]MetricsReport(nil), r.reports...)
+}
+
+// Reset discards all retained reports.
+func (r *InMemoryReporter) Reset() {
+       r.mu.Lock()
+       defer r.mu.Unlock()
+       r.reports = nil
+}
+
+// Combine returns a [Reporter] that forwards each report to all of the given
+// reporters in order. nil reporters are skipped. A panic in one reporter must
+// not prevent the others from receiving the report, so each call is isolated;
+// in keeping with the [Reporter] contract a misbehaving reporter never affects
+// the observed operation. A recovered panic is logged at debug level via
+// [slog.Default] so a broken reporter is not entirely invisible.
+//
+// As a convenience, Combine with no reporters returns [NopReporter], and with 
a
+// single non-nil reporter returns that reporter directly. The per-reporter
+// panic recovery therefore applies only when two or more reporters are
+// combined: a lone reporter is returned unwrapped and runs exactly as it would
+// if called directly, without the safety net.
+func Combine(reporters ...Reporter) Reporter {
+       nonNil := make([]Reporter, 0, len(reporters))
+       for _, r := range reporters {
+               if r != nil {
+                       nonNil = append(nonNil, r)
+               }
+       }
+
+       switch len(nonNil) {

Review Comment:
   Agreed, went with always-wrap. Dropped the case 1 fast path entirely — 
Combine now returns NopReporter{} for zero non-nil reporters and a 
*compositeReporter otherwise, so even a lone reporter gets the isolation we 
advertise and   (*LoggingReporter)(nil) can no longer escape unwrapped to 
nil-deref later. The one extra range over a single-element slice is well worth 
closing the bug class. Added a test that drives a typed-nil pointer through 
Combine and  asserts it doesn't panic.



##########
metrics/reporters.go:
##########
@@ -0,0 +1,141 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package metrics
+
+import (
+       "context"
+       "log/slog"
+       "sync"
+)
+
+// NopReporter is a [Reporter] that discards every report. It is the default
+// when no reporter is configured, so that instrumentation is free unless a 
user
+// opts in. The zero value is ready to use.
+type NopReporter struct{}
+
+var _ Reporter = NopReporter{}
+
+// Report implements [Reporter] and does nothing.
+func (NopReporter) Report(context.Context, MetricsReport) {}
+
+// LoggingReporter is a [Reporter] that logs each report via an [slog.Logger]. 
It
+// is a convenient default for development and debugging.
+type LoggingReporter struct {
+       logger *slog.Logger
+}
+
+var _ Reporter = (*LoggingReporter)(nil)
+
+// NewLoggingReporter returns a [LoggingReporter] that logs to logger. If 
logger
+// is nil, [slog.Default] is used.
+func NewLoggingReporter(logger *slog.Logger) *LoggingReporter {
+       if logger == nil {
+               logger = slog.Default()
+       }
+
+       return &LoggingReporter{logger: logger}
+}
+
+// Report logs report at info level.
+func (r *LoggingReporter) Report(ctx context.Context, report MetricsReport) {
+       if report == nil {
+               return
+       }
+       r.logger.InfoContext(ctx, "iceberg metrics report", "report", report)
+}
+
+// InMemoryReporter is a [Reporter] that retains every report it receives. It 
is
+// primarily intended for tests and inspection. It is safe for concurrent use.
+type InMemoryReporter struct {
+       mu      sync.Mutex
+       reports []MetricsReport
+}
+
+var _ Reporter = (*InMemoryReporter)(nil)
+
+// Report appends report to the retained set.
+func (r *InMemoryReporter) Report(_ context.Context, report MetricsReport) {
+       if report == nil {
+               return
+       }
+       r.mu.Lock()
+       defer r.mu.Unlock()
+       r.reports = append(r.reports, report)
+}
+
+// Reports returns a copy of the reports received so far, in arrival order.
+func (r *InMemoryReporter) Reports() []MetricsReport {
+       r.mu.Lock()
+       defer r.mu.Unlock()
+
+       return append([]MetricsReport(nil), r.reports...)
+}
+
+// Reset discards all retained reports.
+func (r *InMemoryReporter) Reset() {
+       r.mu.Lock()
+       defer r.mu.Unlock()
+       r.reports = nil
+}
+
+// Combine returns a [Reporter] that forwards each report to all of the given
+// reporters in order. nil reporters are skipped. A panic in one reporter must
+// not prevent the others from receiving the report, so each call is isolated;
+// in keeping with the [Reporter] contract a misbehaving reporter never affects
+// the observed operation. A recovered panic is logged at debug level via
+// [slog.Default] so a broken reporter is not entirely invisible.
+//
+// As a convenience, Combine with no reporters returns [NopReporter], and with 
a
+// single non-nil reporter returns that reporter directly. The per-reporter
+// panic recovery therefore applies only when two or more reporters are
+// combined: a lone reporter is returned unwrapped and runs exactly as it would
+// if called directly, without the safety net.
+func Combine(reporters ...Reporter) Reporter {
+       nonNil := make([]Reporter, 0, len(reporters))
+       for _, r := range reporters {
+               if r != nil {
+                       nonNil = append(nonNil, r)
+               }
+       }
+
+       switch len(nonNil) {
+       case 0:
+               return NopReporter{}
+       case 1:
+               return nonNil[0]
+       default:
+               return compositeReporter(nonNil)
+       }
+}
+
+type compositeReporter []Reporter
+

Review Comment:
   compositeReporter is now a struct carrying an optional *slog.Logger, and the 
recover logs at Warn with the offending reporter's %T plus the recovered value 
before continuing — matching the re-surface pattern in  schema.go/visitors.go. 
Still swallowed for propagation, just no longer silent.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to