Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit fa651b0

Browse files
panzerfahrerrghetia
authored andcommitted
Add runtime metrics support (#1156)
* Add runtime metrics support * Rename Options to RunMetricOptions * Make runmetrics producer registration easier with Enable/Disable * Rename and cleanup metric names
1 parent f58a717 commit fa651b0

File tree

6 files changed

+579
-0
lines changed

6 files changed

+579
-0
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ require (
44
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6
55
github.com/golang/protobuf v1.3.1
66
github.com/google/go-cmp v0.3.0
7+
github.com/stretchr/testify v1.4.0
78
golang.org/x/net v0.0.0-20190620200207-3b0461eec859
89
golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd // indirect
910
golang.org/x/text v0.3.2 // indirect
11+
google.golang.org/appengine v1.4.0 // indirect
1012
google.golang.org/genproto v0.0.0-20190425155659-357c62f0e4bb // indirect
1113
google.golang.org/grpc v1.20.1
1214
)

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
22
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
33
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
4+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
5+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
46
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
57
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
68
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6 h1:ZgQEtGgCBiWRM39fZuwSd1LwSqqSW0hOdXCYYDX0R3I=
@@ -12,6 +14,11 @@ github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg
1214
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
1315
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
1416
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
17+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
18+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
19+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
20+
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
21+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
1522
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
1623
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
1724
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
@@ -60,4 +67,7 @@ google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZi
6067
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
6168
google.golang.org/grpc v1.20.1 h1:Hz2g2wirWK7H0qIIhGIqRGTuMwTE8HEKFnDZZ7lm9NU=
6269
google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38=
70+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
71+
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
72+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
6373
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

plugin/runmetrics/doc.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2019, OpenCensus Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Package runmetrics contains support for runtime metrics.
16+
//
17+
// To enable collecting runtime metrics, just call Enable():
18+
//
19+
// _ := runmetrics.Enable(runmetrics.RunMetricOptions{
20+
// EnableCPU: true,
21+
// EnableMemory: true,
22+
// })
23+
package runmetrics // import "go.opencensus.io/plugin/runmetrics"

plugin/runmetrics/example_test.go

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

0 commit comments

Comments
 (0)