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


##########
metrics/reporters.go:
##########
@@ -0,0 +1,131 @@
+// 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"
+)
+
+// Nop 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 Nop struct{}
+
+var _ Reporter = Nop{}
+
+// Report implements [Reporter] and does nothing.
+func (Nop) Report(context.Context, Report) {}
+
+// 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 Report) {
+       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 []Report
+}
+
+var _ Reporter = (*InMemoryReporter)(nil)
+
+// Report appends report to the retained set.
+func (r *InMemoryReporter) Report(_ context.Context, report Report) {
+       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() []Report {
+       r.mu.Lock()
+       defer r.mu.Unlock()
+
+       return append([]Report(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.
+//
+// As a convenience, Combine with no reporters returns [Nop], and with a single
+// non-nil reporter returns that reporter directly.
+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 Nop{}
+       case 1:
+               return nonNil[0]

Review Comment:
   A few minor notes here (all non-blocking):
   
   - **Isolation asymmetry:** `Combine(r)` returns `r` directly, so a *single* 
reporter is not panic-isolated, whereas the composite path below (L124-129) 
recovers per reporter. Fine as an optimization, but worth a doc line that the 
panic safety-net only applies with 2+ reporters (or always wrap for uniform 
behavior).
   - **Silent recover (L127):** `defer func() { _ = recover() }()` swallows a 
panicking reporter entirely. Per the `Reporter` contract that's intentional, 
but a fully-silent recover makes a broken reporter invisible — consider logging 
the recovered value at debug level.
   - **FYI (not a change):** this is Phase 1 scaffolding — no concrete `Report` 
types or scan/commit instrumentation yet, so nothing produces reports in-tree 
until the later phases of #1236. Noting for anyone reading the package in 
isolation.



##########
metrics/reporters.go:
##########
@@ -0,0 +1,131 @@
+// 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"
+)
+
+// Nop 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 Nop struct{}

Review Comment:
   Tiny consistency nit: the other built-ins are `LoggingReporter` / 
`InMemoryReporter`, but this one is `Nop`. Consider `NopReporter` so the set 
reads uniformly — or keep `Nop` intentionally as the short form; just flagging 
the asymmetry. Non-blocking.



##########
metrics/reporter.go:
##########
@@ -0,0 +1,54 @@
+// 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 implements Iceberg's Metrics Reporting API for iceberg-go.
+//
+// A [Reporter] is a pluggable sink that receives a [Report] — a ScanReport
+// after scan planning or a CommitReport after a commit — describing what the
+// client did (files and manifests considered, scanned and skipped; bytes read;
+// commit attempts and durations). Reporting gives operators a standard way to
+// aggregate these otherwise-invisible client-side metrics across many clients.
+//
+// Reporting is strictly opt-in: with no reporter configured the instrumented
+// code paths do no work. Reporters must never block or fail the scan/commit
+// they observe — see [Reporter] for the contract.
+//
+// This package provides the contract and the built-in reporters ([Nop],
+// [LoggingReporter], [InMemoryReporter], and [Combine]). The concrete report
+// types and the scan/commit instrumentation are layered on top in later work.
+package metrics
+
+import "context"
+
+// Report is the sealed marker interface implemented by the concrete report
+// types (ScanReport and CommitReport). It is sealed to this package so the set
+// of report types stays under the framework's control.
+type Report interface {

Review Comment:
   **Naming — the one thing worth settling before this public API ships.**
   
   The marker interface is `Report` while the contract method is 
`Reporter.Report(ctx, report Report)`, so signatures read as 
`Report(context.Context, Report)` — a method named like its own parameter type. 
Java/PyIceberg call the marker **`MetricsReport`**, with 
`ScanReport`/`CommitReport` implementing it and 
`MetricsReporter.report(MetricsReport)`.
   
   Suggest renaming this interface `Report` → `MetricsReport` (keep 
`Reporter.Report(...)` as the verb). That (a) disambiguates the type from the 
method, and (b) aligns the Go API with the rest of the Iceberg ecosystem, 
easing ports from Java. Since `ScanReport`/`CommitReport` arrive in later 
phases, settling the marker name now avoids a churny rename across those PRs.



-- 
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