lostluck commented on a change in pull request #13272:
URL: https://github.com/apache/beam/pull/13272#discussion_r523128139



##########
File path: sdks/go/pkg/beam/core/runtime/metricsx/metricsx.go
##########
@@ -0,0 +1,181 @@
+// 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 metricsx
+
+import (
+       "bytes"
+       "fmt"
+       "log"
+       "time"
+
+       "github.com/apache/beam/sdks/go/pkg/beam/core/graph/coder"
+       "github.com/apache/beam/sdks/go/pkg/beam/core/metrics"
+       pipepb "github.com/apache/beam/sdks/go/pkg/beam/model/pipeline_v1"
+)
+
+// FromMonitoringInfos extracts metrics from monitored states and
+// groups them into counters, distributions and gauges.
+func FromMonitoringInfos(attempted []*pipepb.MonitoringInfo, committed 
[]*pipepb.MonitoringInfo) *metrics.Results {
+       ac, ad, ag := groupByType(attempted)
+       cc, cd, cg := groupByType(committed)
+
+       return metrics.NewResults(mergeCounters(ac, cc), mergeDistributions(ad, 
cd), mergeGauges(ag, cg))
+}
+
+func groupByType(minfos []*pipepb.MonitoringInfo) (
+       map[metrics.StepKey]int64,
+       map[metrics.StepKey]metrics.DistributionValue,
+       map[metrics.StepKey]metrics.GaugeValue) {
+       counters := make(map[metrics.StepKey]int64)
+       distributions := make(map[metrics.StepKey]metrics.DistributionValue)
+       gauges := make(map[metrics.StepKey]metrics.GaugeValue)
+
+       for _, minfo := range minfos {
+               key, err := extractKey(minfo)
+               if err != nil {
+                       log.Println(err)
+                       continue
+               }
+
+               r := bytes.NewReader(minfo.GetPayload())
+
+               switch minfo.GetType() {
+               case "beam:metrics:sum_int64:v1":
+                       value, err := extractCounterValue(r)
+                       if err != nil {
+                               log.Println(err)
+                               continue
+                       }
+                       counters[key] = value
+               case "beam:metrics:distribution_int64:v1":
+                       value, err := extractDistributionValue(r)
+                       if err != nil {
+                               log.Println(err)
+                               continue
+                       }
+                       distributions[key] = value
+               case
+                       "beam:metrics:latest_int64:v1",
+                       "beam:metrics:top_n_int64:v1",
+                       "beam:metrics:bottom_n_int64:v1":
+                       value, err := extractGaugeValue(r)
+                       if err != nil {
+                               log.Println(err)
+                               continue
+                       }
+                       gauges[key] = value
+               default:
+                       log.Println("unknown metric type")
+               }
+       }
+       return counters, distributions, gauges
+}
+
+func mergeCounters(
+       attempted map[metrics.StepKey]int64,
+       committed map[metrics.StepKey]int64) []metrics.CounterResult {
+       res := make([]metrics.CounterResult, 0)
+
+       for k := range attempted {
+               v, ok := committed[k]
+               if !ok {
+                       v = -1
+               }

Review comment:
        If the user created a counter, and no increments occurred, the counter 
will never appear in the final results. It would be missing entirely. Beam 
counters operate on a "diff per bundle" set up, and the counters aren't known 
to the runner a-priory, so a runner can't known about them in advanced.
   
   0 would be correct for committed values that have an attempted value, as no 
count has been committed.
   
   I can't think of a useful case of distinguishing between "does not exist" 
for committed only, and simply saying 0. 
   
   Another reason: users can also use negative values for counters/increments 
if they so choose. It's not incorrect. So -1 isn't a useful way to signal 
non-existance.
   




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

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to