github.com/apache/beam/sdks/v2@v2.48.2/go/examples/snippets/10metrics.go (about)

     1  // Licensed to the Apache Software Foundation (ASF) under one or more
     2  // contributor license agreements.  See the NOTICE file distributed with
     3  // this work for additional information regarding copyright ownership.
     4  // The ASF licenses this file to You under the Apache License, Version 2.0
     5  // (the "License"); you may not use this file except in compliance with
     6  // the License.  You may obtain a copy of the License at
     7  //
     8  //    http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package snippets
    17  
    18  import (
    19  	"context"
    20  	"fmt"
    21  	"reflect"
    22  
    23  	"github.com/apache/beam/sdks/v2/go/pkg/beam"
    24  	"github.com/apache/beam/sdks/v2/go/pkg/beam/core/metrics"
    25  )
    26  
    27  // [START metrics_query]
    28  
    29  func queryMetrics(pr beam.PipelineResult, ns, n string) metrics.QueryResults {
    30  	return pr.Metrics().Query(func(r beam.MetricResult) bool {
    31  		return r.Namespace() == ns && r.Name() == n
    32  	})
    33  }
    34  
    35  // [END metrics_query]
    36  
    37  var runner = "direct"
    38  
    39  // [START metrics_pipeline]
    40  
    41  func addMetricDoFnToPipeline(s beam.Scope, input beam.PCollection) beam.PCollection {
    42  	return beam.ParDo(s, &MyMetricsDoFn{}, input)
    43  }
    44  
    45  func executePipelineAndGetMetrics(ctx context.Context, p *beam.Pipeline) (metrics.QueryResults, error) {
    46  	pr, err := beam.Run(ctx, runner, p)
    47  	if err != nil {
    48  		return metrics.QueryResults{}, err
    49  	}
    50  
    51  	// Request the metric called "counter1" in namespace called "namespace"
    52  	ms := pr.Metrics().Query(func(r beam.MetricResult) bool {
    53  		return r.Namespace() == "namespace" && r.Name() == "counter1"
    54  	})
    55  
    56  	// Print the metric value - there should be only one line because there is
    57  	// only one metric called "counter1" in the namespace called "namespace"
    58  	for _, c := range ms.Counters() {
    59  		fmt.Println(c.Namespace(), "-", c.Name(), ":", c.Committed)
    60  	}
    61  	return ms, nil
    62  }
    63  
    64  type MyMetricsDoFn struct {
    65  	counter beam.Counter
    66  }
    67  
    68  func init() {
    69  	beam.RegisterType(reflect.TypeOf((*MyMetricsDoFn)(nil)))
    70  }
    71  
    72  func (fn *MyMetricsDoFn) Setup() {
    73  	// While metrics can be defined in package scope or dynamically
    74  	// it's most efficient to include them in the DoFn.
    75  	fn.counter = beam.NewCounter("namespace", "counter1")
    76  }
    77  
    78  func (fn *MyMetricsDoFn) ProcessElement(ctx context.Context, v beam.V, emit func(beam.V)) {
    79  	// count the elements
    80  	fn.counter.Inc(ctx, 1)
    81  	emit(v)
    82  }
    83  
    84  // [END metrics_pipeline]