Repository navigation
[Prism] Aggregate bounded trie and histogram metrics in job metrics - #41708
prodriguezdefino wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
📋 Review Summary
This PR adds support for aggregating BoundedTrie (beam:metrics:bounded_trie:v1) and HistogramValue (beam:metrics:histogram_int64:v1) metrics in the Prism runner's job service, and enables the corresponding BoundedTrie validation tests in the Python and Java test suites. The implementation of trie merging, bound trimming, and histogram bucket accumulation aligns well with existing Beam SDK implementations and includes thorough unit test coverage.
🔍 General Feedback
- The
BoundedTrieaccumulator accurately mirrors the singleton-to-trie transition, merging, and deterministic subtree trimming semantics found in the Java and Python SDKs. - The
HistogramValueaccumulator properly handles bucket option validation, count summation, and variable-lengthbucket_countsslices.
💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.
|
Assigning reviewers: R: @lostluck for label go. Note: If you would like to opt out of this review, comment Available commands:
The PR bot will only process comments in the main thread (not review comments). |
Problem:
In buildUrnToOpsMap (sdks/go/pkg/beam/runners/prism/internal/jobservices/metrics.go),
typ2accumFac registers accumulators for counters, distributions, gauges, top-N, bottom-N,
string sets, and progress metrics, but omits MonitoringInfoTypeUrns_BOUNDED_TRIE_TYPE
(beam:metrics:bounded_trie:v1) and MonitoringInfoTypeUrns_HISTOGRAM
(beam:metrics:histogram_int64:v1).
When AddShortIDs resolves a metric with either type URN, metricsStore logs an unknown
short-ID debug message, creates no accumulator, drops every update in contributeMetrics,
and omits the metric from GetJobMetrics. Python's PrismRunnerTest disables bounded trie
checks with check_bounded_trie=False, and Java's Prism build excludes
UsesBoundedTrieMetrics.
Fix:
Add metricAccumulator implementations for BoundedTrie (merging singleton and trie nodes
with bound trimming) and HistogramValue (summing bucket counts and total count across
matching BucketOptions), and register both type URNs in typ2accumFac. Enable bounded trie
metric checks in prism_runner_test.py and runners/prism/java/build.gradle, and add unit
tests in metrics_test.go.
Reproduction (Python SDK on PrismRunner):
Python's HistogramCell.to_runner_api_monitoring_info is a stub returning None, so this
script wires it to monitoring_infos.user_histogram to report USER_HISTOGRAM payloads from
the LOOPBACK worker.
```python
import sys
import apache_beam as beam
from apache_beam.metrics import cells
from apache_beam.metrics import monitoring_infos
from apache_beam.metrics.metric import Metrics
from apache_beam.metrics.metric import MetricsFilter
from apache_beam.options.pipeline_options import PipelineOptions
from apache_beam.portability.api import beam_job_api_pb2
from apache_beam.portability.api import metrics_pb2
from apache_beam.utils.histogram import LinearBucket
cells.HistogramCell.to_runner_api_monitoring_info = (
lambda self, name, transform_id: monitoring_infos.user_histogram(
name.namespace, name.name, self.data, ptransform=transform_id))
class RecordMetricsDoFn(beam.DoFn):
def __init__(self):
self.trie = Metrics.bounded_trie("ns", "paths")
self.hist = Metrics.histogram("ns", "latency", LinearBucket(0, 10, 5))
def process(self, element):
seg, val = element
self.trie.add(("root", seg))
self.hist.update(val)
yield element
if __name__ == "__main__":
options = PipelineOptions(
["--runner=PrismRunner", "--environment_type=LOOPBACK"] + sys.argv[1:]
)
p = beam.Pipeline(options=options)
_ = (
p
| beam.Create([("a", 5), ("b", 15), ("b", 25)])
| beam.ParDo(RecordMetricsDoFn())
)
res = p.run()
res.wait_until_finish()
tries = res.metrics().query(
MetricsFilter().with_namespace("ns").with_name("paths")
)["bounded_tries"]
resp = res._job_service.GetJobMetrics(
beam_job_api_pb2.GetJobMetricsRequest(job_id=res._job_id)
)
hists = [
metrics_pb2.HistogramValue.FromString(mi.payload)
for mi in resp.metrics.committed
if monitoring_infos.is_histogram(mi)
and mi.labels.get("NAMESPACE") == "ns"
and mi.labels.get("NAME") == "latency"
]
trie_paths = [set(t.committed.flattened()) for t in tries]
hist_counts = [(h.count, list(h.bucket_counts)) for h in hists]
print("bounded_tries:", trie_paths)
print("histograms:", hist_counts)
assert trie_paths == [{("root", "a", False), ("root", "b", False)}]
assert hist_counts == [(3, [1, 1, 1, 0, 0])]
```
Before the fix (`master`): `bounded_tries: []` and `histograms: []`, and the first assertion fails.
After the fix: `bounded_tries: [{('root', 'a', False), ('root', 'b', False)}]` and `histograms: [(3, [1, 1, 1, 0, 0])]`.
bdb2196 to
1c60804
Compare
Problem:
In
buildUrnToOpsMap(sdks/go/pkg/beam/runners/prism/internal/jobservices/metrics.go),typ2accumFacregisters accumulators for counters, distributions, gauges, top-N, bottom-N, string sets, and progress metrics, but omitsMonitoringInfoTypeUrns_BOUNDED_TRIE_TYPE(beam:metrics:bounded_trie:v1) andMonitoringInfoTypeUrns_HISTOGRAM(beam:metrics:histogram_int64:v1).When
AddShortIDsresolves a metric with either type URN,metricsStorelogs an unknown short-ID debug message, creates no accumulator, drops every update incontributeMetrics, and omits the metric fromGetJobMetrics. Python'sPrismRunnerTestdisables bounded trie checks withcheck_bounded_trie=False, and Java's Prism build excludesUsesBoundedTrieMetrics.Fix:
Add
metricAccumulatorimplementations forBoundedTrie(merging singleton and trie nodes with bound trimming) andHistogramValue(summing bucket counts and total count across matchingBucketOptions), and register both type URNs intyp2accumFac. Enable bounded trie metric checks inprism_runner_test.pyandrunners/prism/java/build.gradle, and add unit tests inmetrics_test.go.Reproduction (Python SDK on
PrismRunner):Build Prism (
cd sdks && go build -o /tmp/prism ./go/cmd/prism) and runpython repro.py --prism_location=/tmp/prism. Python'sHistogramCell.to_runner_api_monitoring_infois a stub returningNone, so this script wires it tomonitoring_infos.user_histogramto reportUSER_HISTOGRAMpayloads from theLOOPBACKworker.Before the fix (
master):bounded_tries: []andhistograms: [], and the first assertion fails.After the fix:
bounded_tries: [{('root', 'a', False), ('root', 'b', False)}]andhistograms: [(3, [1, 1, 1, 0, 0])].