You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2020/12/08 16:59:06 UTC
[GitHub] [beam] kamilwu opened a new pull request #13505: [BEAM-11425] Metrics extraction via Monitoring API
kamilwu opened a new pull request #13505:
URL: https://github.com/apache/beam/pull/13505
For more details, refer to JIRA description: https://issues.apache.org/jira/browse/BEAM-11425
------------------------
Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
- [ ] [**Choose reviewer(s)**](https://beam.apache.org/contribute/#make-your-change) and mention them in a comment (`R: @username`).
- [ ] Format the pull request title like `[BEAM-XXX] Fixes bug in ApproximateQuantiles`, where you replace `BEAM-XXX` with the appropriate JIRA issue, if applicable. This will automatically link the pull request to the issue.
- [ ] Update `CHANGES.md` with noteworthy changes.
- [ ] If this contribution is large, please file an Apache [Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf).
See the [Contributor Guide](https://beam.apache.org/contribute) for more tips on [how to make review process smoother](https://beam.apache.org/contribute/#make-reviewers-job-easier).
Post-Commit Tests Status (on master branch)
------------------------------------------------------------------------------------------------
Lang | SDK | Dataflow | Flink | Samza | Spark | Twister2
--- | --- | --- | --- | --- | --- | ---
Go | [](https://ci-beam.apache.org/job/beam_PostCommit_Go/lastCompletedBuild/) | --- | [](https://ci-beam.apache.org/job/beam_PostCommit_Go_VR_Flink/lastCompletedBuild/) | --- | [](https://ci-beam.apache.org/job/beam_PostCommit_Go_VR_Spark/lastCompletedBuild/) | ---
Java | [](https://ci-beam.apache.org/job/beam_PostCommit_Java/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Dataflow/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Java_VR_Dataflow_V2/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Dataflow_Java11/lastCompletedBuild/) | [](https://ci-beam
.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Flink/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Flink_Java11/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Java_PVR_Flink_Batch/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Java_PVR_Flink_Streaming/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Samza/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Spark/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Java_PVR_Spark_Batch/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_SparkStructuredStreaming/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Twister2/lastCompletedBuild/)
Python | [](https://ci-beam.apache.org/job/beam_PostCommit_Python36/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Python37/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Python38/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PostCommit_Py_VR_Dataflow/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Py_VR_Dataflow_V2/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Py_ValCont/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PreCommit_Python_PVR_Flink_Cron/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Python_VR_Flink/lastCompletedBuild/) | --- | [](https://ci-beam.apache.org/job/beam_PostCommit_Python_VR_Spark/lastCompletedBuild/) | ---
XLang | [](https://ci-beam.apache.org/job/beam_PostCommit_XVR_Direct/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PostCommit_XVR_Dataflow/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PostCommit_XVR_Flink/lastCompletedBuild/) | --- | [](https://ci-beam.apache.org/job/beam_PostCommit_XVR_Spark/lastCompletedBuild/) | ---
Pre-Commit Tests Status (on master branch)
------------------------------------------------------------------------------------------------
--- |Java | Python | Go | Website | Whitespace | Typescript
--- | --- | --- | --- | --- | --- | ---
Non-portable | [](https://ci-beam.apache.org/job/beam_PreCommit_Java_Cron/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PreCommit_Python_Cron/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PreCommit_PythonLint_Cron/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PreCommit_PythonDocker_Cron/lastCompletedBuild/) <br>[](https://ci-beam.apache.org/job/beam_PreCommit_PythonDocs_Cron/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PreCommit_Go_Cron/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PreCommit_Website_Cron/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PreCommit_Whitespace_Cron/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PreCommit_Typescript_Cron/lastCompletedBuild/)
Portable | --- | [](https://ci-beam.apache.org/job/beam_PreCommit_Portable_Python_Cron/lastCompletedBuild/) | --- | --- | --- | ---
See [.test-infra/jenkins/README](https://github.com/apache/beam/blob/master/.test-infra/jenkins/README.md) for trigger phrase, status and link of all Jenkins jobs.
GitHub Actions Tests Status (on master branch)
------------------------------------------------------------------------------------------------
[](https://github.com/apache/beam/actions?query=workflow%3A%22Build+python+source+distribution+and+wheels%22+branch%3Amaster+event%3Aschedule)
[](https://github.com/apache/beam/actions?query=workflow%3A%22Python+Tests%22+branch%3Amaster+event%3Aschedule)
[](https://github.com/apache/beam/actions?query=workflow%3A%22Java+Tests%22+branch%3Amaster+event%3Aschedule)
See [CI.md](https://github.com/apache/beam/blob/master/CI.md) for more information about GitHub Actions CI.
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [beam] kamilwu commented on pull request #13505: [BEAM-11425] Metrics extraction via Monitoring API
Posted by GitBox <gi...@apache.org>.
kamilwu commented on pull request #13505:
URL: https://github.com/apache/beam/pull/13505#issuecomment-741742713
@lostluck Thank you for review. I pushed all fixes.
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [beam] kamilwu commented on a change in pull request #13505: [BEAM-11425] Metrics extraction via Monitoring API
Posted by GitBox <gi...@apache.org>.
kamilwu commented on a change in pull request #13505:
URL: https://github.com/apache/beam/pull/13505#discussion_r539238569
##########
File path: sdks/go/pkg/beam/runners/dataflow/dataflowlib/metrics.go
##########
@@ -0,0 +1,125 @@
+// 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 dataflowlib translates a Beam pipeline model to the
+// Dataflow API job model, for submission to Google Cloud Dataflow.
+
+package dataflowlib
+
+import (
+ "encoding/json"
+ "fmt"
+
+ "github.com/apache/beam/sdks/go/pkg/beam/core/metrics"
+ df "google.golang.org/api/dataflow/v1b3"
+)
+
+// FromMetricUpdates extracts metrics from a slice of MetricUpdate objects and
+// groups them into counters, distributions and gauges.
+//
+// Dataflow currently only reports Counter and Distribution metrics to Cloud
+// Monitoring. Gauge metrics are not supported. The output metrics.Results will
+// not contain any gauges.
+func FromMetricUpdates(allMetrics []*df.MetricUpdate, job *df.Job) *metrics.Results {
+ ac, ad := groupByType(allMetrics, job, true)
+ cc, cd := groupByType(allMetrics, job, false)
+
+ return metrics.NewResults(metrics.MergeCounters(ac, cc), metrics.MergeDistributions(ad, cd), make([]metrics.GaugeResult, 0))
+}
+
+func groupByType(allMetrics []*df.MetricUpdate, job *df.Job, tentative bool) (
+ map[metrics.StepKey]int64,
+ map[metrics.StepKey]metrics.DistributionValue) {
+ counters := make(map[metrics.StepKey]int64)
+ distributions := make(map[metrics.StepKey]metrics.DistributionValue)
+
+ for _, metric := range allMetrics {
+ isTentative := metric.Name.Context["tentative"] == "true"
+ // Returns true when variables differ (exclusive or)
+ if (isTentative || tentative) && (!isTentative || !tentative) {
+ continue
+ }
+
+ key, err := extractKey(metric, job)
+ if err != nil {
+ continue
+ }
+
+ if metric.Scalar != nil {
+ v, err := extractCounterValue(metric.Scalar)
+ if err != nil {
+ continue
+ }
+ counters[key] = v
+ } else if metric.Distribution != nil {
+ v, err := extractDistributionValue(metric.Distribution)
+ if err != nil {
+ continue
+ }
+ distributions[key] = v
+ }
+ }
+ return counters, distributions
+}
+
+func extractKey(metric *df.MetricUpdate, job *df.Job) (metrics.StepKey, error) {
+ stepName, ok := metric.Name.Context["step"]
+ if !ok {
+ return metrics.StepKey{}, fmt.Errorf("could not find the internal step name")
+ }
+ userStepName := ""
+
+ for _, step := range job.Steps {
+ if step.Name == stepName {
+ properties := make(map[string]string)
+ json.Unmarshal(step.Properties, &properties)
+ userStepName = properties["user_name"]
+ break
+ }
+ }
+ if userStepName == "" {
+ return metrics.StepKey{}, fmt.Errorf("could not translate the internal step name %v", stepName)
+ }
+
+ namespace := metric.Name.Context["namespace"]
+ if namespace == "" {
+ namespace = "dataflow/v1b3"
+ }
+
+ return metrics.StepKey{Step: userStepName, Name: metric.Name.Name, Namespace: namespace}, nil
+}
+
+func extractCounterValue(obj interface{}) (int64, error) {
+ v, ok := obj.(float64)
+ if !ok {
+ return -1, fmt.Errorf("expected float64, got data of type %T instead", obj)
+ }
+ return int64(v), nil
+}
+
+func extractDistributionValue(obj interface{}) (metrics.DistributionValue, error) {
+ m := obj.(map[string]interface{})
+ propertiesToVisit := []string{"count", "sum", "min", "max"}
+ values := make([]int64, 4)
+
+ for i, p := range propertiesToVisit {
+ v, ok := m[p].(float64)
+ if !ok {
+ return metrics.DistributionValue{}, fmt.Errorf("expected float64, got data of type %T instead", m[p])
+ }
+ values[i] = int64(v)
+ }
+ return metrics.DistributionValue{Count: values[0], Sum: values[1], Min: values[2], Max: values[3]}, nil
+}
Review comment:
Good to hear that!
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [beam] lostluck commented on a change in pull request #13505: [BEAM-11425] Metrics extraction via Monitoring API
Posted by GitBox <gi...@apache.org>.
lostluck commented on a change in pull request #13505:
URL: https://github.com/apache/beam/pull/13505#discussion_r539556727
##########
File path: sdks/go/pkg/beam/runners/dataflow/dataflowlib/metrics.go
##########
@@ -0,0 +1,125 @@
+// 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 dataflowlib translates a Beam pipeline model to the
+// Dataflow API job model, for submission to Google Cloud Dataflow.
+
+package dataflowlib
+
+import (
+ "encoding/json"
+ "fmt"
+
+ "github.com/apache/beam/sdks/go/pkg/beam/core/metrics"
+ df "google.golang.org/api/dataflow/v1b3"
+)
+
+// FromMetricUpdates extracts metrics from a slice of MetricUpdate objects and
+// groups them into counters, distributions and gauges.
+//
+// Dataflow currently only reports Counter and Distribution metrics to Cloud
+// Monitoring. Gauge metrics are not supported. The output metrics.Results will
+// not contain any gauges.
+func FromMetricUpdates(allMetrics []*df.MetricUpdate, job *df.Job) *metrics.Results {
+ ac, ad := groupByType(allMetrics, job, true)
+ cc, cd := groupByType(allMetrics, job, false)
+
+ return metrics.NewResults(metrics.MergeCounters(ac, cc), metrics.MergeDistributions(ad, cd), make([]metrics.GaugeResult, 0))
+}
+
+func groupByType(allMetrics []*df.MetricUpdate, job *df.Job, tentative bool) (
+ map[metrics.StepKey]int64,
+ map[metrics.StepKey]metrics.DistributionValue) {
+ counters := make(map[metrics.StepKey]int64)
+ distributions := make(map[metrics.StepKey]metrics.DistributionValue)
+
+ for _, metric := range allMetrics {
+ isTentative := metric.Name.Context["tentative"] == "true"
+ // Returns true when variables differ (exclusive or)
+ if (isTentative || tentative) && (!isTentative || !tentative) {
Review comment:
No worries. I've been there myself.
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [beam] kamilwu commented on a change in pull request #13505: [BEAM-11425] Metrics extraction via Monitoring API
Posted by GitBox <gi...@apache.org>.
kamilwu commented on a change in pull request #13505:
URL: https://github.com/apache/beam/pull/13505#discussion_r539248435
##########
File path: sdks/go/pkg/beam/runners/dataflow/dataflowlib/metrics.go
##########
@@ -0,0 +1,125 @@
+// 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 dataflowlib translates a Beam pipeline model to the
+// Dataflow API job model, for submission to Google Cloud Dataflow.
+
+package dataflowlib
+
+import (
+ "encoding/json"
+ "fmt"
+
+ "github.com/apache/beam/sdks/go/pkg/beam/core/metrics"
+ df "google.golang.org/api/dataflow/v1b3"
+)
+
+// FromMetricUpdates extracts metrics from a slice of MetricUpdate objects and
+// groups them into counters, distributions and gauges.
+//
+// Dataflow currently only reports Counter and Distribution metrics to Cloud
+// Monitoring. Gauge metrics are not supported. The output metrics.Results will
+// not contain any gauges.
+func FromMetricUpdates(allMetrics []*df.MetricUpdate, job *df.Job) *metrics.Results {
+ ac, ad := groupByType(allMetrics, job, true)
+ cc, cd := groupByType(allMetrics, job, false)
+
+ return metrics.NewResults(metrics.MergeCounters(ac, cc), metrics.MergeDistributions(ad, cd), make([]metrics.GaugeResult, 0))
+}
+
+func groupByType(allMetrics []*df.MetricUpdate, job *df.Job, tentative bool) (
+ map[metrics.StepKey]int64,
+ map[metrics.StepKey]metrics.DistributionValue) {
+ counters := make(map[metrics.StepKey]int64)
+ distributions := make(map[metrics.StepKey]metrics.DistributionValue)
+
+ for _, metric := range allMetrics {
+ isTentative := metric.Name.Context["tentative"] == "true"
+ // Returns true when variables differ (exclusive or)
+ if (isTentative || tentative) && (!isTentative || !tentative) {
Review comment:
Right, I overcomplicated it. Thank you.
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [beam] lostluck commented on pull request #13505: [BEAM-11425] Metrics extraction via Monitoring API
Posted by GitBox <gi...@apache.org>.
lostluck commented on pull request #13505:
URL: https://github.com/apache/beam/pull/13505#issuecomment-742733157
Nope. Sorry about that.
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [beam] lostluck merged pull request #13505: [BEAM-11425] Metrics extraction via Monitoring API
Posted by GitBox <gi...@apache.org>.
lostluck merged pull request #13505:
URL: https://github.com/apache/beam/pull/13505
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [beam] kamilwu commented on a change in pull request #13505: [BEAM-11425] Metrics extraction via Monitoring API
Posted by GitBox <gi...@apache.org>.
kamilwu commented on a change in pull request #13505:
URL: https://github.com/apache/beam/pull/13505#discussion_r539246687
##########
File path: sdks/go/pkg/beam/runners/dataflow/dataflowlib/execute.go
##########
@@ -122,3 +135,21 @@ func PrintJob(ctx context.Context, job *df.Job) {
}
log.Info(ctx, string(str))
}
+
+type dataflowPipelineResult struct {
+ JobID string
Review comment:
I had to add the `JobID()` to the `PipelineResult` interface also. But I think it's fine, since most runners have some kind of a job ID.
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [beam] lostluck commented on a change in pull request #13505: [BEAM-11425] Metrics extraction via Monitoring API
Posted by GitBox <gi...@apache.org>.
lostluck commented on a change in pull request #13505:
URL: https://github.com/apache/beam/pull/13505#discussion_r539014592
##########
File path: sdks/go/pkg/beam/runners/dataflow/dataflowlib/execute.go
##########
@@ -122,3 +135,21 @@ func PrintJob(ctx context.Context, job *df.Job) {
}
log.Info(ctx, string(str))
}
+
+type dataflowPipelineResult struct {
+ JobID string
Review comment:
Note that an exported field on an unexported struct is still nominally inaccessible. Consider unexporting it, and having a JobID() method instead.
##########
File path: sdks/go/pkg/beam/runners/dataflow/dataflowlib/metrics.go
##########
@@ -0,0 +1,125 @@
+// 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 dataflowlib translates a Beam pipeline model to the
+// Dataflow API job model, for submission to Google Cloud Dataflow.
+
+package dataflowlib
+
+import (
+ "encoding/json"
+ "fmt"
+
+ "github.com/apache/beam/sdks/go/pkg/beam/core/metrics"
+ df "google.golang.org/api/dataflow/v1b3"
+)
+
+// FromMetricUpdates extracts metrics from a slice of MetricUpdate objects and
+// groups them into counters, distributions and gauges.
+//
+// Dataflow currently only reports Counter and Distribution metrics to Cloud
+// Monitoring. Gauge metrics are not supported. The output metrics.Results will
+// not contain any gauges.
+func FromMetricUpdates(allMetrics []*df.MetricUpdate, job *df.Job) *metrics.Results {
+ ac, ad := groupByType(allMetrics, job, true)
+ cc, cd := groupByType(allMetrics, job, false)
+
+ return metrics.NewResults(metrics.MergeCounters(ac, cc), metrics.MergeDistributions(ad, cd), make([]metrics.GaugeResult, 0))
+}
+
+func groupByType(allMetrics []*df.MetricUpdate, job *df.Job, tentative bool) (
+ map[metrics.StepKey]int64,
+ map[metrics.StepKey]metrics.DistributionValue) {
+ counters := make(map[metrics.StepKey]int64)
+ distributions := make(map[metrics.StepKey]metrics.DistributionValue)
+
+ for _, metric := range allMetrics {
+ isTentative := metric.Name.Context["tentative"] == "true"
+ // Returns true when variables differ (exclusive or)
+ if (isTentative || tentative) && (!isTentative || !tentative) {
Review comment:
If the goal here is to only provide either the tentative values OR the commited values, and not both, wouldn't
```
if isTentative != tentative {
continue
}
```
be simpler?
##########
File path: sdks/go/pkg/beam/runners/dataflow/dataflowlib/metrics.go
##########
@@ -0,0 +1,125 @@
+// 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 dataflowlib translates a Beam pipeline model to the
+// Dataflow API job model, for submission to Google Cloud Dataflow.
+
+package dataflowlib
+
+import (
+ "encoding/json"
+ "fmt"
+
+ "github.com/apache/beam/sdks/go/pkg/beam/core/metrics"
+ df "google.golang.org/api/dataflow/v1b3"
+)
+
+// FromMetricUpdates extracts metrics from a slice of MetricUpdate objects and
+// groups them into counters, distributions and gauges.
+//
+// Dataflow currently only reports Counter and Distribution metrics to Cloud
+// Monitoring. Gauge metrics are not supported. The output metrics.Results will
+// not contain any gauges.
+func FromMetricUpdates(allMetrics []*df.MetricUpdate, job *df.Job) *metrics.Results {
+ ac, ad := groupByType(allMetrics, job, true)
+ cc, cd := groupByType(allMetrics, job, false)
+
+ return metrics.NewResults(metrics.MergeCounters(ac, cc), metrics.MergeDistributions(ad, cd), make([]metrics.GaugeResult, 0))
+}
+
+func groupByType(allMetrics []*df.MetricUpdate, job *df.Job, tentative bool) (
+ map[metrics.StepKey]int64,
+ map[metrics.StepKey]metrics.DistributionValue) {
+ counters := make(map[metrics.StepKey]int64)
+ distributions := make(map[metrics.StepKey]metrics.DistributionValue)
+
+ for _, metric := range allMetrics {
+ isTentative := metric.Name.Context["tentative"] == "true"
+ // Returns true when variables differ (exclusive or)
+ if (isTentative || tentative) && (!isTentative || !tentative) {
+ continue
+ }
+
+ key, err := extractKey(metric, job)
+ if err != nil {
+ continue
+ }
+
+ if metric.Scalar != nil {
+ v, err := extractCounterValue(metric.Scalar)
+ if err != nil {
+ continue
+ }
+ counters[key] = v
+ } else if metric.Distribution != nil {
+ v, err := extractDistributionValue(metric.Distribution)
+ if err != nil {
+ continue
+ }
+ distributions[key] = v
+ }
+ }
+ return counters, distributions
+}
+
+func extractKey(metric *df.MetricUpdate, job *df.Job) (metrics.StepKey, error) {
+ stepName, ok := metric.Name.Context["step"]
+ if !ok {
+ return metrics.StepKey{}, fmt.Errorf("could not find the internal step name")
+ }
+ userStepName := ""
+
+ for _, step := range job.Steps {
+ if step.Name == stepName {
+ properties := make(map[string]string)
+ json.Unmarshal(step.Properties, &properties)
+ userStepName = properties["user_name"]
+ break
+ }
+ }
+ if userStepName == "" {
+ return metrics.StepKey{}, fmt.Errorf("could not translate the internal step name %v", stepName)
+ }
+
+ namespace := metric.Name.Context["namespace"]
+ if namespace == "" {
+ namespace = "dataflow/v1b3"
+ }
+
+ return metrics.StepKey{Step: userStepName, Name: metric.Name.Name, Namespace: namespace}, nil
+}
+
+func extractCounterValue(obj interface{}) (int64, error) {
+ v, ok := obj.(float64)
+ if !ok {
+ return -1, fmt.Errorf("expected float64, got data of type %T instead", obj)
+ }
+ return int64(v), nil
+}
+
+func extractDistributionValue(obj interface{}) (metrics.DistributionValue, error) {
+ m := obj.(map[string]interface{})
+ propertiesToVisit := []string{"count", "sum", "min", "max"}
+ values := make([]int64, 4)
Review comment:
Since we know we have a fixed 4 properties anyway, consider just using an array [4]int64 instead of a slice. It works the same.
##########
File path: sdks/go/pkg/beam/runners/dataflow/dataflowlib/metrics.go
##########
@@ -0,0 +1,125 @@
+// 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 dataflowlib translates a Beam pipeline model to the
+// Dataflow API job model, for submission to Google Cloud Dataflow.
+
+package dataflowlib
+
+import (
+ "encoding/json"
+ "fmt"
+
+ "github.com/apache/beam/sdks/go/pkg/beam/core/metrics"
+ df "google.golang.org/api/dataflow/v1b3"
+)
+
+// FromMetricUpdates extracts metrics from a slice of MetricUpdate objects and
+// groups them into counters, distributions and gauges.
+//
+// Dataflow currently only reports Counter and Distribution metrics to Cloud
+// Monitoring. Gauge metrics are not supported. The output metrics.Results will
+// not contain any gauges.
+func FromMetricUpdates(allMetrics []*df.MetricUpdate, job *df.Job) *metrics.Results {
+ ac, ad := groupByType(allMetrics, job, true)
+ cc, cd := groupByType(allMetrics, job, false)
+
+ return metrics.NewResults(metrics.MergeCounters(ac, cc), metrics.MergeDistributions(ad, cd), make([]metrics.GaugeResult, 0))
+}
+
+func groupByType(allMetrics []*df.MetricUpdate, job *df.Job, tentative bool) (
+ map[metrics.StepKey]int64,
+ map[metrics.StepKey]metrics.DistributionValue) {
+ counters := make(map[metrics.StepKey]int64)
+ distributions := make(map[metrics.StepKey]metrics.DistributionValue)
+
+ for _, metric := range allMetrics {
+ isTentative := metric.Name.Context["tentative"] == "true"
+ // Returns true when variables differ (exclusive or)
+ if (isTentative || tentative) && (!isTentative || !tentative) {
+ continue
+ }
+
+ key, err := extractKey(metric, job)
+ if err != nil {
+ continue
+ }
+
+ if metric.Scalar != nil {
+ v, err := extractCounterValue(metric.Scalar)
+ if err != nil {
+ continue
+ }
+ counters[key] = v
+ } else if metric.Distribution != nil {
+ v, err := extractDistributionValue(metric.Distribution)
+ if err != nil {
+ continue
+ }
+ distributions[key] = v
+ }
+ }
+ return counters, distributions
+}
+
+func extractKey(metric *df.MetricUpdate, job *df.Job) (metrics.StepKey, error) {
+ stepName, ok := metric.Name.Context["step"]
+ if !ok {
+ return metrics.StepKey{}, fmt.Errorf("could not find the internal step name")
+ }
+ userStepName := ""
+
+ for _, step := range job.Steps {
+ if step.Name == stepName {
+ properties := make(map[string]string)
+ json.Unmarshal(step.Properties, &properties)
+ userStepName = properties["user_name"]
+ break
+ }
+ }
+ if userStepName == "" {
+ return metrics.StepKey{}, fmt.Errorf("could not translate the internal step name %v", stepName)
+ }
+
+ namespace := metric.Name.Context["namespace"]
+ if namespace == "" {
+ namespace = "dataflow/v1b3"
+ }
+
+ return metrics.StepKey{Step: userStepName, Name: metric.Name.Name, Namespace: namespace}, nil
+}
+
+func extractCounterValue(obj interface{}) (int64, error) {
+ v, ok := obj.(float64)
+ if !ok {
+ return -1, fmt.Errorf("expected float64, got data of type %T instead", obj)
+ }
+ return int64(v), nil
+}
+
+func extractDistributionValue(obj interface{}) (metrics.DistributionValue, error) {
+ m := obj.(map[string]interface{})
+ propertiesToVisit := []string{"count", "sum", "min", "max"}
+ values := make([]int64, 4)
+
+ for i, p := range propertiesToVisit {
+ v, ok := m[p].(float64)
+ if !ok {
+ return metrics.DistributionValue{}, fmt.Errorf("expected float64, got data of type %T instead", m[p])
+ }
+ values[i] = int64(v)
+ }
+ return metrics.DistributionValue{Count: values[0], Sum: values[1], Min: values[2], Max: values[3]}, nil
+}
Review comment:
I love this implementation. That is all.
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [beam] kamilwu commented on a change in pull request #13505: [BEAM-11425] Metrics extraction via Monitoring API
Posted by GitBox <gi...@apache.org>.
kamilwu commented on a change in pull request #13505:
URL: https://github.com/apache/beam/pull/13505#discussion_r539231825
##########
File path: sdks/go/pkg/beam/runners/dataflow/dataflowlib/execute.go
##########
@@ -122,3 +135,21 @@ func PrintJob(ctx context.Context, job *df.Job) {
}
log.Info(ctx, string(str))
}
+
+type dataflowPipelineResult struct {
+ JobID string
Review comment:
Let's have a JobID() method. I think accessing a job ID might be useful in some cases.
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [beam] kamilwu commented on pull request #13505: [BEAM-11425] Metrics extraction via Monitoring API
Posted by GitBox <gi...@apache.org>.
kamilwu commented on pull request #13505:
URL: https://github.com/apache/beam/pull/13505#issuecomment-743082101
Thank you!
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [beam] kamilwu commented on pull request #13505: [BEAM-11425] Metrics extraction via Monitoring API
Posted by GitBox <gi...@apache.org>.
kamilwu commented on pull request #13505:
URL: https://github.com/apache/beam/pull/13505#issuecomment-740778958
R: @lostluck Could you take a look?
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [beam] kamilwu commented on pull request #13505: [BEAM-11425] Metrics extraction via Monitoring API
Posted by GitBox <gi...@apache.org>.
kamilwu commented on pull request #13505:
URL: https://github.com/apache/beam/pull/13505#issuecomment-741727921
Run Go PostCommit
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [beam] kamilwu commented on pull request #13505: [BEAM-11425] Metrics extraction via Monitoring API
Posted by GitBox <gi...@apache.org>.
kamilwu commented on pull request #13505:
URL: https://github.com/apache/beam/pull/13505#issuecomment-740764079
Run Go PostCommit
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [beam] kamilwu commented on pull request #13505: [BEAM-11425] Metrics extraction via Monitoring API
Posted by GitBox <gi...@apache.org>.
kamilwu commented on pull request #13505:
URL: https://github.com/apache/beam/pull/13505#issuecomment-742689292
@lostluck Is there anything else I need to do, or can we merge this already?
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [beam] lostluck commented on a change in pull request #13505: [BEAM-11425] Metrics extraction via Monitoring API
Posted by GitBox <gi...@apache.org>.
lostluck commented on a change in pull request #13505:
URL: https://github.com/apache/beam/pull/13505#discussion_r539571853
##########
File path: sdks/go/pkg/beam/runners/dataflow/dataflowlib/execute.go
##########
@@ -122,3 +135,21 @@ func PrintJob(ctx context.Context, job *df.Job) {
}
log.Info(ctx, string(str))
}
+
+type dataflowPipelineResult struct {
+ JobID string
Review comment:
SGTM for JobID. Remember that in Go it's not necessary to return the exact interface that's supported every time. If the dataflow results or the flink results need different method sets, they can export different methods, and just document what are available on a custom results interface, that can be type asserted to.
----------------------------------------------------------------
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:
users@infra.apache.org