This repository has been archived by the owner on Jul 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 327
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add runtime metrics support * Rename Options to RunMetricOptions * Make runmetrics producer registration easier with Enable/Disable * Rename and cleanup metric names
- Loading branch information
1 parent
f58a717
commit fa651b0
Showing
6 changed files
with
579 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright 2019, OpenCensus Authors | ||
// | ||
// Licensed 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 runmetrics contains support for runtime metrics. | ||
// | ||
// To enable collecting runtime metrics, just call Enable(): | ||
// | ||
// _ := runmetrics.Enable(runmetrics.RunMetricOptions{ | ||
// EnableCPU: true, | ||
// EnableMemory: true, | ||
// }) | ||
package runmetrics // import "go.opencensus.io/plugin/runmetrics" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package runmetrics_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"go.opencensus.io/metric/metricdata" | ||
"go.opencensus.io/metric/metricexport" | ||
"go.opencensus.io/plugin/runmetrics" | ||
"log" | ||
"sort" | ||
) | ||
|
||
type printExporter struct { | ||
} | ||
|
||
func (l *printExporter) ExportMetrics(ctx context.Context, data []*metricdata.Metric) error { | ||
mapData := make(map[string]metricdata.Metric, 0) | ||
|
||
for _, v := range data { | ||
mapData[v.Descriptor.Name] = *v | ||
} | ||
|
||
mapKeys := make([]string, 0, len(mapData)) | ||
for key := range mapData { | ||
mapKeys = append(mapKeys, key) | ||
} | ||
sort.Strings(mapKeys) | ||
|
||
// for the sake of a simple example, we cannot use the real value here | ||
simpleVal := func(v interface{}) int { return 42 } | ||
|
||
for _, k := range mapKeys { | ||
v := mapData[k] | ||
fmt.Printf("%s %d\n", k, simpleVal(v.TimeSeries[0].Points[0].Value)) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func ExampleEnable() { | ||
|
||
// Enable collection of runtime metrics and supply options | ||
err := runmetrics.Enable(runmetrics.RunMetricOptions{ | ||
EnableCPU: true, | ||
EnableMemory: true, | ||
Prefix: "mayapp/", | ||
}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// Use your reader/exporter to extract values | ||
// This part is not specific to runtime metrics and only here to make it a complete example. | ||
metricexport.NewReader().ReadAndExport(&printExporter{}) | ||
|
||
// output: | ||
// mayapp/process/cpu_cgo_calls 42 | ||
// mayapp/process/cpu_goroutines 42 | ||
// mayapp/process/heap_alloc 42 | ||
// mayapp/process/heap_idle 42 | ||
// mayapp/process/heap_inuse 42 | ||
// mayapp/process/heap_objects 42 | ||
// mayapp/process/heap_release 42 | ||
// mayapp/process/memory_alloc 42 | ||
// mayapp/process/memory_frees 42 | ||
// mayapp/process/memory_lookups 42 | ||
// mayapp/process/memory_malloc 42 | ||
// mayapp/process/stack_inuse 42 | ||
// mayapp/process/stack_mcache_inuse 42 | ||
// mayapp/process/stack_mspan_inuse 42 | ||
// mayapp/process/sys_heap 42 | ||
// mayapp/process/sys_memory_alloc 42 | ||
// mayapp/process/sys_stack 42 | ||
// mayapp/process/sys_stack_mcache 42 | ||
// mayapp/process/sys_stack_mspan 42 | ||
// mayapp/process/total_memory_alloc 42 | ||
} |
Oops, something went wrong.