[
https://issues.apache.org/jira/browse/BEAM-3545?focusedWorklogId=84653&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-84653
]
ASF GitHub Bot logged work on BEAM-3545:
----------------------------------------
Author: ASF GitHub Bot
Created on: 27/Mar/18 01:17
Start Date: 27/Mar/18 01:17
Worklog Time Spent: 10m
Work Description: lostluck commented on a change in pull request #4899:
[BEAM-3545] Go SDK UserCounters
URL: https://github.com/apache/beam/pull/4899#discussion_r177281280
##########
File path: sdks/go/pkg/beam/metrics_test.go
##########
@@ -0,0 +1,119 @@
+// 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 beam_test
+
+import (
+ "context"
+ "regexp"
+ "time"
+
+ "github.com/apache/beam/sdks/go/pkg/beam"
+ "github.com/apache/beam/sdks/go/pkg/beam/core/util/bundle"
+ "github.com/apache/beam/sdks/go/pkg/beam/core/util/ptransform"
+
+ "github.com/apache/beam/sdks/go/pkg/beam/core/metrics"
+)
+
+func ctxWithPtransformID(id string) context.Context {
+ ctx := context.Background()
+ ctx = ptransform.SetID(ctx, id)
+ ctx = bundle.SetID(ctx, "exampleBundle")
+ return ctx
+}
+
+func dumpAndClearMetrics() {
+ metrics.DumpToOut()
+ metrics.Clear()
+}
+
+var (
+ wordRE = regexp.MustCompile(`[a-zA-Z]+('[a-z])?`)
+)
+
+func Example_metricsDeclaredAnywhere() {
+
+ // Metrics can be declared both inside or outside DoFns.
+ outside := beam.GetCounter("example.namespace", "count")
+
+ extractWordsDofn := func(ctx context.Context, line string, emit
func(string)) {
+ inside := beam.GetDistribution("example.namespace",
"characters")
+ for _, word := range wordRE.FindAllString(line, -1) {
+ emit(word)
+ outside.Inc(ctx, 1)
+ inside.Update(ctx, int64(len(word)))
+ }
+ }
+ ctx := ctxWithPtransformID("example")
+ extractWordsDofn(ctx, "this has six words in it", func(string) {})
+ extractWordsDofn(ctx, "this has seven words in it, see?", func(string)
{})
+
+ dumpAndClearMetrics()
+ // Output: Bundle: "exampleBundle" - PTransformID: "example"
+ // example.namespace.characters - count: 13 sum: 43 min: 2 max: 5
+ // example.namespace.count - value: 13
+}
+
+func Example_metricsReusable() {
+
+ // Metrics can be used in multiple DoFns
+ c := beam.GetCounter("example.reusable", "count")
+
+ extractWordsDofn := func(ctx context.Context, line string, emit
func(string)) {
+ for _, word := range wordRE.FindAllString(line, -1) {
+ emit(word)
+ c.Inc(ctx, 1)
+ }
+ }
+
+ extractRunesDofn := func(ctx context.Context, line string, emit
func(rune)) {
+ for _, r := range line {
+ emit(r)
+ c.Inc(ctx, 1)
+ }
+ }
+ extractWordsDofn(ctxWithPtransformID("extract1"), "this has six words
in it", func(string) {})
+
+ extractRunesDofn(ctxWithPtransformID("extract2"), "seven thousand",
func(rune) {})
+
+ dumpAndClearMetrics()
+ // Output: Bundle: "exampleBundle" - PTransformID: "extract1"
+ // example.reusable.count - value: 6
+ // Bundle: "exampleBundle" - PTransformID: "extract2"
+ // example.reusable.count - value: 14
+}
+
+var ctx = context.Background()
Review comment:
Yup! Added a comment to that effect, and moved it to the top.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
Issue Time Tracking
-------------------
Worklog Id: (was: 84653)
Time Spent: 7.5h (was: 7h 20m)
> Fn API metrics in Go SDK harness
> --------------------------------
>
> Key: BEAM-3545
> URL: https://issues.apache.org/jira/browse/BEAM-3545
> Project: Beam
> Issue Type: Sub-task
> Components: sdk-go
> Reporter: Kenneth Knowles
> Assignee: Robert Burke
> Priority: Major
> Labels: portability
> Time Spent: 7.5h
> Remaining Estimate: 0h
>
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)