github.com/kaydxh/golang@v0.0.131/pkg/monitor/opentelemetry/opentelemetry_test.go (about)

     1  /*
     2   *Copyright (c) 2022, kaydxh
     3   *
     4   *Permission is hereby granted, free of charge, to any person obtaining a copy
     5   *of this software and associated documentation files (the "Software"), to deal
     6   *in the Software without restriction, including without limitation the rights
     7   *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   *copies of the Software, and to permit persons to whom the Software is
     9   *furnished to do so, subject to the following conditions:
    10   *
    11   *The above copyright notice and this permission notice shall be included in all
    12   *copies or substantial portions of the Software.
    13   *
    14   *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   *SOFTWARE.
    21   */
    22  package opentelemetry_test
    23  
    24  import (
    25  	"fmt"
    26  	"net/http"
    27  	"testing"
    28  	"time"
    29  
    30  	opentelemetry_ "github.com/kaydxh/golang/pkg/monitor/opentelemetry"
    31  	viper_ "github.com/kaydxh/golang/pkg/viper"
    32  	"github.com/sirupsen/logrus"
    33  	"go.opentelemetry.io/otel"
    34  	"go.opentelemetry.io/otel/attribute"
    35  	"go.opentelemetry.io/otel/metric"
    36  	"go.opentelemetry.io/otel/metric/global"
    37  	"golang.org/x/net/context"
    38  )
    39  
    40  const (
    41  	instrumentationName    = "github/kaydxh/instrument"
    42  	instrumentationVersion = "v0.0.1"
    43  )
    44  
    45  var (
    46  	meter = global.MeterProvider().Meter(
    47  		"",
    48  		metric.WithInstrumentationVersion(instrumentationVersion),
    49  	)
    50  
    51  	funcLoopCounter, _ = meter.SyncInt64().Counter("function_loops")
    52  	funcNameKey        = attribute.Key("function_name")
    53  )
    54  
    55  func memoryUsageCallBack(total, free uint64, usage float64) {
    56  	logrus.Infof("memory total: %v, free: %v, usage: %v", total, free, usage)
    57  }
    58  
    59  func TestMetric(t *testing.T) {
    60  	cfgFile := "./opentelemetry.yaml"
    61  	config := opentelemetry_.NewConfig(
    62  		opentelemetry_.WithViper(viper_.GetViper(cfgFile, "monitor.open_telemetry")),
    63  		opentelemetry_.WithMemoryCallBack(memoryUsageCallBack),
    64  	)
    65  
    66  	ctx := context.Background()
    67  	err := config.Complete().New(ctx)
    68  	if err != nil {
    69  		t.Errorf("failed to new config err: %v", err)
    70  	}
    71  	fmt.Printf("config: %+v", config.Proto.String())
    72  
    73  	go func() {
    74  		_ = http.ListenAndServe(":2222", nil)
    75  	}()
    76  
    77  	for {
    78  		metrics(ctx, 100)
    79  		time.Sleep(time.Second)
    80  	}
    81  
    82  }
    83  
    84  func metrics(ctx context.Context, n int) {
    85  	funcNameKV := funcNameKey.String("metrics")
    86  	for i := 0; i < n; i++ {
    87  		funcLoopCounter.Add(ctx, 1, funcNameKV)
    88  	}
    89  }
    90  
    91  // https://github.com/open-telemetry/opentelemetry-go/blob/main/example/jaeger/main.go
    92  func TestTrace(t *testing.T) {
    93  	cfgFile := "./opentelemetry.yaml"
    94  	config := opentelemetry_.NewConfig(opentelemetry_.WithViper(viper_.GetViper(cfgFile, "monitor.open_telemetry")))
    95  
    96  	ctx := context.Background()
    97  	err := config.Complete().New(ctx)
    98  	if err != nil {
    99  		t.Errorf("failed to new config err: %v", err)
   100  	}
   101  	fmt.Printf("config: %+v", config.Proto.String())
   102  
   103  	trace := otel.GetTracerProvider()
   104  	tr := trace.Tracer(instrumentationName)
   105  
   106  	ctx, span := tr.Start(ctx, "traceFunc")
   107  	defer span.End()
   108  
   109  	for {
   110  		doTrace(ctx)
   111  		time.Sleep(time.Second)
   112  	}
   113  
   114  }
   115  
   116  func doTrace(ctx context.Context) {
   117  	// Use the global TracerProvider.
   118  	tr := otel.Tracer("component-doTrace")
   119  	_, span := tr.Start(ctx, "doTrace")
   120  	span.SetAttributes(attribute.Key("testset").String("value"))
   121  	defer span.End()
   122  
   123  	time.Sleep(200 * time.Millisecond)
   124  }