...

Source file src/go.opencensus.io/plugin/runmetrics/example_test.go

Documentation: go.opencensus.io/plugin/runmetrics

     1  package runmetrics_test
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"log"
     7  	"sort"
     8  
     9  	"go.opencensus.io/metric/metricdata"
    10  	"go.opencensus.io/metric/metricexport"
    11  	"go.opencensus.io/plugin/runmetrics"
    12  )
    13  
    14  type printExporter struct {
    15  }
    16  
    17  func (l *printExporter) ExportMetrics(ctx context.Context, data []*metricdata.Metric) error {
    18  	mapData := make(map[string]metricdata.Metric, 0)
    19  
    20  	for _, v := range data {
    21  		mapData[v.Descriptor.Name] = *v
    22  	}
    23  
    24  	mapKeys := make([]string, 0, len(mapData))
    25  	for key := range mapData {
    26  		mapKeys = append(mapKeys, key)
    27  	}
    28  	sort.Strings(mapKeys)
    29  
    30  	// for the sake of a simple example, we cannot use the real value here
    31  	simpleVal := func(v interface{}) int { return 42 }
    32  
    33  	for _, k := range mapKeys {
    34  		v := mapData[k]
    35  		fmt.Printf("%s %d\n", k, simpleVal(v.TimeSeries[0].Points[0].Value))
    36  	}
    37  
    38  	return nil
    39  }
    40  
    41  func ExampleEnable() {
    42  
    43  	// Enable collection of runtime metrics and supply options
    44  	err := runmetrics.Enable(runmetrics.RunMetricOptions{
    45  		EnableCPU:    true,
    46  		EnableMemory: true,
    47  		Prefix:       "mayapp/",
    48  	})
    49  	if err != nil {
    50  		log.Fatal(err)
    51  	}
    52  
    53  	// Use your reader/exporter to extract values
    54  	// This part is not specific to runtime metrics and only here to make it a complete example.
    55  	metricexport.NewReader().ReadAndExport(&printExporter{})
    56  
    57  	// output:
    58  	// mayapp/process/cpu_cgo_calls 42
    59  	// mayapp/process/cpu_goroutines 42
    60  	// mayapp/process/gc_cpu_fraction 42
    61  	// mayapp/process/gc_sys 42
    62  	// mayapp/process/heap_alloc 42
    63  	// mayapp/process/heap_idle 42
    64  	// mayapp/process/heap_inuse 42
    65  	// mayapp/process/heap_objects 42
    66  	// mayapp/process/heap_release 42
    67  	// mayapp/process/last_gc_finished_timestamp 42
    68  	// mayapp/process/memory_alloc 42
    69  	// mayapp/process/memory_frees 42
    70  	// mayapp/process/memory_lookups 42
    71  	// mayapp/process/memory_malloc 42
    72  	// mayapp/process/next_gc_heap_size 42
    73  	// mayapp/process/num_forced_gc 42
    74  	// mayapp/process/num_gc 42
    75  	// mayapp/process/other_sys 42
    76  	// mayapp/process/pause_total 42
    77  	// mayapp/process/stack_inuse 42
    78  	// mayapp/process/stack_mcache_inuse 42
    79  	// mayapp/process/stack_mspan_inuse 42
    80  	// mayapp/process/sys_heap 42
    81  	// mayapp/process/sys_memory_alloc 42
    82  	// mayapp/process/sys_stack 42
    83  	// mayapp/process/sys_stack_mcache 42
    84  	// mayapp/process/sys_stack_mspan 42
    85  	// mayapp/process/total_memory_alloc 42
    86  }
    87  

View as plain text