github.com/facebookincubator/go-belt@v0.0.0-20230703220935-39cd348f1a38/tool/experimental/metrics/examples/doc_test.go (about)

     1  // Copyright 2022 Meta Platforms, Inc. and affiliates.
     2  //
     3  // Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
     4  //
     5  // 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
     6  //
     7  // 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
     8  //
     9  // 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
    10  //
    11  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    12  
    13  package metrics_test
    14  
    15  import (
    16  	"context"
    17  
    18  	"github.com/facebookincubator/go-belt"
    19  	"github.com/facebookincubator/go-belt/beltctx"
    20  	"github.com/facebookincubator/go-belt/tool/experimental/metrics"
    21  	"github.com/facebookincubator/go-belt/tool/experimental/metrics/implementation/prometheus"
    22  	"github.com/facebookincubator/go-belt/tool/experimental/metrics/implementation/tsmetrics"
    23  )
    24  
    25  func Example() {
    26  	var m metrics.Metrics
    27  
    28  	// easy to use:
    29  	m = prometheus.Default()
    30  	someFunction(1, m)
    31  
    32  	// implementation agnostic:
    33  	m = tsmetrics.New()
    34  	someFunction(2, m)
    35  
    36  	// one may still reuse all the features of the backend Metrics:
    37  	m.(*tsmetrics.Metrics).Registry.SetDisabled(true)
    38  
    39  	// use go-belt to manage the Metrics
    40  	obs := belt.New()
    41  	obs = metrics.BeltWithMetrics(obs, m)
    42  	someBeltyFunction(3, obs)
    43  
    44  	// use context to manage the Metrics
    45  	ctx := context.Background()
    46  	ctx = metrics.CtxWithMetrics(ctx, m)
    47  	someContextyFunction(ctx, 4)
    48  
    49  	// use a singletony Metrics:
    50  	metrics.Default = func() metrics.Metrics {
    51  		return m
    52  	}
    53  	yetOneMoreFunction(5)
    54  }
    55  
    56  func someFunction(arg int, m metrics.Metrics) {
    57  	// experience close to logrus/zap:
    58  	m = metrics.WithField(m, "arg", arg)
    59  	anotherFunction(m)
    60  }
    61  
    62  func anotherFunction(m metrics.Metrics) {
    63  	m.Count("hello").Add(1)
    64  }
    65  
    66  func someBeltyFunction(arg int, obs *belt.Belt) {
    67  	obs = obs.WithField("arg", arg)
    68  	anotherBeltyFunction(obs)
    69  }
    70  
    71  func anotherBeltyFunction(obs *belt.Belt) {
    72  	metrics.FromBelt(obs).Count("hello").Add(1)
    73  }
    74  
    75  func someContextyFunction(ctx context.Context, arg int) {
    76  	ctx = beltctx.WithField(ctx, "arg", arg)
    77  	anotherContextyFunction(ctx)
    78  }
    79  
    80  func anotherContextyFunction(ctx context.Context) {
    81  	metrics.FromCtx(ctx).Count("hello").Add(1)
    82  }
    83  
    84  func yetOneMoreFunction(arg int) {
    85  	m := metrics.Default()
    86  	m = metrics.WithField(m, "arg", arg)
    87  	m.Count("hello").Add(1)
    88  }